LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
Loading...
Searching...
No Matches
refutils.cc
1
5#include "refutils.h"
6
7#include <lf/io/io.h>
8#include <lf/mesh/utils/utils.h>
9
10#include <fstream>
11
12namespace lf::refinement {
13
14inline int normalize_idx(glb_idx_t idx) {
15 LF_ASSERT_MSG(idx == idx_nil || idx <= std::numeric_limits<int>::max(),
16 "Error, trying to convert " << idx << "into an int");
17 return (idx == idx_nil) ? -1 : static_cast<int>(idx);
18}
19
20void WriteMatlabLevel(const MeshHierarchy &hier_mesh, size_type level,
21 std::string filename) {
22 // Preprocess filename: append .m, if not there already
23 {
24 const size_type fn_len = filename.size();
25 if ((fn_len > 1) && (filename[fn_len - 2] != '.') &&
26 (filename[fn_len - 1] != 'm')) {
27 filename += ".m";
28 }
29 }
30 // Open file for writing
31 std::ofstream file(filename);
32 if (file.is_open()) {
33 // Start regular writing operations
34 // Remove trailing .m
35 filename.pop_back();
36 filename.pop_back();
37 file << "function [PTPAR,EDPAR,CELLPAR] = " << filename << "()" << '\n';
38 file << "% Parent data for a hybid 2D mesh" << '\n';
39
40 // Mesh on the selected level
41 const std::shared_ptr<const mesh::Mesh> mesh = hier_mesh.getMesh(level);
42 {
43 // Output parent information for nodes
44 const std::vector<ParentInfo> &pt_parent_info(
45 hier_mesh.ParentInfos(level, 2));
46 const size_type no_nodes = mesh->NumEntities(2);
47 for (int k = 0; k < no_nodes; k++) {
48 file << "PTPAR(" << k + 1 << ",:) = ["
49 << normalize_idx(pt_parent_info[k].parent_index) << " , "
50 << normalize_idx(pt_parent_info[k].child_number) << "];" << '\n';
51 }
52 }
53
54 {
55 // Output parent information for edge
56 const std::vector<ParentInfo> &ed_parent_info(
57 hier_mesh.ParentInfos(level, 1));
58 const size_type no_edges = mesh->NumEntities(1);
59 for (int k = 0; k < no_edges; k++) {
60 file << "EDPAR(" << k + 1 << ",:) = ["
61 << normalize_idx(ed_parent_info[k].parent_index) << " , "
62 << normalize_idx(ed_parent_info[k].child_number) << "];" << '\n';
63 }
64 }
65
66 {
67 // Output parent information for cells
68 const std::vector<ParentInfo> &cell_parent_info(
69 hier_mesh.ParentInfos(level, 0));
70 const std::vector<glb_idx_t> &ref_eds(hier_mesh.RefinementEdges(level));
71 const size_type no_cells = mesh->NumEntities(0);
72 for (int k = 0; k < no_cells; k++) {
73 file << "CELLPAR(" << k + 1 << ",:) = ["
74 << normalize_idx(cell_parent_info[k].parent_index) << " , "
75 << normalize_idx(cell_parent_info[k].child_number) << " , "
76 << normalize_idx(ref_eds[k]) << " ];" << '\n';
77 }
78 }
79 }
80}
81
82void WriteMatlab(const MeshHierarchy &hier_mesh, const std::string &basename) {
83 const size_type n_levels = hier_mesh.NumLevels();
84 for (int level = 0; level < n_levels; level++) {
85 // prepare filename
86 std::stringstream level_asc;
87 level_asc << level;
88 const std::string filebase = basename + "_L" + level_asc.str();
89
90 // Fetch mesh on the current level
91 const std::shared_ptr<const mesh::Mesh> mesh = hier_mesh.getMesh(level);
92
93 // Output of mesh data
94 io::writeMatlab(*mesh, filebase + ".m");
95 // Output of parent/refinement edge information
96 WriteMatlabLevel(hier_mesh, level, filebase + "_pi.m");
97 }
98}
99
100} // namespace lf::refinement
A hierarchy of nested 2D hybrid meshes created by refinement.
std::shared_ptr< const mesh::Mesh > getMesh(size_type level) const
access the mesh on a particular level
const std::vector< ParentInfo > & ParentInfos(size_type level, dim_t codim) const
Fetch information about parents.
const std::vector< sub_idx_t > & RefinementEdges(size_type level) const
Access refinement edge indices.
size_type NumLevels() const
number of meshes contained in the hierarchy, 1 for a single mesh
void writeMatlab(const lf::mesh::Mesh &mesh, std::string filename)
Writes affine triangulation data to file in MATLAB format.
tools for regular or local refinement of 2D hybrid meshes
void WriteMatlab(const MeshHierarchy &hier_mesh, const std::string &basename)
Generate MATLAB code describing a multilevel hierarchy of meshes.
Definition refutils.cc:82
const unsigned int idx_nil
int normalize_idx(glb_idx_t idx)
Definition refutils.cc:14
void WriteMatlabLevel(const MeshHierarchy &hier_mesh, size_type level, std::string filename)
Generate MATLAB function providing parent/child information.
Definition refutils.cc:20