v0.16.3
Loading...
Searching...
No Matches
Classes | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
MoFEM::MedInterface Struct Reference

Interface for load MED files. More...

#include "src/interfaces/MedInterface.hpp"

Inheritance diagram for MoFEM::MedInterface:
[legend]
Collaboration diagram for MoFEM::MedInterface:
[legend]

Classes

struct  FieldData
 

Public Member Functions

MoFEMErrorCode query_interface (boost::typeindex::type_index type_index, UnknownInterface **iface) const
 
 MedInterface (const MoFEM::Core &core)
 
MoFEMErrorCode getFileNameFromCommandLine (int verb=1)
 Get MED file name from command line.
 
PetscBool getFlgFile () const
 Check if file name is given in command line.
 
MoFEMErrorCode medGetFieldNames (const string &file, int verb=1)
 Get field names in MED file.
 
MoFEMErrorCode medGetFieldNames (int verb=1)
 get field names in MED file
 
MoFEMErrorCode readMed (const string &file, int verb=1)
 read MED file
 
MoFEMErrorCode readMed (int verb=1)
 read MED file
 
MoFEMErrorCode writeMed (boost::shared_ptr< Range > range_ptr=nullptr, int verb=1)
 write MED file
 
MoFEMErrorCode getMeshsets (boost::shared_ptr< std::vector< const CubitMeshSets * > > &meshsets_ptr)
 
MoFEMErrorCode writeMed (const string &file, boost::shared_ptr< std::vector< const CubitMeshSets * > > meshsets_ptr, boost::shared_ptr< Range > range_ptr=nullptr, int verb=1)
 write MED file
 
MoFEMErrorCode readFields (const std::string &file_name, const std::string &field_name, const bool load_series=false, const int only_step=-1, int verb=1)
 
- Public Member Functions inherited from MoFEM::UnknownInterface
template<class IFACE >
MoFEMErrorCode registerInterface (bool error_if_registration_failed=true)
 Register interface.
 
template<class IFACE >
MoFEMErrorCode getInterface (IFACE *&iface) const
 Get interface reference to pointer of interface.
 
template<class IFACE >
MoFEMErrorCode getInterface (IFACE **const iface) const
 Get interface pointer to pointer of interface.
 
template<class IFACE , typename boost::enable_if< boost::is_pointer< IFACE >, int >::type = 0>
IFACE getInterface () const
 Get interface pointer to pointer of interface.
 
template<class IFACE , typename boost::enable_if< boost::is_reference< IFACE >, int >::type = 0>
IFACE getInterface () const
 Get reference to interface.
 
template<class IFACE >
IFACE * getInterface () const
 Function returning pointer to interface.
 
virtual ~UnknownInterface ()=default
 

Public Attributes

std::vector< std::string > meshNames
 list of meshes in MED file
 
std::vector< EntityHandle > meshMeshsets
 meshset for each mesh
 
std::map< std::string, FieldData > fieldNames
 
std::string medFileName
 MED file name.
 

Private Member Functions

MoFEMErrorCode readMesh (const string &file, const int index, std::map< int, Range > &family_elem_map, int verb=1)
 read mesh from MED file
 
MoFEMErrorCode readFamily (const string &file, const int index, const std::map< int, Range > &family_elem_mapint, std::map< string, Range > &group_elem_map, int verb=1)
 read family and groups
 
MoFEMErrorCode makeBlockSets (const std::map< string, Range > &group_elem_map, int verb=1)
 make from groups meshsets
 

Private Attributes

MoFEM::Core & cOre
 core database
 
PetscBool flgFile
 true if file name given in command line
 
CubitMeshSet_multiIndex cubitMeshsets
 cubit meshsets
 

Additional Inherited Members

- Static Public Member Functions inherited from MoFEM::UnknownInterface
static MoFEMErrorCode getLibVersion (Version &version)
 Get library version.
 
static MoFEMErrorCode getFileVersion (moab::Interface &moab, Version &version)
 Get database major version.
 
static MoFEMErrorCode setFileVersion (moab::Interface &moab, Version version=Version(MoFEM_VERSION_MAJOR, MoFEM_VERSION_MINOR, MoFEM_VERSION_BUILD))
 Get database major version.
 
static MoFEMErrorCode getInterfaceVersion (Version &version)
 Get database major version.
 

Detailed Description

Interface for load MED files.

Definition at line 23 of file MedInterface.hpp.

Constructor & Destructor Documentation

◆ MedInterface()

MoFEM::MedInterface::MedInterface ( const MoFEM::Core &  core)

Definition at line 53 of file MedInterface.cpp.

54 : cOre(const_cast<Core &>(core)), flgFile(PETSC_FALSE) {
55
56 if (!LogManager::checkIfChannelExist("MEDWORLD")) {
57 auto core_log = logging::core::get();
58 core_log->add_sink(
60 LogManager::setLog("MEDWORLD");
61 MOFEM_LOG_TAG("MEDWORLD", "MED");
62 }
63
64 MOFEM_LOG("MEDWORLD", Sev::noisy) << "Mashset manager created";
65}
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
CoreTmp< 0 > Core
Definition Core.hpp:1161
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
static bool checkIfChannelExist(const std::string channel)
Check if channel exist.
MoFEM::Core & cOre
core database
PetscBool flgFile
true if file name given in command line

Member Function Documentation

◆ getFileNameFromCommandLine()

MoFEMErrorCode MoFEM::MedInterface::getFileNameFromCommandLine ( int  verb = 1)

Get MED file name from command line.

Parameters
verbverbosity level
Returns
error code

Definition at line 67 of file MedInterface.cpp.

67 {
68 Interface &m_field = cOre;
69 char mesh_file_name[255];
71 PetscOptionsBegin(m_field.get_comm(), "", "MED Interface", "none");
72 ierr = PetscOptionsString("-med_file", "med file name", "", "mesh.med",
73 mesh_file_name, 255, &flgFile);
75 PetscOptionsEnd();
76 if (flgFile == PETSC_TRUE) {
77 medFileName = std::string(mesh_file_name);
78 } else {
79 medFileName = std::string("mesh.med");
80 }
82}
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERRG(n)
Check error code of MoFEM/MOAB/PETSc function.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
DeprecatedCoreInterface Interface
std::string medFileName
MED file name.

◆ getFlgFile()

PetscBool MoFEM::MedInterface::getFlgFile ( ) const
inline

Check if file name is given in command line.

Definition at line 39 of file MedInterface.hpp.

39{ return flgFile; }

◆ getMeshsets()

MoFEMErrorCode MoFEM::MedInterface::getMeshsets ( boost::shared_ptr< std::vector< const CubitMeshSets * > > &  meshsets_ptr)

Retrieves the meshsets

Parameters
meshsets_ptrA pointer to a vector of const CubitMeshSets objects. If provided, the retrieved meshsets will be stored in this vector.
Returns
A MoFEMErrorCode indicating the success or failure of the operation.

Definition at line 801 of file MedInterface.cpp.

802 {
803 Interface &m_field = cOre;
805
806 auto &meshsets_idx =
807 m_field.getInterface<MeshsetsManager>()->getMeshsetsMultindex();
808 std::vector<const CubitMeshSets *> meshsets;
809
810 for (auto &m : meshsets_idx) {
811 meshsets.push_back(&m);
812 }
813 // Sort meshsets based on meshsetId
814 std::sort(meshsets.begin(), meshsets.end(),
815 [](const CubitMeshSets *a, const CubitMeshSets *b) {
816 return a->getMeshsetId() < b->getMeshsetId();
817 });
818
819 meshsets_ptr =
820 boost::make_shared<std::vector<const CubitMeshSets *>>(meshsets);
822}
constexpr double a
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
FTensor::Index< 'm', 3 > m
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

◆ makeBlockSets()

MoFEMErrorCode MoFEM::MedInterface::makeBlockSets ( const std::map< string, Range > &  group_elem_map,
int  verb = 1 
)
private

make from groups meshsets

Parameters
group_elem_mapmap of groups and elements
verbverbosity level
Returns
error code

Definition at line 756 of file MedInterface.cpp.

757 {
758
759 Interface &m_field = cOre;
761 MeshsetsManager *meshsets_manager_ptr;
762 CHKERR m_field.getInterface(meshsets_manager_ptr);
763
764 int max_id = 0;
766 max_id = (max_id < cit->getMeshsetId()) ? cit->getMeshsetId() : max_id;
767 }
768 max_id++;
769
770 for (std::map<string, Range>::const_iterator git = group_elem_map.begin();
771 git != group_elem_map.end(); git++) {
772 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, max_id, git->first);
773 CubitMeshSet_multiIndex::index<
774 Composite_Cubit_msId_And_MeshsetType_mi_tag>::type::iterator cit;
775 cit =
776 meshsets_manager_ptr->getMeshsetsMultindex()
777 .get<Composite_Cubit_msId_And_MeshsetType_mi_tag>()
778 .find(boost::make_tuple(max_id, CubitBCType(BLOCKSET).to_ulong()));
779 EntityHandle meshsets = cit->getMeshset();
780 if (!git->second.empty()) {
781 CHKERR m_field.get_moab().add_entities(meshsets, git->second);
782 }
783 max_id++;
784 }
785
787 MOFEM_LOG("MEDWORLD", Sev::verbose) << *cit;
788
790}
@ BLOCKSET
#define CHKERR
Inline error check.
#define _IT_CUBITMESHSETS_BY_SET_TYPE_FOR_LOOP_(MESHSET_MANAGER, CUBITBCTYPE, IT)
Iterator that loops over a specific Cubit MeshSet having a particular BC meshset in a moFEM field.
std::bitset< 32 > CubitBCType
Definition Types.hpp:52

◆ medGetFieldNames() [1/2]

MoFEMErrorCode MoFEM::MedInterface::medGetFieldNames ( const string &  file,
int  verb = 1 
)

Get field names in MED file.

Parameters
filefile name
verbverbosity level
Returns
error code

Definition at line 84 of file MedInterface.cpp.

84 {
85 Interface &m_field = cOre;
87 med_idt fid = MEDfileOpen(file.c_str(), MED_ACC_RDONLY);
88 if (fid < 0) {
89 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
90 "Unable to open file '%s'", file.c_str());
91 }
92 med_int num_fields = MEDnField(fid);
93 for (int index = 0; index < num_fields; index++) {
94
95 med_int num_comp = MEDfieldnComponent(fid, index + 1);
96 if (num_comp <= 0) {
97 SETERRQ(m_field.get_comm(), MOFEM_IMPOSSIBLE_CASE,
98 "Could not get number of components for MED field");
99 }
100
101 char name[MED_NAME_SIZE + 1], mesh_name[MED_NAME_SIZE + 1];
102 char dt_unit[MED_SNAME_SIZE + 1];
103 std::vector<char> comp_name(num_comp * MED_SNAME_SIZE + 1);
104 std::vector<char> comp_unit(num_comp * MED_SNAME_SIZE + 1);
105 med_int num_steps = 0;
106 med_type_champ type;
107 med_bool local_mesh;
108 if (MEDfieldInfo(fid, index + 1, name, mesh_name, &local_mesh, &type,
109 &comp_name[0], &comp_unit[0], dt_unit, &num_steps) < 0) {
110 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
111 "Could not get MED field info");
112 }
113
114 std::string field_name = std::string(name);
116 fieldNames[field_name].fieldName = field_name;
117 fieldNames[field_name].meshName = std::string(mesh_name);
118 fieldNames[field_name].localMesh = (local_mesh == MED_TRUE);
119 for (int ff = 0; ff != num_comp; ff++) {
120 fieldNames[field_name].componentNames.push_back(
121 std::string(&comp_name[ff * MED_SNAME_SIZE], MED_SNAME_SIZE));
122 fieldNames[field_name].componentUnits.push_back(
123 std::string(&comp_unit[ff * MED_SNAME_SIZE], MED_SNAME_SIZE));
124 }
125 fieldNames[field_name].dtUnit = std::string(&dt_unit[0]);
126 fieldNames[field_name].ncSteps = num_steps;
127
128 if (verb > 0)
129 MOFEM_LOG("MEDWORLD", Sev::inform) << fieldNames[name];
130 }
131 if (MEDfileClose(fid) < 0) {
132 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
133 "Unable to close file '%s'", file.c_str());
134 }
136}
std::map< EntityHandle, int > index
std::string type
std::string name
@ MOFEM_IMPOSSIBLE_CASE
Definition definitions.h:35
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
double FieldData
Field data type.
Definition Types.hpp:25
constexpr auto field_name
std::map< std::string, FieldData > fieldNames

◆ medGetFieldNames() [2/2]

MoFEMErrorCode MoFEM::MedInterface::medGetFieldNames ( int  verb = 1)

get field names in MED file

File name is get form command line

Parameters
verbverbosity level
Returns
error code

Definition at line 138 of file MedInterface.cpp.

138 {
140 if (medFileName.empty()) {
142 }
145}
MoFEMErrorCode getFileNameFromCommandLine(int verb=1)
Get MED file name from command line.
MoFEMErrorCode medGetFieldNames(const string &file, int verb=1)
Get field names in MED file.

◆ query_interface()

MoFEMErrorCode MoFEM::MedInterface::query_interface ( boost::typeindex::type_index  type_index,
UnknownInterface **  iface 
) const
virtual

Implements MoFEM::UnknownInterface.

Definition at line 47 of file MedInterface.cpp.

48 {
49 *iface = const_cast<MedInterface *>(this);
50 return 0;
51}
MedInterface(const MoFEM::Core &core)

◆ readFamily()

MoFEMErrorCode MoFEM::MedInterface::readFamily ( const string &  file,
const int  index,
const std::map< int, Range > &  family_elem_mapint,
std::map< string, Range > &  group_elem_map,
int  verb = 1 
)
private

read family and groups

Parameters
filefile name
indexmesh index
family_elem_mapintmap of families and elements
group_elem_mapmap of groups and elements
verbverbosity level
Returns
error code

Definition at line 664 of file MedInterface.cpp.

666 {
667 Interface &m_field = cOre;
669
670 med_idt fid = MEDfileOpen(file.c_str(), MED_ACC_RDONLY);
671 if (fid < 0) {
672 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
673 "Unable to open file '%s'", file.c_str());
674 }
675 med_int v[3], vf[3];
676 MEDlibraryNumVersion(&v[0], &v[1], &v[2]);
677 MEDfileNumVersionRd(fid, &vf[0], &vf[1], &vf[2]);
678
679 if (verb > 1) {
680 MOFEM_LOG_C("MEDWORLD", Sev::noisy,
681 "Reading MED file V%d.%d.%d using MED library V%d.%d.%d", vf[0],
682 vf[1], vf[2], v[0], v[1], v[2]);
683 }
684 if (vf[0] < 2 || (vf[0] == 2 && vf[1] < 2)) {
685 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
686 "Cannot read MED file older than V2.2");
687 }
688
689 // clear groups
690 group_elem_map.clear();
691
692 med_int num_families = MEDnFamily(fid, meshNames[index].c_str());
693 if (num_families < 0) {
694 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
695 "Could not read MED families");
696 }
697 for (int i = 0; i < num_families; i++) {
698 med_int num_attrib =
699 (vf[0] == 2)
700 ? MEDnFamily23Attribute(fid, meshNames[index].c_str(), i + 1)
701 : 0;
702 med_int num_groups = MEDnFamilyGroup(fid, meshNames[index].c_str(), i + 1);
703 if (num_attrib < 0 || num_groups < 0) {
704 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
705 "Could not read MED groups or attributes");
706 }
707
708 std::vector<med_int> attribId(num_attrib + 1);
709 std::vector<med_int> attrib_val(num_attrib + 1);
710 std::vector<char> attrib_des(MED_COMMENT_SIZE * num_attrib + 1);
711 std::vector<char> group_names(MED_LNAME_SIZE * num_groups + 1);
712 char family_name[MED_NAME_SIZE + 1];
713 med_int family_num;
714
715 if (vf[0] == 2) { // MED2 file
716 if (MEDfamily23Info(fid, meshNames[index].c_str(), i + 1, family_name,
717 &attribId[0], &attrib_val[0], &attrib_des[0],
718 &family_num, &group_names[0]) < 0) {
719 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
720 "Could not read info for MED2 family %d", i + 1);
721 }
722 } else {
723 if (MEDfamilyInfo(fid, meshNames[index].c_str(), i + 1, family_name,
724 &family_num, &group_names[0]) < 0) {
725 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
726 "Could not read info for MED3 family %d", i + 1);
727 }
728 }
729
730 for (int g = 0; g != num_groups; g++) {
731 std::string name =
732 std::string(&group_names[MED_LNAME_SIZE * g], MED_LNAME_SIZE - 1);
733 name.resize(NAME_TAG_SIZE - 1);
734 if (family_elem_map.find(family_num) == family_elem_map.end()) {
736 "MEDWORLD", Sev::warning,
737 "Family %d not read, likely type of element is not added "
738 "to moab database. Currently only triangle, quad, tetrahedral and "
739 "hexahedral elements are read to moab database",
740 family_num);
741 } else {
742 group_elem_map[name].merge(family_elem_map.at(family_num));
743 }
744 }
745 }
746
747 if (MEDfileClose(fid) < 0) {
748 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
749 "Unable to close file '%s'", file.c_str());
750 }
751
753}
#define MOFEM_LOG_C(channel, severity, format,...)
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
FTensor::Index< 'i', SPACE_DIM > i
const double v
phase velocity of light in medium (cm/ns)
std::vector< std::string > meshNames
list of meshes in MED file

◆ readFields()

MoFEMErrorCode MoFEM::MedInterface::readFields ( const std::string &  file_name,
const std::string &  field_name,
const bool  load_series = false,
const int  only_step = -1,
int  verb = 1 
)

Read fields

Parameters
file_namefile name
file_indexmesh index
loadSeriesload field into series
onlyStepread only one step
Returns
error code

Definition at line 1170 of file MedInterface.cpp.

1173 {
1174
1175 Interface &m_field = cOre;
1177 med_idt fid = MEDfileOpen((char *)file_name.c_str(), MED_LECTURE);
1178 if (fid < 0) {
1179 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1180 "Unable to open file '%s'", file_name.c_str());
1181 }
1182
1183 med_int num_comp = MEDfieldnComponentByName(fid, field_name.c_str());
1184 if (num_comp <= 0) {
1185 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1186 "Could not get number of components for MED field");
1187 }
1188
1189 char meshName[MED_NAME_SIZE + 1];
1190 char dtUnit[MED_SNAME_SIZE + 1];
1191 std::vector<char> compName(num_comp * MED_SNAME_SIZE + 1);
1192 std::vector<char> compUnit(num_comp * MED_SNAME_SIZE + 1);
1193 med_int numSteps = 0;
1194 med_type_champ type;
1195 med_bool localMesh;
1196 if (MEDfieldInfoByName(fid, field_name.c_str(), meshName, &localMesh, &type,
1197 &compName[0], &compUnit[0], dtUnit, &numSteps) < 0) {
1198 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1199 "Could not get MED field info");
1200 }
1201
1202 // Get meshset
1203 MeshsetsManager *meshsets_manager_ptr;
1204 CHKERR m_field.getInterface(meshsets_manager_ptr);
1205 const CubitMeshSets *cubit_meshset_ptr;
1206 CHKERR meshsets_manager_ptr->getCubitMeshsetPtr(meshName, &cubit_meshset_ptr);
1207 EntityHandle meshset = cubit_meshset_ptr->getMeshset();
1208
1209 // Paraview can only plot field which have 1, 3, or 9 components. If field has
1210 // more that 9 comonents, it is stored on MOAB mesh but not viable in
1211 // ParaView.
1212 int num_comp_msh = (num_comp <= 1) ? 1
1213 : (num_comp <= 3) ? 3
1214 : (num_comp <= 9) ? 9
1215 : num_comp;
1216
1217 // Create tag to store nodal or cell values read form med file. Note that tag
1218 // has prefix MED to avoid collision with other tags.
1219 Tag th;
1220 std::string tag_name = "MED_" + field_name;
1221 {
1222 std::vector<double> def_val(num_comp_msh, 0);
1223 CHKERR m_field.get_moab().tag_get_handle(
1224 tag_name.c_str(), num_comp_msh, MB_TYPE_DOUBLE, th,
1225 MB_TAG_CREAT | MB_TAG_DENSE, &def_val[0]);
1226 }
1227
1228 // Warning! The ordering of the elements in the last two lists is
1229 // important: it should match the ordering of the MSH element types
1230 // (when elements are saved without tags, this governs the order
1231 // with which we implicitly index them in GModel::readMED)
1232 const med_entity_type entType[] = {MED_NODE, MED_CELL, MED_NODE_ELEMENT};
1233 const med_geometrie_element eleType[] = {
1234 MED_NONE, MED_SEG2, MED_TRIA3, MED_QUAD4, MED_TETRA4, MED_HEXA8,
1235 MED_PENTA6, MED_PYRA5, MED_SEG3, MED_TRIA6, MED_QUAD9, MED_TETRA10,
1236 MED_HEXA27, MED_POINT1, MED_QUAD8, MED_HEXA20, MED_PENTA15, MED_PYRA13};
1237 // const int nodesPerEle[] = {0, 2, 3, 4, 4, 8, 6, 5, 3, 6, 9, 10, 27, 1, 8,
1238 // 20, 15, 13};
1239
1240 std::vector<std::pair<int, int>> pairs;
1241 for (unsigned int i = 0; i < sizeof(entType) / sizeof(entType[0]); i++) {
1242 for (unsigned int j = 0; j < sizeof(eleType) / sizeof(eleType[0]); j++) {
1243 if ((!i && !j) || j) {
1244 med_int n = numSteps;
1245 if (n > 0) {
1246 pairs.push_back(std::pair<int, int>(i, j));
1247 numSteps = std::max(numSteps, n);
1248 }
1249 if (!i && !j)
1250 break; // MED_NOEUD does not care about eleType
1251 }
1252 }
1253 }
1254
1255 if (numSteps < 1 || pairs.empty()) {
1256 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1257 "Nothing to import from MED file");
1258 }
1259
1260 if (load_series) {
1261 SETERRQ(m_field.get_comm(), MOFEM_NOT_IMPLEMENTED, "Not yet implemented");
1262 }
1263
1264 if (numSteps == 1 && only_step > 0) {
1265 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
1266 "Field has only one step. Cannot load step with -med_time_step, "
1267 "remove from command line.");
1268 } else if (numSteps == only_step) {
1269 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
1270 "Cannot load final step %d. -med_time_step should be %d.",
1271 only_step, only_step - 1);
1272 }
1273
1274 for (int step = (only_step == -1) ? 0 : only_step; step < numSteps; step++) {
1275 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Loading step %d of field %s", step + 1,
1276 field_name.c_str());
1277 if (!load_series && only_step != step)
1278 continue;
1279
1280 // FIXME: in MED3 we might want to loop over all profiles instead
1281 // of relying of the default one
1282
1283 // FIXME: MED3 allows to store multi-step meshes; we should
1284
1285 for (unsigned int pair = 0; pair < pairs.size(); pair++) {
1286
1287 // get step info
1288 med_entite_maillage ent = entType[pairs[pair].first];
1289 med_geometrie_element ele = eleType[pairs[pair].second];
1290 med_int numdt, numit, ngauss;
1291 med_float dt;
1292 if (MEDfieldComputingStepInfo(fid, field_name.c_str(), step + 1, &numdt,
1293 &numit, &dt) < 0) {
1294 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
1295 "Could not read step info");
1296 }
1297
1298 char locName[MED_NAME_SIZE + 1], profileName[MED_NAME_SIZE + 1];
1299
1300 // get number of values in the field (numVal takes the number of
1301 // Gauss points or the number of nodes per element into account,
1302 // but not the number of components)
1303 med_int profileSize;
1304 med_int numVal = MEDfieldnValueWithProfile(
1305 fid, field_name.c_str(), numdt, numit, ent, ele, 1,
1306 MED_COMPACT_STMODE, profileName, &profileSize, locName, &ngauss);
1307 numVal *= ngauss;
1308
1309 if (numVal <= 0)
1310 continue;
1311
1312 // read field data
1313 std::vector<double> val(numVal * num_comp);
1314 if (MEDfieldValueWithProfileRd(fid, field_name.c_str(), numdt, numit, ent,
1315 ele, MED_COMPACT_STMODE, profileName,
1316 MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
1317 (unsigned char *)&val[0]) < 0) {
1318 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
1319 "Could not read field values");
1320 }
1321
1322 switch (ent) {
1323 case MED_CELL: {
1324 EntityType ent_type = MBMAXTYPE;
1325 switch (ele) {
1326 case MED_TETRA4:
1327 case MED_TETRA10:
1328 ent_type = MBTET;
1329 break;
1330 case MED_HEXA8:
1331 case MED_HEXA20:
1332 ent_type = MBHEX;
1333 break;
1334 default:
1335 MOFEM_LOG_C("MEDWORLD", Sev::warning,
1336 "Not yet implemented for this cell %d", ele);
1337 }
1338 if (ent_type != MBMAXTYPE) {
1339 if (ngauss == 1) {
1340 Range ents;
1341 CHKERR m_field.get_moab().get_entities_by_type(meshset, ent_type,
1342 ents, true);
1343 double e_vals[num_comp_msh];
1344 bzero(e_vals, sizeof(double) * num_comp_msh);
1345 std::vector<double>::iterator vit = val.begin();
1346 for (Range::iterator eit = ents.begin(); eit != ents.end(); eit++) {
1347 for (int ii = 0; ii != num_comp; ii++, vit++) {
1348 e_vals[ii] = *vit;
1349 }
1350 CHKERR m_field.get_moab().tag_set_data(th, &*eit, 1, e_vals);
1351 }
1352 } else {
1353 Range ents;
1354 CHKERR m_field.get_moab().get_entities_by_type(meshset, ent_type,
1355 ents, true);
1356 if (ents.size() * ngauss * num_comp != val.size()) {
1357 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
1358 "data inconsistency");
1359 }
1360 // FIXME simply average gauss values, far from perfect need fix
1361 double e_vals[num_comp_msh];
1362 std::vector<double>::iterator vit = val.begin();
1363 for (Range::iterator eit = ents.begin(); eit != ents.end(); eit++) {
1364 bzero(e_vals, sizeof(double) * num_comp_msh);
1365 for (int gg = 0; gg != ngauss; gg++) {
1366 for (int ii = 0; ii != num_comp; ii++, vit++) {
1367 e_vals[ii] += *vit / ngauss;
1368 }
1369 }
1370 CHKERR m_field.get_moab().tag_set_data(th, &*eit, 1, e_vals);
1371 }
1372 }
1373 }
1374 } break;
1375 case MED_NODE:
1376 case MED_NODE_ELEMENT: {
1377 EntityType ent_type = MBVERTEX;
1378 Range ents;
1379 CHKERR m_field.get_moab().get_entities_by_type(meshset, ent_type, ents,
1380 true);
1381 double e_vals[num_comp_msh];
1382 bzero(e_vals, sizeof(double) * num_comp_msh);
1383 std::vector<double>::iterator vit = val.begin();
1384 for (Range::iterator eit = ents.begin(); eit != ents.end(); eit++) {
1385 for (int ii = 0; ii != num_comp; ii++, vit++) {
1386 e_vals[ii] = *vit;
1387 }
1388 CHKERR m_field.get_moab().tag_set_data(th, &*eit, 1, e_vals);
1389 }
1390 } break;
1391 default:
1392 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Entity type %d not implemented",
1393 ent);
1394 }
1395 }
1396 }
1397
1399}
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
double dt
FTensor::Index< 'j', 3 > j

◆ readMed() [1/2]

MoFEMErrorCode MoFEM::MedInterface::readMed ( const string &  file,
int  verb = 1 
)

read MED file

Vertex GLOBAL_ID is the MED node number: the number stored in the file, or the node's position 1..N when the file stores none. A mesh read into a database that already has vertices, such as the second mesh of a file, is offset past the largest vertex ID, so IDs stay unique.

Elements use stored MED cell numbers, or generated consecutive numbers when absent. IDs are positive, below INT_MAX and unique within each dimension. A batch whose numbers collide with existing elements is offset past the largest existing ID; non-colliding MED numbers are kept.

Parameters
filefield name
verbverbosity level
Returns
error code

Definition at line 147 of file MedInterface.cpp.

147 {
148 Interface &m_field = cOre;
150
151 med_idt fid = MEDfileOpen(file.c_str(), MED_ACC_RDONLY);
152 if (fid < 0) {
153 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
154 "Unable to open file '%s'", file.c_str());
155 }
156
157 med_int v[3], vf[3];
158 MEDlibraryNumVersion(&v[0], &v[1], &v[2]);
159 MEDfileNumVersionRd(fid, &vf[0], &vf[1], &vf[2]);
160
161 if (verb > 0)
162 MOFEM_LOG_C("MEDWORLD", Sev::inform,
163 "Reading MED file V%d.%d.%d using MED library V%d.%d.%d", vf[0],
164 vf[1], vf[2], v[0], v[1], v[2]);
165
166 if (vf[0] < 2 || (vf[0] == 2 && vf[1] < 2)) {
167 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
168 "Cannot read MED file older than V2.2");
169 }
170
171 for (int i = 0; i < MEDnMesh(fid); i++) {
172 char mesh_name[MED_NAME_SIZE + 1], mesh_desc[MED_COMMENT_SIZE + 1];
173 bzero(mesh_name, MED_NAME_SIZE);
174 bzero(mesh_desc, MED_COMMENT_SIZE);
175 med_int space_dim;
176 med_mesh_type mesh_type;
177 med_int mesh_dim, n_step;
178 char dt_unit[MED_SNAME_SIZE + 1];
179 char axis_name[3 * MED_SNAME_SIZE + 1], axis_unit[3 * MED_SNAME_SIZE + 1];
180 med_sorting_type sorting_type;
181 med_axis_type axis_type;
182 if (MEDmeshInfo(fid, i + 1, mesh_name, &space_dim, &mesh_dim, &mesh_type,
183 mesh_desc, dt_unit, &sorting_type, &n_step, &axis_type,
184 axis_name, axis_unit) < 0) {
185 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
186 "Unable to read mesh information");
187 }
188 meshNames.push_back(std::string(mesh_name));
189 if (verb > 0) {
190 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Check mesh %s nsteps %d", mesh_name,
191 n_step);
192 }
193 }
194
195 std::map<int, Range> family_elem_map;
196 std::map<string, Range> group_elem_map;
197
198 for (unsigned int ii = 0; ii != meshNames.size(); ii++) {
199 CHKERR readMesh(file, ii, family_elem_map, verb);
200 CHKERR readFamily(file, ii, family_elem_map, group_elem_map, verb);
201 CHKERR makeBlockSets(group_elem_map, verb);
202 }
203
204 if (MEDfileClose(fid) < 0) {
205 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
206 "Unable to close file '%s'", file.c_str());
207 }
208
210}
MoFEMErrorCode readFamily(const string &file, const int index, const std::map< int, Range > &family_elem_mapint, std::map< string, Range > &group_elem_map, int verb=1)
read family and groups
MoFEMErrorCode makeBlockSets(const std::map< string, Range > &group_elem_map, int verb=1)
make from groups meshsets
MoFEMErrorCode readMesh(const string &file, const int index, std::map< int, Range > &family_elem_map, int verb=1)
read mesh from MED file

◆ readMed() [2/2]

MoFEMErrorCode MoFEM::MedInterface::readMed ( int  verb = 1)

read MED file

File name is form command line ////

Parameters
verbverbosity level
Returns
error code

Definition at line 792 of file MedInterface.cpp.

792 {
794 if (medFileName.empty()) {
796 }
799}
MoFEMErrorCode readMed(const string &file, int verb=1)
read MED file

◆ readMesh()

MoFEMErrorCode MoFEM::MedInterface::readMesh ( const string &  file,
const int  index,
std::map< int, Range > &  family_elem_map,
int  verb = 1 
)
private

read mesh from MED file

Parameters
filefile name
indexindex of mesh
family_elem_mapmap of families and elements
verbverbosity level
Returns
error code

Definition at line 392 of file MedInterface.cpp.

394 {
395
396 Interface &m_field = cOre;
398
399 med_idt fid = MEDfileOpen(file.c_str(), MED_ACC_RDONLY);
400 if (fid < 0) {
401 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
402 "Unable to open file '%s'", file.c_str());
403 }
404 med_int v[3], vf[3];
405 MEDlibraryNumVersion(&v[0], &v[1], &v[2]);
406 MEDfileNumVersionRd(fid, &vf[0], &vf[1], &vf[2]);
407 if (verb > 1)
408 MOFEM_LOG_C("MEDWORLD", Sev::noisy,
409 "Reading MED file V%d.%d.%d using MED library V%d.%d.%d", vf[0],
410 vf[1], vf[2], v[0], v[1], v[2]);
411
412 if (vf[0] < 2 || (vf[0] == 2 && vf[1] < 2)) {
413 SETERRQ(m_field.get_comm(), MOFEM_DATA_INCONSISTENCY,
414 "Cannot read MED file older than V2.2");
415 }
416
417 char mesh_name[MED_NAME_SIZE + 1], mesh_desc[MED_COMMENT_SIZE + 1];
418 bzero(mesh_name, MED_NAME_SIZE + 1);
419 bzero(mesh_desc, MED_COMMENT_SIZE + 1);
420 med_int space_dim;
421 med_mesh_type mesh_type;
422 med_int mesh_dim, n_step;
423 char dt_unit[MED_SNAME_SIZE + 1];
424 char axis_name[3 * MED_SNAME_SIZE + 1], axis_unit[3 * MED_SNAME_SIZE + 1];
425 med_sorting_type sorting_type;
426 med_axis_type axis_type;
427 if (MEDmeshInfo(fid, index + 1, mesh_name, &space_dim, &mesh_dim, &mesh_type,
428 mesh_desc, dt_unit, &sorting_type, &n_step, &axis_type,
429 axis_name, axis_unit) < 0) {
430 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
431 "Unable to read mesh information");
432 }
433 if (verb > 0)
434 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Reading mesh %s nsteps %d", mesh_name,
435 n_step);
436
437 switch (axis_type) {
438 case MED_CARTESIAN:
439 break;
440 case MED_SPHERICAL:
441 case MED_CYLINDRICAL:
442 default:
443 SETERRQ(m_field.get_comm(), MOFEM_NOT_IMPLEMENTED,
444 "Curvilinear coordinate system implemented");
445 }
446 if (space_dim < 2) {
447 SETERRQ(m_field.get_comm(), MOFEM_NOT_IMPLEMENTED,
448 "Not implemented for space dim %d", space_dim);
449 }
450
451 EntityHandle mesh_meshset;
452 {
453 MeshsetsManager *meshsets_manager_ptr;
454 CHKERR m_field.getInterface(meshsets_manager_ptr);
455 int max_id = 0;
457 max_id = (max_id < cit->getMeshsetId()) ? cit->getMeshsetId() : max_id;
458 }
459 max_id++;
460 CHKERR meshsets_manager_ptr->addMeshset(BLOCKSET, max_id,
461 std::string(mesh_name));
462 CubitMeshSet_multiIndex::index<
463 Composite_Cubit_msId_And_MeshsetType_mi_tag>::type::iterator cit;
464 cit =
465 meshsets_manager_ptr->getMeshsetsMultindex()
466 .get<Composite_Cubit_msId_And_MeshsetType_mi_tag>()
467 .find(boost::make_tuple(max_id, CubitBCType(BLOCKSET).to_ulong()));
468 max_id++;
469 mesh_meshset = cit->getMeshset();
470 meshMeshsets.push_back(mesh_meshset);
471 }
472
473 med_bool change_of_coord, geo_transform;
474 med_int num_nodes = MEDmeshnEntity(
475 fid, mesh_name, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
476 MED_COORDINATE, MED_NO_CMODE, &change_of_coord, &geo_transform);
477 if (num_nodes < 0) {
478 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
479 "Could not read number of MED nodes");
480 }
481 if (num_nodes == 0) {
482 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
483 "No nodes in MED mesh");
484 }
485 if (verb > 0)
486 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Read number of nodes %d", num_nodes);
487
488 std::vector<med_float> coord_med(space_dim * num_nodes);
489 if (MEDmeshNodeCoordinateRd(fid, mesh_name, MED_NO_DT, MED_NO_IT,
490 MED_NO_INTERLACE, &coord_med[0]) < 0) {
491 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
492 "Could not read MED node coordinates");
493 }
494
495 // Add vertices to moab
496 ReadUtilIface *iface;
497 vector<double *> arrays_coord;
498 EntityHandle startv;
499 CHKERR m_field.get_moab().query_interface(iface);
500 CHKERR iface->get_node_coords(3, num_nodes, 0, startv, arrays_coord);
501 Range verts(startv, startv + num_nodes - 1);
502 std::copy(&coord_med[0 * num_nodes], &coord_med[1 * num_nodes],
503 arrays_coord[0]);
504 std::copy(&coord_med[1 * num_nodes], &coord_med[2 * num_nodes],
505 arrays_coord[1]);
506 if (space_dim == 2) {
507 std::fill(arrays_coord[2], &arrays_coord[2][num_nodes], 0.);
508 } else {
509 std::copy(&coord_med[2 * num_nodes], &coord_med[3 * num_nodes],
510 arrays_coord[2]);
511 }
512 CHKERR m_field.get_moab().add_entities(mesh_meshset, verts);
513 CHKERR setNodeIds(m_field, fid, mesh_name, verts, verb);
514 family_elem_map.clear();
515
516 // get family for vertices
517 {
518 std::vector<med_int> nodes_tags(num_nodes, 0);
519 if (MEDmeshEntityFamilyNumberRd(fid, mesh_name, MED_NO_DT, MED_NO_IT,
520 MED_NODE, MED_NO_GEOTYPE,
521 &nodes_tags[0]) < 0) {
522 nodes_tags.clear();
523 }
524 for (int i = 0; i < num_nodes; i++) {
525 family_elem_map[nodes_tags.empty() ? i : nodes_tags[i]].insert(verts[i]);
526 }
527 }
528
529 // read elements (loop over all possible MSH element types)
530 for (EntityType ent_type = MBVERTEX; ent_type < MBMAXTYPE; ent_type++) {
531
532 auto types = moab2med_element_type(ent_type);
533
534 for (auto type : types) {
535
536 // get number of cells
537 med_bool change_of_coord;
538 med_bool geo_transform;
539 med_int num_ele = MEDmeshnEntity(
540 fid, mesh_name, MED_NO_DT, MED_NO_IT, MED_CELL, type,
541 MED_CONNECTIVITY, MED_NODAL, &change_of_coord, &geo_transform);
542
543 // get connectivity
544 if (num_ele <= 0)
545 continue;
546
547 int num_nod_per_ele = type % 100;
548 if (verb > 0)
549 MOFEM_LOG("MEDWORLD", Sev::inform)
550 << "Reading elements " << num_ele << " of type "
551 << moab::CN::EntityTypeName(ent_type) << " number of nodes "
552 << num_nod_per_ele;
553
554 std::vector<med_int> conn_med(num_ele * num_nod_per_ele);
555 if (MEDmeshElementConnectivityRd(fid, mesh_name, MED_NO_DT, MED_NO_IT,
556 MED_CELL, type, MED_NODAL,
557 MED_FULL_INTERLACE, &conn_med[0]) < 0) {
558 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
559 "Could not read MED elements");
560 }
561
562 Range ents;
563
564 if (ent_type != MBVERTEX) {
565 EntityHandle *conn_moab;
566 EntityHandle starte;
567 CHKERR iface->get_element_connect(num_ele, num_nod_per_ele, ent_type, 0,
568 starte, conn_moab);
569 switch (ent_type) {
570 // FIXME: Some connectivity could not work, need to run and test
571 case MBTET: {
572 int ii = 0;
573 for (int ee = 0; ee != num_ele; ee++) {
574 std::vector<EntityHandle> n(num_nod_per_ele);
575 for (int nn = 0; nn != num_nod_per_ele; nn++) {
576 n[nn] = verts[conn_med[ii + nn] - 1];
577 }
578
579 std::array<int, 10> nodes_map{
580
581 1, 0, 2, 3,
582
583 4, 6, 5, 8,
584 7, 9
585
586 };
587
588 for (int nn = 0; nn != num_nod_per_ele; nn++, ii++) {
589 conn_moab[ii] = n[nodes_map[nn]];
590 }
591 }
592 } break;
593 case MBHEX: {
594 int ii = 0;
595 for (int ee = 0; ee != num_ele; ee++) {
596 std::vector<EntityHandle> n(num_nod_per_ele);
597 for (int nn = 0; nn != num_nod_per_ele; nn++) {
598 n[nn] = verts[conn_med[ii + nn] - 1];
599 }
600
601 std::array<int, 20> nodes_map{
602 0, 3, 2, 1, 4, 7, 6, 5, // corner nodes
603 11, 10, 9, 8, // edges on bottom face
604 16, 19, 18, 17, // vertical edges
605 15, 14, 13, 12 // edges on top face
606
607 };
608
609 for (int nn = 0; nn != num_nod_per_ele; nn++, ii++) {
610 conn_moab[ii] = n[nodes_map[nn]];
611 }
612 }
613 } break;
614 default: {
615 int ii = 0;
616 for (int ee = 0; ee != num_ele; ee++) {
617 for (int nn = 0; nn != num_nod_per_ele; nn++, ii++) {
618 conn_moab[ii] = verts[conn_med[ii] - 1];
619 }
620 }
621 }
622 }
623 CHKERR iface->update_adjacencies(starte, num_ele, num_nod_per_ele,
624 conn_moab);
625 ents = Range(starte, starte + num_ele - 1);
626 CHKERR setElementIds(m_field, fid, mesh_name, type, ents, verb);
627 } else {
628 // This is special case when in med vertices are defined as elements
629 int ii = 0;
630 std::vector<EntityHandle> conn_moab(num_ele * num_nod_per_ele);
631 for (int ee = 0; ee != num_ele; ++ee)
632 for (int nn = 0; nn != num_nod_per_ele; ++nn, ++ii)
633 conn_moab[ii] = verts[conn_med[ii] - 1];
634 ents.insert_list(conn_moab.begin(), conn_moab.end());
635 }
636
637 // Add elements to family meshset
638 CHKERR m_field.get_moab().add_entities(mesh_meshset, ents);
639
640 // get family for cells
641 {
642 std::vector<med_int> fam(num_ele, 0);
643 if (MEDmeshEntityFamilyNumberRd(fid, mesh_name, MED_NO_DT, MED_NO_IT,
644 MED_CELL, type, &fam[0]) < 0) {
645 MOFEM_LOG("MEDWORLD", Sev::warning) <<
646 "No family number for elements: using 0 as default family number";
647 }
648 for (int j = 0; j < num_ele; j++) {
649 family_elem_map[fam[j]].insert(ents[j]);
650 }
651 }
652 }
653 }
654
655 if (MEDfileClose(fid) < 0) {
656 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
657 "Unable to close file '%s'", file.c_str());
658 }
659
661}
static MoFEMErrorCode setNodeIds(MoFEM::Interface &m_field, med_idt fid, const char *mesh_name, const Range &verts, int verb)
static std::vector< med_geometrie_element > moab2med_element_type(const EntityType type)
static MoFEMErrorCode setElementIds(MoFEM::Interface &m_field, med_idt fid, const char *mesh_name, med_geometry_type type, const Range &ents, int verb)
std::vector< EntityHandle > meshMeshsets
meshset for each mesh

◆ writeMed() [1/2]

MoFEMErrorCode MoFEM::MedInterface::writeMed ( boost::shared_ptr< Range >  range_ptr = nullptr,
int  verb = 1 
)

write MED file

File name is form command line

Parameters
verbverbosity level
Returns
error code

Definition at line 824 of file MedInterface.cpp.

825 {
827 if (medFileName.empty()) {
829 }
830
831 // Get all meshsets
832 boost::shared_ptr<std::vector<const CubitMeshSets *>> meshsets_ptr;
833 getMeshsets(meshsets_ptr);
834
835 CHKERR writeMed(medFileName, meshsets_ptr, range_ptr, verb);
837}
MoFEMErrorCode writeMed(boost::shared_ptr< Range > range_ptr=nullptr, int verb=1)
write MED file
MoFEMErrorCode getMeshsets(boost::shared_ptr< std::vector< const CubitMeshSets * > > &meshsets_ptr)

◆ writeMed() [2/2]

MoFEMErrorCode MoFEM::MedInterface::writeMed ( const string &  file,
boost::shared_ptr< std::vector< const CubitMeshSets * > >  meshsets_ptr,
boost::shared_ptr< Range >  range_ptr = nullptr,
int  verb = 1 
)

write MED file

The MED node numbers are the vertices' GLOBAL_ID when every written vertex has a distinct positive one, else their positions 1..N.

Parameters
filefile name
meshsets_ptrmeshsets
verbverbosity level
Returns
error code

Definition at line 839 of file MedInterface.cpp.

842 {
843 Interface &m_field = cOre;
844 // Get the MOAB instance from the field
845 moab::Interface &moab = m_field.get_moab();
846
848 MOFEM_LOG("MEDWORLD", Sev::warning)
849 << "WRITE_MED IS EXPERIMENTAL, MAY CONTAIN BUGS, ALWAYS CHECK THE OUTPUT "
850 "FILE";
851 // Open a med file with the specified version
852 med_idt fid =
853 MEDfileVersionOpen((char *)file.c_str(), MED_ACC_CREAT, MED_MAJOR_NUM,
854 MED_MINOR_NUM, MED_RELEASE_NUM);
855 // Throw an error if the file could not be opened
856 if (fid < 0) {
857 SETERRQ(m_field.get_comm(), MOFEM_OPERATION_UNSUCCESSFUL,
858 "Cannot create MED file");
859 return 0;
860 }
861
862 // Write appropriate header
863 CHKERR MEDfichDesEcr(fid, (char *)"MED file generated by MoFEM");
864
865 // Generate empty mesh
866 char dtUnit[MED_SNAME_SIZE + 1] = ""; // time unit
867 char axisName[3 * MED_SNAME_SIZE + 1] = ""; // axis name
868 char axisUnit[3 * MED_SNAME_SIZE + 1] = ""; // axis unit
869
870 PetscBool is_cubit_meshset = PETSC_TRUE;
871 int med_mesh_name_id = 0;
872
873 // Create the mesh
874 char mesh_name_char[255] = {};
875 std::string mesh_name = "Mesh";
876 PetscBool mesh_name_set = PETSC_FALSE;
877
878 // Get mesh_name from command line
879 PetscOptionsBegin(PETSC_COMM_WORLD, "", "MED mesh options", "");
880 CHKERR PetscOptionsString("-med_mesh_name", "get med mesh name", "",
881 mesh_name.c_str(), mesh_name_char, 255,
882 &mesh_name_set);
883 PetscOptionsEnd();
884
885 if (mesh_name_set)
886 mesh_name = mesh_name_char;
887
888 // check if mesh_name is a meshset
889 for (auto &m : *meshsets_ptr) {
890 if (m->getName() == mesh_name) {
891 med_mesh_name_id = m->getMeshsetId();
892 is_cubit_meshset = PETSC_FALSE;
893 break;
894 }
895 }
896
897 // Get the maximum meshset id
898 int max_id = 0;
899 for (auto &m : *meshsets_ptr) {
900 if (m->getMeshsetId() == med_mesh_name_id)
901 mesh_name = m->getName();
902 max_id = (max_id < m->getMeshsetId()) ? m->getMeshsetId() : max_id;
903 }
904
905 // Create a mesh
906 CHKERR MEDmeshCr(fid, mesh_name.c_str(), 3, 3, MED_UNSTRUCTURED_MESH,
907 "Mesh created", dtUnit, MED_SORT_DTIT, MED_CARTESIAN,
908 axisName, axisUnit);
909
910 // Create a map of meshset names to family ids and shared meshsets
911 med_int family_id = 0;
912 std::map<std::vector<std::string>, std::tuple<med_int, std::vector<int>>>
913 shared_meshsets_map;
914 std::map<EntityHandle, med_int> entityHandle_family_map;
915
916 // initialise zero family id
917 shared_meshsets_map[std::vector<string>()] =
918 std::make_tuple(family_id, std::vector<int>());
919
920 // function to get meshset names
921 auto get_set_name = [&](const CubitMeshSets *iit) {
922 std::string bc_type_name;
923 if (iit->getBcTypeULong() & BLOCKSET) {
924
925 bc_type_name = iit->getName();
926 if (bc_type_name == "NoNameSet") {
927 bc_type_name = "BLOCKSET_";
928 bc_type_name += std::to_string(iit->getMeshsetId());
929 }
930 return bc_type_name;
931 } else if (iit->getBcTypeULong() & SIDESET ||
932 iit->getBcTypeULong() & NODESET) {
933
934 CubitBCType cubitBcType(iit->getBcTypeULong());
935
936 unsigned jj = 0;
937 while (1 << jj != LASTSET_BC) {
938 const CubitBCType jj_bc_type = 1 << jj;
939 if ((iit->getBcType() & jj_bc_type).any()) {
940
941 std::string temp_bc_type_name = string(CubitBCNames[jj + 1]);
942 if (temp_bc_type_name.length() > 10) {
943 temp_bc_type_name = temp_bc_type_name.substr(0, 4);
944 }
945 bc_type_name += temp_bc_type_name;
946 bc_type_name += "_";
947 }
948 ++jj;
949 }
950 bc_type_name += std::to_string(iit->getMeshsetId());
951 } else {
952 bc_type_name = "UnknownSet";
953 }
954 return bc_type_name;
955 };
956
957 // loop over all entities in the write range
958 // FIXME: This is a very slow implementation
959 // We should use a binary search tree
960 // instead of looping each entity in
961 // write_range_ptr to find shared meshsets
962 for (auto &entity : *write_range_ptr) {
963 // check if entity is shared with other meshsets
964 std::vector<int> shared_meshsets;
965 std::vector<string> shared_names;
966
967 // function to add shared meshsets to vectors
968 auto add_shared_meshset = [&](auto &other_meshset) {
969 std::string other_name = get_set_name(other_meshset);
970 if (std::find(shared_names.begin(), shared_names.end(), other_name) ==
971 shared_names.end()) {
972 shared_meshsets.push_back(other_meshset->getMeshsetId());
973 shared_names.push_back(other_name);
974 }
975 };
976
977 // loop over all meshsets provided
978 for (auto &other_meshset : *meshsets_ptr) {
979 // skip if meshset is the global meshset given by user
980 if (med_mesh_name_id == other_meshset->getMeshsetId())
981 continue;
982
983 Range other_entities;
984 EntityHandle other_set = other_meshset->getMeshset();
985 moab.get_entities_by_handle(other_set, other_entities);
986 if (other_entities.empty())
987 continue;
988
989 bool is_in_meshset = moab.contains_entities(other_set, &entity, 1);
990 if (is_in_meshset) {
991 // add shared meshset id to list
992 add_shared_meshset(other_meshset);
993 }
994 }
995
996 // check if shared meshset is already in map
997 auto it = shared_meshsets_map.find(shared_names);
998 if (it == shared_meshsets_map.end()) {
999 // create new family id
1000 family_id++;
1001 // create and insert new tuple into the map
1002 it = shared_meshsets_map
1003 .insert(
1004 {shared_names, std::make_tuple(family_id, shared_meshsets)})
1005 .first;
1006 }
1007 // assign family id to entity
1008 entityHandle_family_map[entity] = std::get<0>(it->second);
1009 }
1010
1011 // loop to create families based on shared meshsets map
1012 for (const auto &it : shared_meshsets_map) {
1013 // create family
1014 std::string family_name = "F_";
1015 const auto &[family_id, shared_meshsets] = it.second;
1016 family_name += std::to_string(family_id);
1017
1018 const auto &shared_meshset_names = it.first;
1019 std::string group_name;
1020 for (const auto &name : shared_meshset_names) {
1021 // get meshset name
1022 std::string meshset_name = name;
1023 meshset_name.resize(MED_LNAME_SIZE, ' ');
1024 group_name += meshset_name;
1025 }
1026
1027 // create family
1028 CHKERR MEDfamilyCr(fid, mesh_name.c_str(), family_name.c_str(), family_id,
1029 shared_meshset_names.size(), group_name.c_str());
1030
1031 MOFEM_LOG("MEDWORLD", Sev::inform)
1032 << "Creating family " << family_name << " with id " << family_id
1033 << " and " << shared_meshset_names.size() << " groups " << std::endl;
1034 }
1035
1036 // write nodes from range
1037 Range verts;
1038 verts = write_range_ptr->subset_by_type(MBVERTEX);
1039
1040 // Node numbers: the vertices' GLOBAL_ID when every one is positive and
1041 // distinct, else the positions 1..N. The connectivity always refers to the
1042 // positions.
1043 std::vector<int> gids(verts.size());
1044 CHKERR moab.tag_get_data(moab.globalId_tag(), verts, gids.data());
1045 bool use_gids =
1046 std::all_of(gids.begin(), gids.end(), [](int id) { return id > 0; });
1047 if (use_gids) {
1048 auto sorted = gids;
1049 std::sort(sorted.begin(), sorted.end());
1050 use_gids = std::adjacent_find(sorted.begin(), sorted.end()) == sorted.end();
1051 }
1052 if (!use_gids)
1053 MOFEM_LOG("MEDWORLD", Sev::warning)
1054 << "Vertices without distinct positive GLOBAL_ID; MED node numbers "
1055 "are their positions";
1056
1057 // Prepare arrays to hold the coordinates
1058 std::vector<med_float> coord_med(3 * verts.size());
1059 std::vector<med_int> fam;
1060 std::vector<med_int> tags;
1061 std::map<EntityHandle, med_int> entityHandle_tag_map;
1062 med_int med_node_num = 1;
1063
1064 // For each vertex, get its coordinates
1065 for (Range::iterator it = verts.begin(); it != verts.end(); ++it) {
1066 double coords[3];
1067 moab.get_coords(&(*it), 1, coords);
1068 coord_med[3 * (it - verts.begin())] = coords[0];
1069 coord_med[3 * (it - verts.begin()) + 1] = coords[1];
1070 coord_med[3 * (it - verts.begin()) + 2] = coords[2];
1071 fam.push_back(entityHandle_family_map[*it]);
1072 tags.push_back(use_gids ? gids[it - verts.begin()] : med_node_num);
1073 // map entityHandle to its position, used by the connectivity
1074 entityHandle_tag_map[*it] = med_node_num;
1075 med_node_num++;
1076 }
1077 // Write the coordinates to the MED file
1078 CHKERR MEDmeshNodeWr(fid, mesh_name.c_str(), MED_NO_DT, MED_NO_IT,
1079 MED_UNDEF_DT, MED_FULL_INTERLACE, verts.size(),
1080 &coord_med[0], MED_FALSE, "", MED_TRUE, &tags[0],
1081 MED_TRUE, &fam[0]);
1082
1083 // element numbers follow the largest node number
1084 med_int last_tag =
1085 tags.empty() ? 0 : *std::max_element(tags.begin(), tags.end());
1086 // loop to write elements to families
1087 for (EntityType ent_type = MBVERTEX; ent_type < MBMAXTYPE; ent_type++) {
1088 // get all entities of type ent_type
1089 Range entities;
1090 moab.get_entities_by_type(0, ent_type, entities, true);
1091 Range ents_to_write;
1092 ents_to_write = intersect(entities, *write_range_ptr);
1093
1094 if (ents_to_write.empty())
1095 continue;
1096
1097 // vectors to write
1098 std::vector<med_int> tag_number;
1099 std::vector<med_int> family_number;
1100 std::vector<med_int> connectivity;
1101
1102 // loop over all entities
1103 for (auto &entity : ents_to_write) {
1104 if (ent_type != MBVERTEX) {
1105 last_tag++;
1106 // get family id for entity
1107 family_number.push_back(entityHandle_family_map[entity]);
1108 // get tag number for entity
1109 tag_number.push_back(last_tag);
1110 // get connectivity for entity
1111 std::vector<EntityHandle> conn;
1112 moab.get_connectivity(&entity, 1, conn);
1113 // add connectivity to vector from map of entityHandle to new tag number
1114 for (auto &c : conn) {
1115 connectivity.push_back(entityHandle_tag_map[c]);
1116 }
1117 } else {
1118 continue;
1119 }
1120 }
1121
1122 auto get_med_element_type = [](EntityType ent_type) {
1123 med_geometrie_element type;
1124 switch (ent_type) {
1125 case MBHEX:
1126 type = MED_HEXA8;
1127 break;
1128 case MBTET:
1129 type = MED_TETRA4;
1130 break;
1131 case MBQUAD:
1132 type = MED_QUAD4;
1133 break;
1134 case MBTRI:
1135 type = MED_TRIA3;
1136 break;
1137 case MBEDGE:
1138 type = MED_SEG2;
1139 break;
1140 case MBPRISM:
1141 type = MED_PENTA6;
1142 break;
1143 default:
1144 type = MED_POINT1;
1145 break;
1146 }
1147 return type;
1148 };
1149
1150 med_geometrie_element med_type = get_med_element_type(ent_type);
1151 if (ent_type == MBENTITYSET) {
1152 continue;
1153 }
1154 CHKERR MEDmeshElementWr(fid, mesh_name.c_str(), MED_NO_DT, MED_NO_IT, 0.,
1155 MED_CELL, med_type, MED_NODAL, MED_FULL_INTERLACE,
1156 family_number.size(), &connectivity[0], MED_FALSE,
1157 nullptr, MED_TRUE, &tag_number[0], MED_TRUE,
1158 &family_number[0]);
1159
1160 MOFEM_LOG_C("MEDWORLD", Sev::inform, "Writing %zu elements of type %i (%s) ",
1161 family_number.size(), med_type,
1162 moab::CN::EntityTypeName(ent_type));
1163 }
1164
1165 // Close the MED file
1166 CHKERR MEDfermer(fid);
1168}
std::array< int, 4 > connectivity
It it
Definition Schur.cpp:2568
@ NODESET
@ SIDESET
@ LASTSET_BC
static const char *const CubitBCNames[]
Names of types of sets and boundary conditions.
const double c
speed of light (cm/ns)
moab::Interface & get_moab()
Definition Core.hpp:327

Member Data Documentation

◆ cOre

MoFEM::Core& MoFEM::MedInterface::cOre
private

core database

Definition at line 151 of file MedInterface.hpp.

◆ cubitMeshsets

CubitMeshSet_multiIndex MoFEM::MedInterface::cubitMeshsets
private

cubit meshsets

Definition at line 155 of file MedInterface.hpp.

◆ fieldNames

std::map<std::string, FieldData> MoFEM::MedInterface::fieldNames

Definition at line 147 of file MedInterface.hpp.

◆ flgFile

PetscBool MoFEM::MedInterface::flgFile
private

true if file name given in command line

Definition at line 153 of file MedInterface.hpp.

◆ medFileName

std::string MoFEM::MedInterface::medFileName

MED file name.

Definition at line 148 of file MedInterface.hpp.

◆ meshMeshsets

std::vector<EntityHandle> MoFEM::MedInterface::meshMeshsets

meshset for each mesh

Definition at line 146 of file MedInterface.hpp.

◆ meshNames

std::vector<std::string> MoFEM::MedInterface::meshNames

list of meshes in MED file

Definition at line 145 of file MedInterface.hpp.


The documentation for this struct was generated from the following files: