LehrFEM++ 1.0.0
A simple Finite Element Library for teaching
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) ? int(-1) : 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 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 << "()"
38 << std::endl;
39 file << "% Parent data for a hybid 2D mesh" << std::endl;
40
41 // Mesh on the selected level
42 std::shared_ptr<const mesh::Mesh> mesh = hier_mesh.getMesh(level);
43 {
44 // Output parent information for nodes
45 const std::vector<ParentInfo> &pt_parent_info(
46 hier_mesh.ParentInfos(level, 2));
47 const size_type no_nodes = mesh->NumEntities(2);
48 for (int k = 0; k < no_nodes; k++) {
49 file << "PTPAR(" << k + 1 << ",:) = ["
50 << normalize_idx(pt_parent_info[k].parent_index) << " , "
51 << normalize_idx(pt_parent_info[k].child_number) << "];"
52 << std::endl;
53 }
54 }
55
56 {
57 // Output parent information for edge
58 const std::vector<ParentInfo> &ed_parent_info(
59 hier_mesh.ParentInfos(level, 1));
60 const size_type no_edges = mesh->NumEntities(1);
61 for (int k = 0; k < no_edges; k++) {
62 file << "EDPAR(" << k + 1 << ",:) = ["
63 << normalize_idx(ed_parent_info[k].parent_index) << " , "
64 << normalize_idx(ed_parent_info[k].child_number) << "];"
65 << std::endl;
66 }
67 }
68
69 {
70 // Output parent information for cells
71 const std::vector<ParentInfo> &cell_parent_info(
72 hier_mesh.ParentInfos(level, 0));
73 const std::vector<glb_idx_t> &ref_eds(hier_mesh.RefinementEdges(level));
74 const size_type no_cells = mesh->NumEntities(0);
75 for (int k = 0; k < no_cells; k++) {
76 file << "CELLPAR(" << k + 1 << ",:) = ["
77 << normalize_idx(cell_parent_info[k].parent_index) << " , "
78 << normalize_idx(cell_parent_info[k].child_number) << " , "
79 << normalize_idx(ref_eds[k]) << " ];" << std::endl;
80 }
81 }
82 }
83}
84
85void WriteMatlab(const MeshHierarchy &hier_mesh, const std::string &basename) {
86 const size_type n_levels = hier_mesh.NumLevels();
87 for (int level = 0; level < n_levels; level++) {
88 // prepare filename
89 std::stringstream level_asc;
90 level_asc << level;
91 std::string filebase = basename + "_L" + level_asc.str();
92
93 // Fetch mesh on the current level
94 std::shared_ptr<const mesh::Mesh> mesh = hier_mesh.getMesh(level);
95
96 // Output of mesh data
97 io::writeMatlab(*mesh, filebase + ".m");
98 // Output of parent/refinement edge information
99 WriteMatlabLevel(hier_mesh, level, filebase + "_pi.m");
100 }
101}
102
103} // 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
unsigned int size_type
general type for variables related to size of arrays
Definition: base.h:24
unsigned int glb_idx_t
type for global index of mesh entities (nodes, edges, cells)
Definition: base.h:28
void writeMatlab(const lf::mesh::Mesh &mesh, std::string filename)
Writes affine triangulation data to file in MATLAB format.
Definition: write_matlab.cc:9
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:85
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