16 #include "mesh_reader_h2d_bson.h"
30 MeshReaderH2DBSON::MeshReaderH2DBSON()
34 MeshReaderH2DBSON::~MeshReaderH2DBSON()
38 void MeshReaderH2DBSON::load(
const char *filename, MeshSharedPtr mesh)
41 throw Exceptions::NullException(1);
46 fpr = fopen(filename,
"rb");
49 fseek(fpr, 0, SEEK_END);
50 int size = ftell(fpr);
54 char *datar = malloc_with_check<char>(size);
55 fread(datar, size, 1, fpr);
59 bson_init_finished_data(&br, datar, 0);
65 bson_find(&it, &br,
"vertex-count");
66 int vertices_count = bson_iterator_int(&it);
69 int mesh_size = HashTable::H2D_DEFAULT_HASH_SIZE;
70 while (mesh_size < 8 * vertices_count)
72 mesh->init(mesh_size);
75 double* vertex_xes =
new double[vertices_count];
76 double* vertex_yes =
new double[vertices_count];
79 bson_iterator it_coeffs;
80 bson_find(&it_coeffs, &br,
"vertex-x");
81 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
82 bson_iterator_init(&it, &sub);
84 while (bson_iterator_next(&it))
85 vertex_xes[index_coeff++] = bson_iterator_double(&it);
88 bson_find(&it_coeffs, &br,
"vertex-y");
89 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
90 bson_iterator_init(&it, &sub);
92 while (bson_iterator_next(&it))
93 vertex_yes[index_coeff++] = bson_iterator_double(&it);
96 for (
int vertex_i = 0; vertex_i < vertices_count; vertex_i++)
98 Node* node = mesh->nodes.add();
99 assert(node->id == vertex_i);
100 node->ref = TOP_LEVEL_REF;
101 node->type = HERMES_TYPE_VERTEX;
103 node->p1 = node->p2 = -1;
104 node->next_hash =
nullptr;
105 node->x = vertex_xes[vertex_i];
106 node->y = vertex_yes[vertex_i];
108 mesh->ntopvert = vertices_count;
114 bson_find(&it, &br,
"element-count");
115 int element_count = bson_iterator_int(&it);
116 mesh->nbase = mesh->nactive = mesh->ninitial = element_count;
118 int* vertex_0 =
new int[element_count];
119 int* vertex_1 =
new int[element_count];
120 int* vertex_2 =
new int[element_count];
121 int* vertex_3 =
new int[element_count];
122 const char** markers =
new const char*[element_count];
125 bson_find(&it_coeffs, &br,
"element-1");
126 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
127 bson_iterator_init(&it, &sub);
129 while (bson_iterator_next(&it))
130 vertex_0[index_coeff++] = bson_iterator_int(&it);
132 bson_find(&it_coeffs, &br,
"element-2");
133 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
134 bson_iterator_init(&it, &sub);
136 while (bson_iterator_next(&it))
137 vertex_1[index_coeff++] = bson_iterator_int(&it);
139 bson_find(&it_coeffs, &br,
"element-3");
140 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
141 bson_iterator_init(&it, &sub);
143 while (bson_iterator_next(&it))
144 vertex_2[index_coeff++] = bson_iterator_int(&it);
146 bson_find(&it_coeffs, &br,
"element-4");
147 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
148 bson_iterator_init(&it, &sub);
150 while (bson_iterator_next(&it))
151 vertex_3[index_coeff++] = bson_iterator_int(&it);
153 bson_find(&it_coeffs, &br,
"element-marker");
154 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
155 bson_iterator_init(&it, &sub);
157 while (bson_iterator_next(&it))
158 markers[index_coeff++] = bson_iterator_string(&it);
161 for (
int element_i = 0; element_i < element_count; element_i++)
163 mesh->element_markers_conversion.insert_marker(markers[element_i]);
164 if (vertex_3[element_i] != -1)
165 e = mesh->create_quad(mesh->element_markers_conversion.get_internal_marker(markers[element_i]).marker,
166 &mesh->nodes[vertex_0[element_i]],
167 &mesh->nodes[vertex_1[element_i]],
168 &mesh->nodes[vertex_2[element_i]],
169 &mesh->nodes[vertex_3[element_i]],
172 e = mesh->create_triangle(mesh->element_markers_conversion.get_internal_marker(markers[element_i]).marker,
173 &mesh->nodes[vertex_0[element_i]],
174 &mesh->nodes[vertex_1[element_i]],
175 &mesh->nodes[vertex_2[element_i]],
186 bson_find(&it, &br,
"edge-count");
187 int edges_count = bson_iterator_int(&it);
189 int* vertex_0_edge =
new int[edges_count];
190 int* vertex_1_edge =
new int[edges_count];
191 const char** edge_markers =
new const char*[edges_count];
194 bson_find(&it_coeffs, &br,
"edge-1");
195 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
196 bson_iterator_init(&it, &sub);
198 while (bson_iterator_next(&it))
199 vertex_0_edge[index_coeff++] = bson_iterator_int(&it);
201 bson_find(&it_coeffs, &br,
"edge-2");
202 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
203 bson_iterator_init(&it, &sub);
205 while (bson_iterator_next(&it))
206 vertex_1_edge[index_coeff++] = bson_iterator_int(&it);
208 bson_find(&it_coeffs, &br,
"edge-marker");
209 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
210 bson_iterator_init(&it, &sub);
212 while (bson_iterator_next(&it))
213 edge_markers[index_coeff++] = bson_iterator_string(&it);
216 for (
unsigned int edge_i = 0; edge_i < edges_count; edge_i++)
218 en = mesh->peek_edge_node(vertex_0_edge[edge_i], vertex_1_edge[edge_i]);
220 throw Hermes::Exceptions::MeshLoadFailureException(
"Boundary data #%d: edge %d-%d does not exist.", edge_i, vertex_0_edge[edge_i], vertex_1_edge[edge_i]);
226 mesh->boundary_markers_conversion.insert_marker(edge_marker);
228 en->marker = mesh->boundary_markers_conversion.get_internal_marker(edge_marker).marker;
232 for_all_edge_nodes(node, mesh)
236 mesh->nodes[node->p1].bnd = 1;
237 mesh->nodes[node->p2].bnd = 1;
243 for_all_edge_nodes(en, mesh)
244 if (en->ref < 2 && en->marker == 0)
245 this->warn("Boundary edge node does not have a boundary marker.");
247 delete[] vertex_0_edge;
248 delete[] vertex_1_edge;
249 delete[] edge_markers;
255 bson_find(&it, &br, "arc-count");
256 int arc_count = bson_iterator_int(&it);
258 int* p1s = new
int[arc_count];
259 int* p2s = new
int[arc_count];
260 double* angles = new
double[arc_count];
263 bson_find(&it_coeffs, &br, "arcs-p1");
264 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
265 bson_iterator_init(&it, &sub);
267 while (bson_iterator_next(&it))
268 p1s[index_coeff++] = bson_iterator_int(&it);
270 bson_find(&it_coeffs, &br, "arcs-p2");
271 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
272 bson_iterator_init(&it, &sub);
274 while (bson_iterator_next(&it))
275 p2s[index_coeff++] = bson_iterator_int(&it);
277 bson_find(&it_coeffs, &br, "arcs-angle");
278 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
279 bson_iterator_init(&it, &sub);
281 while (bson_iterator_next(&it))
282 angles[index_coeff++] = bson_iterator_double(&it);
285 for (
unsigned int curves_i = 0; curves_i < arc_count; curves_i++)
297 angle = angles[curves_i];
299 curve = MeshUtil::load_arc(mesh, curves_i, &en, p1, p2, angle);
302 MeshUtil::assign_curve(en, curve, p1, p2);
306 for_all_used_elements(e, mesh)
308 if (e->cm !=
nullptr)
309 e->cm->update_refmap_coeffs(e);
310 RefMap::set_element_iro_cache(e);
318 bson_find(&it, &br,
"refinement-count");
319 int refinement_count = bson_iterator_int(&it);
320 int* refined_elements =
new int[refinement_count];
321 int* refinement_types =
new int[refinement_count];
324 bson_find(&it_coeffs, &br,
"refinement-id");
325 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
326 bson_iterator_init(&it, &sub);
328 while (bson_iterator_next(&it))
329 refined_elements[index_coeff++] = bson_iterator_int(&it);
331 bson_find(&it_coeffs, &br,
"refinement-type");
332 bson_iterator_subobject_init(&it_coeffs, &sub, 0);
333 bson_iterator_init(&it, &sub);
335 while (bson_iterator_next(&it))
336 refinement_types[index_coeff++] = bson_iterator_int(&it);
339 free_with_check(datar);
342 for (
unsigned int refinement_i = 0; refinement_i < refinement_count; refinement_i++)
344 int element_id = refined_elements[refinement_i];
345 int refinement_type = refinement_types[refinement_i];
346 if (refinement_type == -1)
347 mesh->unrefine_element_id(element_id);
349 mesh->refine_element_id(element_id, refinement_type);
352 delete[] refined_elements;
353 delete[] refinement_types;
355 if (HermesCommonApi.get_integral_param_value(checkMeshesOnLoad))
356 mesh->initial_single_check();
359 void MeshReaderH2DBSON::save(
const char *filename, MeshSharedPtr mesh)
370 bson_append_int(&bw,
"vertex-count", mesh->ntopvert);
372 bson_append_start_array(&bw,
"vertex-x");
373 for (
int i = 0; i < mesh->ntopvert; i++)
374 bson_append_double(&bw,
"c", mesh->nodes[i].x);
375 bson_append_finish_array(&bw);
377 bson_append_start_array(&bw,
"vertex-y");
378 for (
int i = 0; i < mesh->ntopvert; i++)
379 bson_append_double(&bw,
"c", mesh->nodes[i].y);
380 bson_append_finish_array(&bw);
384 bson_append_int(&bw,
"element-count", mesh->get_num_base_elements());
386 bson_append_start_array(&bw,
"element-1");
387 for (
int i = 0; i < mesh->get_num_base_elements(); i++)
389 e = mesh->get_element_fast(i);
390 bson_append_int(&bw,
"c", e->vn[0]->id);
392 bson_append_finish_array(&bw);
394 bson_append_start_array(&bw,
"element-2");
395 for (
int i = 0; i < mesh->get_num_base_elements(); i++)
397 e = mesh->get_element_fast(i);
398 bson_append_int(&bw,
"c", e->vn[1]->id);
400 bson_append_finish_array(&bw);
402 bson_append_start_array(&bw,
"element-3");
403 for (
int i = 0; i < mesh->get_num_base_elements(); i++)
405 e = mesh->get_element_fast(i);
406 bson_append_int(&bw,
"c", e->vn[2]->id);
408 bson_append_finish_array(&bw);
410 bson_append_start_array(&bw,
"element-4");
411 for (
int i = 0; i < mesh->get_num_base_elements(); i++)
413 e = mesh->get_element_fast(i);
414 if (e->is_triangle())
415 bson_append_int(&bw,
"c", -1);
417 bson_append_int(&bw,
"c", e->vn[3]->id);
419 bson_append_finish_array(&bw);
421 bson_append_start_array(&bw,
"element-marker");
422 for (
int i = 0; i < mesh->get_num_base_elements(); i++)
424 e = mesh->get_element_fast(i);
425 bson_append_string(&bw,
"c", mesh->get_element_markers_conversion().get_user_marker(e->marker).marker.c_str());
427 bson_append_finish_array(&bw);
432 for_all_base_elements(e, mesh)
433 for (
unsigned i = 0; i < e->get_nvert(); i++)
434 if (MeshUtil::get_base_edge_node(e, i)->marker)
436 bson_append_int(&bw, "edge-count", edge_count);
438 bson_append_start_array(&bw, "edge-1");
439 for_all_base_elements(e, mesh)
440 for (
unsigned i = 0; i < e->get_nvert(); i++)
441 if (MeshUtil::get_base_edge_node(e, i)->marker)
442 bson_append_int(&bw, "c", e->vn[i]->
id);
443 bson_append_finish_array(&bw);
445 bson_append_start_array(&bw, "edge-2");
446 for_all_base_elements(e, mesh)
447 for (
unsigned i = 0; i < e->get_nvert(); i++)
448 if (MeshUtil::get_base_edge_node(e, i)->marker)
449 bson_append_int(&bw, "c", e->vn[e->next_vert(i)]->
id);
450 bson_append_finish_array(&bw);
452 bson_append_start_array(&bw, "edge-marker");
453 for_all_base_elements(e, mesh)
454 for (
unsigned i = 0; i < e->get_nvert(); i++)
455 if (MeshUtil::get_base_edge_node(e, i)->marker)
456 bson_append_string(&bw, "c", mesh->boundary_markers_conversion.get_user_marker(MeshUtil::get_base_edge_node(e, i)->marker).marker.c_str());
457 bson_append_finish_array(&bw);
462 for_all_base_elements(e, mesh)
465 for (
unsigned i = 0; i < e->get_nvert(); i++)
466 if (e->cm->curves[i] !=
nullptr)
467 if (e->cm->curves[i]->type == ArcType)
470 bson_append_int(&bw,
"arc-count", arc_count);
472 bson_append_start_array(&bw,
"arcs-p1");
473 for_all_base_elements(e, mesh)
476 for (
unsigned i = 0; i < e->get_nvert(); i++)
477 if (e->cm->curves[i] !=
nullptr)
479 if (e->cm->curves[i]->type != ArcType)
481 bson_append_int(&bw,
"c", e->vn[i]->id);
484 bson_append_finish_array(&bw);
486 bson_append_start_array(&bw,
"arcs-p2");
487 for_all_base_elements(e, mesh)
490 for (
unsigned i = 0; i < e->get_nvert(); i++)
491 if (e->cm->curves[i] !=
nullptr)
493 if (e->cm->curves[i]->type != ArcType)
495 bson_append_int(&bw,
"c", e->vn[e->next_vert(i)]->id);
498 bson_append_finish_array(&bw);
500 bson_append_start_array(&bw,
"arcs-angle");
501 for_all_base_elements(e, mesh)
504 for (
unsigned i = 0; i < e->get_nvert(); i++)
505 if (e->cm->curves[i] !=
nullptr)
507 if (e->cm->curves[i]->type != ArcType)
509 bson_append_int(&bw,
"c", ((Arc*)e->cm->curves[i])->angle);
512 bson_append_finish_array(&bw);
516 for_all_base_elements(e, mesh)
518 for (
unsigned i = 0; i < e->get_nvert(); i++)
519 if (e->cm->curves[i] !=
nullptr)
520 if (e->cm->curves[i]->
type != ArcType)
521 throw Exceptions::Exception("BSON mesh loader can not operate with general NURBS so far.");
525 for (
unsigned int refinement_i = 0; refinement_i < mesh->refinements.size(); refinement_i++)
526 bson_append_int(&bw, "refinement-count", mesh->refinements.size());
528 bson_append_start_array(&bw, "refinement-
id");
529 for (
unsigned int refinement_i = 0; refinement_i < mesh->refinements.size(); refinement_i++)
530 bson_append_int(&bw, "c", mesh->refinements[refinement_i].first);
531 bson_append_finish_array(&bw);
533 bson_append_start_array(&bw, "refinement-
type");
534 for (
unsigned int refinement_i = 0; refinement_i < mesh->refinements.size(); refinement_i++)
535 bson_append_int(&bw, "c", mesh->refinements[refinement_i].second);
536 bson_append_finish_array(&bw);
543 fpw = fopen(filename, "wb");
544 const
char *dataw = (const
char *)bson_data(&bw);
545 fwrite(dataw, bson_size(&bw), 1, fpw);
551 void MeshReaderH2DBSON::save(const
char *filename,
std::vector<MeshSharedPtr> meshes)
554 std::map<std::pair<double, double>,
unsigned int> points_to_vertices;
556 std::map<std::pair<unsigned int, unsigned int>,
unsigned int> vertices_to_boundaries;
558 std::map<std::pair<unsigned int, unsigned int>,
bool> vertices_to_curves;
560 std::vector<element_BSON> elements;
561 std::vector<edge_BSON> edges;
562 std::vector<vertex_BSON> vertices;
563 std::vector<arc_BSON> arcs;
564 std::vector<subdomain_BSON> subdomains;
566 bool* baseElementsSaved =
new bool[meshes[0]->get_num_base_elements()];
567 memset(baseElementsSaved, 0,
sizeof(
bool)* meshes[0]->get_num_base_elements());
569 #pragma region Save to structures
571 for (
unsigned int meshes_i = 0; meshes_i < meshes.size(); meshes_i++)
573 bool hasAllElements = (meshes[meshes_i]->get_num_used_base_elements() == meshes[meshes_i]->get_num_base_elements());
575 subdomain_BSON subdomain;
578 std::map<unsigned int, unsigned int> vertices_to_vertices;
584 for (
int i = 0; i < meshes[meshes_i]->ntopvert; i++)
589 std::map<std::pair<double, double>,
unsigned int>::iterator it = points_to_vertices.find(std::pair<double, double>(meshes[meshes_i]->nodes[i].x, meshes[meshes_i]->nodes[i].y));
590 if (it != points_to_vertices.end())
591 vertices_to_vertices.insert(std::pair<unsigned int, unsigned int>(i, it->second));
594 int new_i = points_to_vertices.size();
595 vertices_to_vertices.insert(std::pair<unsigned int, unsigned int>(i, new_i));
596 points_to_vertices.insert(std::pair<std::pair<double, double>,
unsigned int>(std::pair<double, double>(meshes[meshes_i]->nodes[i].x, meshes[meshes_i]->nodes[i].y), points_to_vertices.size()));
598 vertices.push_back(vertex_BSON(meshes[meshes_i]->nodes[i].x, meshes[meshes_i]->nodes[i].y, new_i));
601 subdomain.vertices.push_back(vertices_to_vertices.find(i)->second);
605 for (
int i = 0; i < meshes[meshes_i]->get_num_base_elements(); i++)
607 e = &(meshes[meshes_i]->elements[i]);
610 if (!baseElementsSaved[e->id])
613 elements.push_back(element_BSON(vertices_to_vertices.find(e->vn[0]->id)->second, vertices_to_vertices.find(e->vn[1]->id)->second, vertices_to_vertices.find(e->vn[2]->id)->second, -1, meshes[meshes_i]->get_element_markers_conversion().get_user_marker(e->marker).marker, e->id));
615 elements.push_back(element_BSON(vertices_to_vertices.find(e->vn[0]->id)->second, vertices_to_vertices.find(e->vn[1]->id)->second, vertices_to_vertices.find(e->vn[2]->id)->second, vertices_to_vertices.find(e->vn[3]->id)->second, meshes[meshes_i]->get_element_markers_conversion().get_user_marker(e->marker).marker, e->id));
616 baseElementsSaved[e->id] =
true;
620 subdomain.elements.push_back(e->id);
624 bool has_inner_edges =
false;
625 for_all_base_elements(e, meshes[meshes_i])
627 for (
unsigned i = 0; i < e->get_nvert(); i++)
629 if (MeshUtil::get_base_edge_node(e, i)->bnd)
631 if (vertices_to_boundaries.find(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second))) == vertices_to_boundaries.end())
633 unsigned int edge_i = edges.size();
634 vertices_to_boundaries.insert(std::pair<std::pair<unsigned int, unsigned int>,
unsigned int>(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second)), edge_i));
635 edges.push_back(edge_BSON(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second, meshes[meshes_i]->boundary_markers_conversion.get_user_marker(MeshUtil::get_base_edge_node(e, i)->marker).marker, edge_i));
638 subdomain.boundary_edges.push_back(vertices_to_boundaries.find(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second)))->second);
641 has_inner_edges =
true;
647 for_all_base_elements(e, meshes[meshes_i])
648 for (
unsigned i = 0; i < e->get_nvert(); i++)
650 if (!MeshUtil::get_base_edge_node(e, i)->bnd)
652 if (vertices_to_boundaries.find(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second))) == vertices_to_boundaries.end())
654 unsigned int edge_i = edges.size();
655 vertices_to_boundaries.insert(std::pair<std::pair<unsigned int, unsigned int>,
unsigned int>(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second)), edge_i));
656 edges.push_back(edge_BSON(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second, meshes[meshes_i]->boundary_markers_conversion.get_user_marker(MeshUtil::get_base_edge_node(e, i)->marker).marker, edge_i));
659 subdomain.inner_edges.push_back(vertices_to_boundaries.find(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second)))->second);
665 for_all_base_elements(e, meshes[meshes_i])
668 for (
unsigned i = 0; i < e->get_nvert(); i++)
669 if (e->cm->curves[i] !=
nullptr)
670 if (vertices_to_curves.find(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second))) == vertices_to_curves.end())
672 if (e->cm->curves[i]->type == ArcType)
673 arcs.push_back(arc_BSON(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second, ((Arc*)e->cm->curves[i])->angle));
675 throw Exceptions::Exception(
"BSON mesh loader can not operate with general NURBS so far.");
677 vertices_to_curves.insert(std::pair<std::pair<unsigned int, unsigned int>,
bool>(std::pair<unsigned int, unsigned int>(std::min(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second), std::max(vertices_to_vertices.find(e->vn[i]->id)->second, vertices_to_vertices.find(e->vn[e->next_vert(i)]->id)->second)),
true));
682 for (
unsigned int refinement_i = 0; refinement_i < meshes[meshes_i]->refinements.size(); refinement_i++)
683 subdomain.refinements.push_back(refinement_BSON(meshes[meshes_i]->refinements[refinement_i].first, meshes[meshes_i]->refinements[refinement_i].second));
685 subdomains.push_back(subdomain);
688 delete[] baseElementsSaved;
690 std::sort(elements.begin(), elements.end(), elementCompare);
699 bson_append_start_array(&bw,
"vertices");
700 for (
int i = 0; i < vertices.size(); i++)
701 vertices[i].save_to_BSON(bw);
702 bson_append_finish_array(&bw);
704 bson_append_start_array(&bw,
"elements");
705 for (
int i = 0; i < elements.size(); i++)
706 elements[i].save_to_BSON(bw);
707 bson_append_finish_array(&bw);
709 bson_append_start_array(&bw,
"edges");
710 for (
int i = 0; i < edges.size(); i++)
711 edges[i].save_to_BSON(bw);
712 bson_append_finish_array(&bw);
714 bson_append_start_array(&bw,
"arcs");
715 for (
int i = 0; i < arcs.size(); i++)
716 arcs[i].save_to_BSON(bw);
717 bson_append_finish_array(&bw);
720 bson_append_start_array(&bw,
"subdomains");
721 for (
int i = 0; i < subdomains.size(); i++)
722 subdomains[i].save_to_BSON(bw);
723 bson_append_finish_array(&bw);
730 fpw = fopen(filename,
"wb");
731 const char *dataw = (
const char *)bson_data(&bw);
732 fwrite(dataw, bson_size(&bw), 1, fpw);
738 void MeshReaderH2DBSON::load(
const char *filename, std::vector<MeshSharedPtr> meshes)
741 fpr = fopen(filename,
"rb");
744 fseek(fpr, 0, SEEK_END);
745 int size = ftell(fpr);
749 char *datar = malloc_with_check<char>(size);
750 fread(datar, size, 1, fpr);
754 bson_init_finished_data(&br, datar, 0);
756 for (
unsigned int meshes_i = 0; meshes_i < meshes.size(); meshes_i++)
757 meshes.at(meshes_i)->free();
759 MeshSharedPtr global_mesh(
new Mesh);
761 std::map<int, int> vertex_is;
762 std::map<int, int> element_is;
763 std::map<int, int> edge_is;
765 std::vector<element_BSON> elements;
766 std::vector<edge_BSON> edges;
767 std::vector<vertex_BSON> vertices;
768 std::vector<arc_BSON> arcs;
769 std::vector<subdomain_BSON> subdomains;
772 this->load_domain(br, global_mesh, vertex_is, element_is, edge_is, elements, edges, vertices, arcs, subdomains);
774 int max_vertex_i = -1;
775 for (std::map<int, int>::iterator it = vertex_is.begin(); it != vertex_is.end(); ++it)
776 if (it->first > max_vertex_i)
777 max_vertex_i = it->first;
778 int max_element_i = -1;
779 for (std::map<int, int>::iterator it = element_is.begin(); it != element_is.end(); ++it)
780 if (it->first > max_element_i)
781 max_element_i = it->first;
783 for (std::map<int, int>::iterator it = edge_is.begin(); it != edge_is.end(); ++it)
784 if (it->first > max_edge_i)
785 max_edge_i = it->first;
788 unsigned int subdomains_count = subdomains.size();
789 if (subdomains_count != meshes.size())
790 throw Hermes::Exceptions::MeshLoadFailureException(
"Number of subdomains( = %u) does not equal the number of provided meshes in the vector( = %u).", subdomains_count, meshes.size());
792 for (
unsigned int subdomains_i = 0; subdomains_i < subdomains_count; subdomains_i++)
794 for (
int element_i = 0; element_i < elements.size(); element_i++)
796 element_BSON element = elements.at(element_i);
798 meshes[subdomains_i]->element_markers_conversion.insert_marker(element.marker);
800 for (
unsigned int edge_i = 0; edge_i < edges.size(); edge_i++)
802 edge_BSON edge = edges.at(edge_i);
804 meshes[subdomains_i]->boundary_markers_conversion.insert_marker(edge.marker);
808 for (
unsigned int subdomains_i = 0; subdomains_i < subdomains_count; subdomains_i++)
810 unsigned int vertex_number_count = subdomains.at(subdomains_i).vertices.empty() ? 0 : subdomains.at(subdomains_i).vertices.size();
811 unsigned int element_number_count = subdomains.at(subdomains_i).elements.empty() ? 0 : subdomains.at(subdomains_i).elements.size();
812 unsigned int boundary_edge_number_count = subdomains.at(subdomains_i).boundary_edges.empty() ? 0 : subdomains.at(subdomains_i).boundary_edges.size();
813 unsigned int inner_edge_number_count = subdomains.at(subdomains_i).inner_edges.empty() ? 0 : subdomains.at(subdomains_i).inner_edges.size();
816 if (element_number_count == 0 || element_number_count == elements.size())
818 meshes[subdomains_i]->copy(global_mesh);
820 if (!subdomains.at(subdomains_i).refinements.empty() && subdomains.at(subdomains_i).refinements.size() > 0)
823 for (
unsigned int i = 0; i < subdomains.at(subdomains_i).refinements.size(); i++)
825 int element_id = subdomains.at(subdomains_i).refinements.at(i).id;
826 int refinement_type = subdomains.at(subdomains_i).refinements.at(i).type;
827 if (refinement_type == -1)
828 meshes[subdomains_i]->unrefine_element_id(element_id);
830 meshes[subdomains_i]->refine_element_id(element_id, refinement_type);
838 std::map<unsigned int, unsigned int> vertex_vertex_numbers;
841 int size = HashTable::H2D_DEFAULT_HASH_SIZE;
842 while (size < 8 * vertex_number_count)
844 meshes[subdomains_i]->init(size);
847 if (vertex_number_count == 0)
848 vertex_number_count = vertices.size();
849 for (
unsigned int vertex_numbers_i = 0; vertex_numbers_i < vertex_number_count; vertex_numbers_i++)
851 unsigned int vertex_number;
852 if (vertex_number_count == vertices.size())
853 vertex_number = vertex_is[vertex_numbers_i];
856 vertex_number = subdomains.at(subdomains_i).vertices.at(vertex_numbers_i);
857 if (vertex_number > max_vertex_i)
858 throw Exceptions::MeshLoadFailureException(
"Wrong vertex number:%u in subdomain %u.", vertex_number, subdomains_i);
861 vertex_vertex_numbers.insert(std::pair<unsigned int, unsigned int>(vertex_number, vertex_numbers_i));
862 Node* node = meshes[subdomains_i]->nodes.add();
863 assert(node->id == vertex_numbers_i);
864 node->ref = TOP_LEVEL_REF;
865 node->type = HERMES_TYPE_VERTEX;
867 node->p1 = node->p2 = -1;
868 node->next_hash =
nullptr;
871 node->x = vertices[vertex_number].x;
872 node->y = vertices[vertex_number].y;
874 meshes[subdomains_i]->ntopvert = vertex_number_count;
877 unsigned int element_count = elements.size();
878 meshes[subdomains_i]->nbase = element_count;
879 meshes[subdomains_i]->nactive = meshes[subdomains_i]->ninitial = element_number_count;
882 int* elements_existing =
new int[element_count];
883 for (
int i = 0; i < element_count; i++)
884 elements_existing[i] = -1;
885 for (
int element_number_i = 0; element_number_i < element_number_count; element_number_i++)
887 int elementI = subdomains.at(subdomains_i).elements.at(element_number_i);
888 if (elementI > max_element_i)
889 throw Exceptions::MeshLoadFailureException(
"Wrong element number:%i in subdomain %u.", elementI, subdomains_i);
891 elements_existing[element_is[subdomains.at(subdomains_i).elements.at(element_number_i)]] = elementI;
893 for (
int element_i = 0; element_i < element_count; element_i++)
896 if (element_number_count == 0)
899 found = elements_existing[element_i] != -1;
903 meshes[subdomains_i]->elements.skip_slot()->cm =
nullptr;
907 element_BSON element(-1, -1, -1, -1,
"", -1);
908 for (
int searched_element_i = 0; searched_element_i < element_count; searched_element_i++)
910 element = elements.at(searched_element_i);
911 if (element.i == elements_existing[element_i])
915 throw Exceptions::MeshLoadFailureException(
"Element number wrong in the mesh file.");
917 if (element.v4 != -1)
918 e = meshes[subdomains_i]->create_quad(meshes[subdomains_i]->element_markers_conversion.get_internal_marker(element.marker).marker,
919 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v1)->second],
920 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v2)->second],
921 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v3)->second],
922 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v4)->second],
925 e = meshes[subdomains_i]->create_triangle(meshes[subdomains_i]->element_markers_conversion.get_internal_marker(element.marker).marker,
926 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v1)->second],
927 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v2)->second],
928 &meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(element.v3)->second],
933 if (boundary_edge_number_count == 0)
934 boundary_edge_number_count = edges.size();
936 for (
int boundary_edge_number_i = 0; boundary_edge_number_i < boundary_edge_number_count; boundary_edge_number_i++)
938 edge_BSON edge(-1, -1,
"", -1);
939 int domain_edge_count = edges.size();
940 for (
unsigned int to_find_i = 0; to_find_i < domain_edge_count; to_find_i++)
942 if (boundary_edge_number_count != domain_edge_count)
944 if (edges.at(to_find_i).i == subdomains.at(subdomains_i).boundary_edges.at(boundary_edge_number_i))
946 edge = edges.at(to_find_i);
952 if (edges.at(to_find_i).i == edge_is[boundary_edge_number_i])
954 edge = edges.at(to_find_i);
961 throw Exceptions::MeshLoadFailureException(
"Wrong boundary-edge number:%i in subdomain %u.", subdomains.at(subdomains_i).boundary_edges.at(boundary_edge_number_i), subdomains_i);
963 Node* en = meshes[subdomains_i]->peek_edge_node(vertex_vertex_numbers.find(edge.v1)->second, vertex_vertex_numbers.find(edge.v2)->second);
965 throw Hermes::Exceptions::MeshLoadFailureException(
"Boundary data error (edge %i does not exist).", boundary_edge_number_i);
967 en->marker = meshes[subdomains_i]->boundary_markers_conversion.get_internal_marker(edge.marker).marker;
969 meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(edge.v1)->second].bnd = 1;
970 meshes[subdomains_i]->nodes[vertex_vertex_numbers.find(edge.v2)->second].bnd = 1;
975 for (
int inner_edge_number_i = 0; inner_edge_number_i < inner_edge_number_count; inner_edge_number_i++)
977 edge_BSON edge(-1, -1,
"", -1);
979 for (
unsigned int to_find_i = 0; to_find_i < edges.size(); to_find_i++)
981 if (edges.at(to_find_i).i == subdomains.at(subdomains_i).inner_edges.at(inner_edge_number_i))
983 edge = edges.at(to_find_i);
989 throw Exceptions::MeshLoadFailureException(
"Wrong inner-edge number:%i in subdomain %u.", subdomains.at(subdomains_i).boundary_edges.at(inner_edge_number_i), subdomains_i);
991 Node* en = meshes[subdomains_i]->peek_edge_node(vertex_vertex_numbers.find(edge.v1)->second, vertex_vertex_numbers.find(edge.v2)->second);
993 throw Hermes::Exceptions::MeshLoadFailureException(
"Inner data error (edge %i does not exist).", inner_edge_number_i);
995 en->marker = meshes[subdomains_i]->boundary_markers_conversion.get_internal_marker(edge.marker).marker;
1001 unsigned int arc_count = arcs.empty() ? 0 : arcs.size();
1003 for (
unsigned int curves_i = 0; curves_i < arc_count; curves_i++)
1011 if (vertex_vertex_numbers.find(arcs.at(curves_i).p1) == vertex_vertex_numbers.end() ||
1012 vertex_vertex_numbers.find(arcs.at(curves_i).p2) == vertex_vertex_numbers.end())
1017 p1 = vertex_vertex_numbers.find(arcs.at(curves_i).p1)->second;
1018 p2 = vertex_vertex_numbers.find(arcs.at(curves_i).p2)->second;
1020 curve = MeshUtil::load_arc(meshes[subdomains_i], curves_i, &en, p1, p2, arcs.at(curves_i).angle,
true);
1021 if (curve ==
nullptr)
1026 MeshUtil::assign_curve(en, curve, p1, p2);
1030 for_all_used_elements(e, meshes[subdomains_i])
1032 if (e->cm !=
nullptr)
1033 e->cm->update_refmap_coeffs(e);
1034 RefMap::set_element_iro_cache(e);
1038 if (!subdomains.at(subdomains_i).refinements.empty() && subdomains.at(subdomains_i).refinements.size() > 0)
1041 for (
unsigned int i = 0; i < subdomains.at(subdomains_i).refinements.size(); i++)
1043 int element_id = subdomains.at(subdomains_i).refinements.at(i).id;
1044 int refinement_type = subdomains.at(subdomains_i).refinements.at(i).type;
1045 if (refinement_type == -1)
1046 meshes[subdomains_i]->unrefine_element_id(element_id);
1048 meshes[subdomains_i]->refine_element_id(element_id, refinement_type);
1052 delete[] elements_existing;
1054 meshes[subdomains_i]->seq = g_mesh_seq++;
1055 if (HermesCommonApi.get_integral_param_value(checkMeshesOnLoad))
1056 meshes[subdomains_i]->initial_single_check();
1059 free_with_check(datar);
1062 void MeshReaderH2DBSON::load_domain(bson& br, MeshSharedPtr mesh, std::map<int, int>& vertex_is, std::map<int, int>& element_is, std::map<int, int>& edge_is,
1063 std::vector<element_BSON>& elements, std::vector<edge_BSON>& edges, std::vector<vertex_BSON>& vertices, std::vector<arc_BSON>& arcs, std::vector<subdomain_BSON>& subdomains)
1065 bson_iterator it, it_sub;
1066 bson b_sub, b_sub_sub;
1067 bson_find(&it, &br,
"vertices");
1068 bson_iterator_subobject_init(&it, &b_sub, 0);
1069 bson_iterator_init(&it_sub, &b_sub);
1070 while (bson_iterator_next(&it_sub))
1073 bson_iterator_subobject_init(&it_sub, &b_sub_sub, 0);
1074 vertex.load_from_BSON(b_sub_sub);
1075 vertices.push_back(vertex);
1078 bson_find(&it, &br,
"elements");
1079 bson_iterator_subobject_init(&it, &b_sub, 0);
1080 bson_iterator_init(&it_sub, &b_sub);
1081 while (bson_iterator_next(&it_sub))
1083 element_BSON element;
1084 bson_iterator_subobject_init(&it_sub, &b_sub_sub, 0);
1085 element.load_from_BSON(b_sub_sub);
1086 elements.push_back(element);
1089 bson_find(&it, &br,
"edges");
1090 bson_iterator_subobject_init(&it, &b_sub, 0);
1091 bson_iterator_init(&it_sub, &b_sub);
1092 while (bson_iterator_next(&it_sub))
1095 bson_iterator_subobject_init(&it_sub, &b_sub_sub, 0);
1096 edge.load_from_BSON(b_sub_sub);
1097 edges.push_back(edge);
1100 bson_find(&it, &br,
"arcs");
1101 bson_iterator_subobject_init(&it, &b_sub, 0);
1102 bson_iterator_init(&it_sub, &b_sub);
1103 while (bson_iterator_next(&it_sub))
1106 bson_iterator_subobject_init(&it_sub, &b_sub_sub, 0);
1107 arc.load_from_BSON(b_sub_sub);
1108 arcs.push_back(arc);
1111 bson_find(&it, &br,
"subdomains");
1112 bson_iterator_subobject_init(&it, &b_sub, 0);
1113 bson_iterator_init(&it_sub, &b_sub);
1114 while (bson_iterator_next(&it_sub))
1116 subdomain_BSON subdomain;
1117 bson_iterator_subobject_init(&it_sub, &b_sub_sub, 0);
1118 subdomain.load_from_BSON(b_sub_sub);
1119 subdomains.push_back(subdomain);
1123 int vertices_count = vertices.size();
1126 int size = HashTable::H2D_DEFAULT_HASH_SIZE;
1127 while (size < 8 * vertices_count)
1132 for (
int vertex_i = 0; vertex_i < vertices_count; vertex_i++)
1134 Node* node = mesh->nodes.add();
1135 assert(node->id == vertex_i);
1136 node->ref = TOP_LEVEL_REF;
1137 node->type = HERMES_TYPE_VERTEX;
1139 node->p1 = node->p2 = -1;
1140 node->next_hash =
nullptr;
1142 if (vertices[vertex_i].i > H2D_MAX_NODE_ID - 1)
1143 throw Exceptions::MeshLoadFailureException(
"The index 'i' of vertex in the mesh file must be lower than %i.", H2D_MAX_NODE_ID);
1146 vertex_is.insert(std::pair<int, int>(vertices[vertex_i].i, vertex_i));
1149 node->x = vertices[vertex_i].x;
1150 node->y = vertices[vertex_i].y;
1152 mesh->ntopvert = vertices_count;
1155 unsigned int element_count = elements.size();
1156 mesh->nbase = mesh->nactive = mesh->ninitial = element_count;
1159 for (
int element_i = 0; element_i < element_count; element_i++)
1161 element_BSON element = elements[element_i];
1164 if (element.i > H2D_MAX_NODE_ID - 1)
1165 throw Exceptions::MeshLoadFailureException(
"The index 'i' of element in the mesh file must be lower than %i.", H2D_MAX_NODE_ID);
1167 element_is.insert(std::pair<int, int>(element.i, element_i));
1169 mesh->element_markers_conversion.insert_marker(element.marker);
1171 if (element.v4 != -1)
1172 e = mesh->create_quad(mesh->element_markers_conversion.get_internal_marker(element.marker).marker,
1173 &mesh->nodes[element.v1],
1174 &mesh->nodes[element.v2],
1175 &mesh->nodes[element.v3],
1176 &mesh->nodes[element.v4],
1179 e = mesh->create_triangle(mesh->element_markers_conversion.get_internal_marker(element.marker).marker,
1180 &mesh->nodes[element.v1],
1181 &mesh->nodes[element.v2],
1182 &mesh->nodes[element.v3],
1187 unsigned int edges_count = edges.size();
1190 for (
unsigned int edge_i = 0; edge_i < edges_count; edge_i++)
1192 int v1 = edges[edge_i].v1;
1193 int v2 = edges[edge_i].v2;
1196 if (edges[edge_i].i > H2D_MAX_NODE_ID - 1)
1197 throw Exceptions::MeshLoadFailureException(
"The index 'i' of edge in the mesh file must be lower than %i.", H2D_MAX_NODE_ID);
1199 edge_is.insert(std::pair<int, int>(edge_i, edges[edge_i].i));
1201 en = mesh->peek_edge_node(v1, v2);
1203 throw Hermes::Exceptions::MeshLoadFailureException(
"Boundary data #%d: edge %d-%d does not exist.", edge_i, v1, v2);
1208 unsigned int begin = edge_marker.find_first_not_of(
" \t\n");
1209 unsigned int end = edge_marker.find_last_not_of(
" \t\n");
1210 edge_marker.erase(end + 1, edge_marker.length());
1211 edge_marker.erase(0, begin);
1215 mesh->boundary_markers_conversion.insert_marker(edge_marker);
1216 int marker = mesh->boundary_markers_conversion.get_internal_marker(edge_marker).marker;
1218 en->marker = marker;
1222 for_all_edge_nodes(node, mesh)
1226 mesh->nodes[node->p1].bnd = 1;
1227 mesh->nodes[node->p2].bnd = 1;
1233 for_all_edge_nodes(en, mesh)
1234 if (en->ref < 2 && en->marker == 0)
1235 this->warn("Boundary edge node does not have a boundary marker.");
1239 unsigned int arc_count = arcs.size();
1241 for (
unsigned int curves_i = 0; curves_i < arc_count; curves_i++)
1250 p1 = arcs[curves_i].p1;
1251 p2 = arcs[curves_i].p2;
1253 curve = MeshUtil::load_arc(mesh, curves_i, &en, p1, p2, arcs[curves_i].angle);
1256 MeshUtil::assign_curve(en, curve, p1, p2);
1260 for_all_used_elements(e, mesh)
1262 if (e->cm !=
nullptr)
1263 e->cm->update_refmap_coeffs(e);
1264 RefMap::set_element_iro_cache(e);
::xsd::cxx::tree::type type
C++ type corresponding to the anyType XML Schema built-in type.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.