711 <<
"WRITE_MED IS EXPERIMENTAL, MAY CONTAIN BUGS, ALWAYS CHECK THE OUTPUT "
715 MEDfileVersionOpen((
char *)file.c_str(), MED_ACC_CREAT, MED_MAJOR_NUM,
716 MED_MINOR_NUM, MED_RELEASE_NUM);
720 "Cannot create MED file");
725 CHKERR MEDfichDesEcr(fid, (
char *)
"MED file generated by MoFEM");
728 char dtUnit[MED_SNAME_SIZE + 1] =
"";
729 char axisName[3 * MED_SNAME_SIZE + 1] =
"";
730 char axisUnit[3 * MED_SNAME_SIZE + 1] =
"";
732 PetscBool is_cubit_meshset = PETSC_TRUE;
733 int med_mesh_name_id = 0;
736 char mesh_name_char[255];
737 std::string mesh_name =
"Mesh";
740 CHKERR PetscOptionsBegin(PETSC_COMM_WORLD,
"",
"MED mesh options",
"");
741 CHKERR PetscOptionsString(
"-med_mesh_name",
"get med mesh name",
"",
742 mesh_name.c_str(), mesh_name_char, 255, PETSC_NULL);
743 ierr = PetscOptionsEnd();
746 mesh_name = mesh_name_char;
749 for (
auto &
m : *meshsets_ptr) {
750 if (
m->getName() == mesh_name) {
751 med_mesh_name_id =
m->getMeshsetId();
752 is_cubit_meshset = PETSC_FALSE;
759 for (
auto &
m : *meshsets_ptr) {
760 if (
m->getMeshsetId() == med_mesh_name_id)
761 mesh_name =
m->getName();
762 max_id = (max_id <
m->getMeshsetId()) ?
m->getMeshsetId() : max_id;
766 CHKERR MEDmeshCr(fid, mesh_name.c_str(), 3, 3, MED_UNSTRUCTURED_MESH,
767 "Mesh created", dtUnit, MED_SORT_DTIT, MED_CARTESIAN,
771 med_int family_id = 0;
772 std::map<std::vector<std::string>, std::tuple<med_int, std::vector<int>>>
774 std::map<EntityHandle, med_int> entityHandle_family_map;
777 shared_meshsets_map[std::vector<string>()] =
778 std::make_tuple(family_id, std::vector<int>());
781 auto get_set_name = [&](
const CubitMeshSets *iit) {
782 std::string bc_type_name;
783 if (iit->getBcTypeULong() &
BLOCKSET) {
785 bc_type_name = iit->getName();
786 if (bc_type_name ==
"NoNameSet") {
787 bc_type_name =
"BLOCKSET_";
788 bc_type_name += std::to_string(iit->getMeshsetId());
791 }
else if (iit->getBcTypeULong() &
SIDESET ||
792 iit->getBcTypeULong() &
NODESET) {
799 if ((iit->getBcType() & jj_bc_type).any()) {
801 std::string temp_bc_type_name = string(
CubitBCNames[jj + 1]);
802 if (temp_bc_type_name.length() > 10) {
803 temp_bc_type_name = temp_bc_type_name.substr(0, 4);
805 bc_type_name += temp_bc_type_name;
810 bc_type_name += std::to_string(iit->getMeshsetId());
812 bc_type_name =
"UnknownSet";
822 for (
auto &entity : *write_range_ptr) {
824 std::vector<int> shared_meshsets;
825 std::vector<string> shared_names;
828 auto add_shared_meshset = [&](
auto &other_meshset) {
829 std::string other_name = get_set_name(other_meshset);
830 if (std::find(shared_names.begin(), shared_names.end(), other_name) ==
831 shared_names.end()) {
832 shared_meshsets.push_back(other_meshset->getMeshsetId());
833 shared_names.push_back(other_name);
838 for (
auto &other_meshset : *meshsets_ptr) {
840 if (med_mesh_name_id == other_meshset->getMeshsetId())
843 Range other_entities;
845 moab.get_entities_by_handle(other_set, other_entities);
846 if (other_entities.empty())
850 EntityType ent_type = moab.type_from_handle(entity);
852 bool is_in_meshset = moab.contains_entities(other_set, &entity, 1);
855 add_shared_meshset(other_meshset);
860 auto it = shared_meshsets_map.find(shared_names);
861 if (it == shared_meshsets_map.end()) {
865 it = shared_meshsets_map
867 {shared_names, std::make_tuple(family_id, shared_meshsets)})
871 entityHandle_family_map[entity] = std::get<0>(it->second);
875 for (
const auto &it : shared_meshsets_map) {
877 std::string family_name =
"F_";
878 const auto &[family_id, shared_meshsets] = it.second;
879 family_name += std::to_string(family_id);
881 const auto &shared_meshset_names = it.first;
882 std::string group_name;
883 for (
const auto &name : shared_meshset_names) {
885 std::string meshset_name = name;
886 meshset_name.resize(MED_LNAME_SIZE,
' ');
887 group_name += meshset_name;
891 CHKERR MEDfamilyCr(fid, mesh_name.c_str(), family_name.c_str(), family_id,
892 shared_meshset_names.size(), group_name.c_str());
895 <<
"Creating family " << family_name <<
" with id " << family_id
896 <<
" and " << shared_meshset_names.size() <<
" groups " << std::endl;
901 verts = write_range_ptr->subset_by_type(MBVERTEX);
904 std::vector<med_float> coord_med(3 * verts.size());
905 std::vector<med_int> fam;
906 std::vector<med_int> tags;
907 std::map<EntityHandle, med_int> entityHandle_tag_map;
908 med_int med_node_num = 1;
911 for (Range::iterator it = verts.begin(); it != verts.end(); ++it) {
913 moab.get_coords(&(*it), 1, coords);
914 coord_med[3 * (it - verts.begin())] = coords[0];
915 coord_med[3 * (it - verts.begin()) + 1] = coords[1];
916 coord_med[3 * (it - verts.begin()) + 2] = coords[2];
917 fam.push_back(entityHandle_family_map[*it]);
920 entityHandle_tag_map[*it] = med_node_num;
924 CHKERR MEDmeshNodeWr(fid, mesh_name.c_str(), MED_NO_DT, MED_NO_IT,
925 MED_UNDEF_DT, MED_FULL_INTERLACE, verts.size(),
926 &coord_med[0], MED_FALSE,
"", MED_TRUE, &tags[0],
930 double last_tag = tags.back();
932 for (EntityType ent_type = MBVERTEX; ent_type < MBMAXTYPE; ent_type++) {
935 moab.get_entities_by_type(0, ent_type, entities,
true);
937 ents_to_write = intersect(entities, *write_range_ptr);
939 if (ents_to_write.empty())
943 std::vector<med_int> tag_number;
944 std::vector<med_int> family_number;
945 std::vector<med_int> connectivity;
948 for (
auto &entity : ents_to_write) {
949 if (ent_type != MBVERTEX) {
952 family_number.push_back(entityHandle_family_map[entity]);
954 tag_number.push_back(last_tag);
956 std::vector<EntityHandle> conn;
957 moab.get_connectivity(&entity, 1, conn);
959 for (
auto &
c : conn) {
960 connectivity.push_back(entityHandle_tag_map[
c]);
967 auto get_med_element_type = [](EntityType ent_type) {
968 med_geometrie_element
type;
995 med_geometrie_element med_type = get_med_element_type(ent_type);
996 if (ent_type == MBENTITYSET) {
999 CHKERR MEDmeshElementWr(fid, mesh_name.c_str(), MED_NO_DT, MED_NO_IT, 0.,
1000 MED_CELL, med_type, MED_NODAL, MED_FULL_INTERLACE,
1001 family_number.size(), &connectivity[0], MED_FALSE,
1002 nullptr, MED_TRUE, &tag_number[0], MED_TRUE,
1005 MOFEM_LOG_C(
"MEDWORLD", Sev::inform,
"Writing %i elements of type %i (%s) ",
1006 family_number.size(), med_type,
1007 moab::CN::EntityTypeName(ent_type));