20#include <medcoupling++.h>
24#include <MEDLoader.hxx>
25#include <MEDFileMesh.hxx>
26#include <MEDCouplingFieldDouble.hxx>
27using namespace MEDCoupling;
44std::vector<std::string> glob_med_files(
const std::string& fileName)
49 glob(fileName.c_str(),GLOB_TILDE,
nullptr,&glob_result);
51 std::vector<std::string> ls_med;
52 for(
unsigned int i=0; i<glob_result.gl_pathc; ++i)
53 ls_med.push_back(std::string(glob_result.gl_pathv[i]));
55 globfree(&glob_result);
67void Merge_MED::mergeFields(
const std::vector< std::string >& field_names,
68 const std::vector< std::string >& meshes_names,
69 const std::vector<std::string>& listmed,
70 const std::vector< std::pair< std::pair< int,int>,
double >>& lst_dt,
74 bool& first_time,
const bool isCell)
const
77 for (
const auto & fld_name: field_names)
80 std::vector<const MEDCouplingFieldDouble *> fields(listmed.size());
82 for (
size_t i=0; i<listmed.size(); i++)
84 MEDCouplingFieldDouble *f =
nullptr;
87 f =
dynamic_cast<MEDCouplingFieldDouble *
>
88 (ReadFieldCell(listmed[i],meshes_names[mesh_numb] ,0,fld_name,
89 lst_dt[iter_numb].first.first,lst_dt[iter_numb].first.second));
93 f =
dynamic_cast<MEDCouplingFieldDouble *
>
94 (ReadFieldNode(listmed[i],meshes_names[mesh_numb] ,0,fld_name,
95 lst_dt[iter_numb].first.first,lst_dt[iter_numb].first.second));
99 Cerr <<
"Unexpected internal error!!" << finl;
105 MCAuto<MEDCouplingFieldDouble> merged_field = MEDCouplingFieldDouble::MergeFields(fields);
111 WriteField(out_file.
getString(),merged_field,
true);
113 WriteField(out_file.
getString(),merged_field,
false);
119 for (
auto & f: fields)
127 inline bool operator() (
const std::pair< std::pair< int,int>,
double >& a,
const std::pair< std::pair< int,int>,
double >& b)
const
129 if (a.first.first == b.first.first)
130 return (a.first.second < b.first.second);
131 return (a.first.first < b.first.first);
142 Cerr<<
"Syntax Merge_MED::interpreter [NOM_DU_CAS|med_files_base_name] [all_times|last_time]"<<finl;
143 Nom med_name,time_opt;
144 is >> med_name>> time_opt;
146 if (med_name ==
"NOM_DU_CAS")
149 Nom files=med_name+
"_*.med";
152 std::vector<std::string> listmed=glob_med_files(files.
getString());
154 if (listmed.size() == 0)
156 Cerr <<
"No multiple med files with a basename " << med_name <<
" are present in this directory !" << finl;
157 Cerr <<
"Verify your data file or check if the files " << med_name <<
"_xxxx.med are present in this directory !" << finl;
162 Cerr <<
"In process of merging " << (int)listmed.size() <<
" med files in a single med file ..." << finl;
165 const std::vector< std::string > meshes_names = GetMeshNames(listmed[0]);
168 const std::vector< std::string > all_field_names = GetAllFieldNames(listmed[0]);
172 std::vector< std::pair< std::pair< int,int>,
double >> lst_dt=GetAllFieldIterations(listmed[0],all_field_names[0]);
174 std::sort(lst_dt.begin(), lst_dt.end(), less_than_key());
176 int nb_steps = (int)lst_dt.size();
177 Cerr <<
"Number of iterations detected : " << nb_steps << finl;
178 Cerr <<
"Number of meshes detected : " << (int)meshes_names.size() << finl;
182 std::vector< std::string > cell_fields;
183 std::vector< std::string > node_fields;
186 for (
size_t i=0; i<meshes_names.size(); i++)
189 cell_fields = GetCellFieldNamesOnMesh(listmed[0],meshes_names[i]);
190 node_fields = GetNodeFieldNamesOnMesh(listmed[0],meshes_names[i]);
191 int tot_fld_num = (int)cell_fields.size() + (int)node_fields.size();
192 Cerr <<
"Number of fields detected on mesh " << (int)i +1 <<
" : " << tot_fld_num << finl;
193 Cerr <<
" - Number of P0 fields : " << (int)cell_fields.size() << finl;
194 Cerr <<
" - Number of P1 fields : " << (int)node_fields.size() << finl;
195 count = count + tot_fld_num;
198 assert(count == (
int)all_field_names.size());
201 Nom out_file=med_name+
".med";
205 size_t first_iter = time_opt ==
"last_time" ? (lst_dt.size()-1) : 0;
206 for (
size_t iter=first_iter; iter<lst_dt.size(); iter++)
208 for (
size_t msh=0; msh<meshes_names.size(); msh++)
210 cell_fields = GetCellFieldNamesOnMesh(listmed[0],meshes_names[msh]);
211 node_fields = GetNodeFieldNamesOnMesh(listmed[0],meshes_names[msh]);
213 mergeFields(cell_fields,meshes_names,listmed,lst_dt,(
int)msh,(
int)iter,out_file,first_time,
true);
214 mergeFields(node_fields,meshes_names,listmed,lst_dt,(
int)msh,(
int)iter,out_file,first_time,
false);
225 Cerr <<
"Version compiled without MEDCoupling" << finl;
231void Merge_MED::mergeFields(
const std::vector< std::string >& field_names,
232 const std::vector< std::string >& meshes_names,
233 const std::vector<std::string>& listmed,
234 const std::vector< std::pair< std::pair< int,int>,
double >>& lst_dt,
238 bool& first_time,
const bool isCell)
const
240 Cerr <<
"Version compiled without MEDCoupling" << finl;
Class defining operators and methods for all reading operation in an input flow (file,...
Classe de base des objets "interprete".
Class Merge_MED Merge several MED files written by several procs into a single field object with a pr...
Entree & interpreter(Entree &) override
class Nom Une chaine de caractere pour nommer les objets de TRUST
const std::string & getString() const
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
static double precision_geom
static const Nom & nom_du_cas()
Renvoie une reference constante vers le nom du cas.
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
static int je_suis_maitre()
renvoie 1 si on est sur le processeur maitre du groupe courant (c'est a dire me() == 0),...
Classe de base des flux de sortie.