1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "MEDMEM_Exception.hxx"
23 #include "MEDMEM_define.hxx"
25 #include "MEDMEM_Med.hxx"
26 #include "MEDMEM_Mesh.hxx"
27 #include "MEDMEM_Family.hxx"
28 #include "MEDMEM_Support.hxx"
29 #include "MEDMEM_Field.hxx"
32 using namespace MEDMEM;
34 void usage(char * name)
36 cout << " ERROR ABOUT SYNTAX " << endl ;
37 cout << " " << name << " <input med file> <output med file> " << endl ;
41 int main (int argc, char ** argv) {
45 set <int> listElements, supset;
46 set <int>::iterator elemIt, ssIt;
49 filenameIN = argv[1] ;
50 filenameOUT = argv[2] ;
51 cout << "-> reading all into the Med file " << filenameIN << " and writing all into the Ensight file " << filenameOUT << endl ;
53 MED myMed(MED_DRIVER,filenameIN) ;
55 cout << "-> Read all meshes " ;
56 int NumberOfMeshes = myMed.getNumberOfMeshes() ;
57 cout << "( "<<NumberOfMeshes << " ) :" << endl ;
58 FIELD<double> **volume = new FIELD<double>*[NumberOfMeshes];
59 deque<string> MeshName = myMed.getMeshNames() ;
60 for (int i=0; i<NumberOfMeshes; i++) {
62 myMed.getMesh(MeshName[i])->read() ;
63 cout << "-> Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
64 int id = myMed.getMesh(MeshName[i])->addDriver(MED_DRIVER,filenameOUT,MeshName[i],MED_EN::MED_ECRI);
65 myMed.getMesh(MeshName[i])->write(id);
66 cout << "-> Mesh "<<i+1<<", named "<<MeshName[i]<<" is written !" << endl;
69 myMed.updateSupport() ;
71 cout << "-> Read all fields " ;
72 int NumberOfFields = myMed.getNumberOfFields() ;
73 cout << "( "<<NumberOfFields << " ) :" << endl;
74 deque<string> FieldName = myMed.getFieldNames() ;
75 for (int i=0; i<NumberOfFields; i++) {
76 deque<DT_IT_> FieldIteration = myMed.getFieldIteration(FieldName[i]) ;
77 cout << "-> Field "<<i+1<<", named "<<FieldName[i] << " :" << endl ;
78 int NumberOfIteration = FieldIteration.size() ;
79 cout << " Number of iteration pair : "<< NumberOfIteration << endl;
80 for (int j=0; j<NumberOfIteration; j++) {
81 FIELD_ * myField = myMed.getField(FieldName[i],FieldIteration[j].dt,FieldIteration[j].it) ;
84 cout << " * Iteration "<<FieldIteration[j].dt<<" and order number "<<FieldIteration[j].it<<" ) is read !" << endl;
86 int NumberOfComponents = myField->getNumberOfComponents();
87 switch(myField->getSupport()->getEntity()){
90 cout << "*************************** CHAMP AUX CELLULES" << endl;
91 MESH *mesh = myField->getSupport()->getMesh();
93 // create new support for new field
95 if( myField->getSupport()->isOnAllElements() )
96 newSup = new SUPPORT(mesh,"Support",MED_NODE);
98 int nbe = myField->getSupport()->getNumberOfElements(MED_ALL_ELEMENTS);
99 const int *numb = myField->getSupport()->getNumber(MED_ALL_ELEMENTS);
101 for(int k=0;k<nbe;k++){
102 myList.push_back(numb[k]);
103 supset.insert(numb[k]);
105 newSup = mesh->buildSupportOnNodeFromElementList(myList,MED_CELL);
108 // read number of nodes
109 int NumberOfNodes = newSup->getNumberOfElements(MED_ALL_ELEMENTS);
110 // calculate reverse connectivity to have the list of elements which contains node i
111 const int *revC = myField->getSupport()->getMesh()->getReverseConnectivity(MED_NODAL,MED_CELL);
112 const int *indC = myField->getSupport()->getMesh()->getReverseConnectivityIndex(MED_NODAL,MED_CELL);
114 // calculate volume field on mesh
115 for(int k=0;k<NumberOfMeshes;k++)
116 if(strcmp(mesh->getName().c_str(),MeshName[k].c_str())==0)
118 if( volume[ivol] == NULL )
119 volume[ivol] = myField->getSupport()->getMesh()->getVolume(myField->getSupport());
120 if (dynamic_cast<MEDMEM::FIELD<double>*>(myField)){
121 FIELD<double> *myDField = (MEDMEM::FIELD<double>*)myField;
122 FIELD<double> *newDField = new FIELD<double>(newSup,NumberOfComponents);
123 newDField->setName(myField->getName());
124 newDField->setIterationNumber(FieldIteration[j].dt);
125 newDField->setOrderNumber(FieldIteration[j].it);
126 newDField->setTime(myDField->getTime());
127 double *val = new double[NumberOfComponents];
128 if( newSup->isOnAllElements() ){
129 for (int k=1; k<NumberOfNodes+1; k++){
130 // listElements contains elements which contains a node of element i
131 listElements.clear();
132 for(int l=indC[k-1];l<indC[k];l++){
133 // c element contains node k
135 listElements.insert(c);
138 // calculate field value on node k
140 for(int l=0;l<NumberOfComponents;l++)
142 for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
144 double vol = volume[ivol]->getValueIJ(elem,1);
147 for(int l=1;l<=NumberOfComponents;l++)
148 val[l-1] += myDField->getValueIJ(elem,l)/vol;
151 for(int l=1;l<=NumberOfComponents;l++)
152 newDField->setValueIJ(k,l,val[l-1]/sigmaV);
156 const int *numb = newSup->getNumber(MED_ALL_ELEMENTS);
157 for (int k=1; k<=NumberOfNodes; k++){
159 // listElements contains elements which contains a node of element i
160 listElements.clear();
161 for(int l=indC[kk-1];l<indC[kk];l++){
162 // c element contains node k
164 // add c element only if it is in partial support
165 ssIt = supset.find(c);
166 if(ssIt!=supset.end())
167 listElements.insert(c);
170 // calculate field value on node k
172 for(int l=0;l<NumberOfComponents;l++)
174 for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
176 double vol = volume[ivol]->getValueIJ(elem,1);
179 for(int l=1;l<=NumberOfComponents;l++)
180 val[l-1] += myDField->getValueIJ(elem,l)/vol;
183 for(int l=1;l<=NumberOfComponents;l++)
184 newDField->setValueIJ(k,l,val[l-1]/sigmaV);
188 int id = newDField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
189 newDField->write(id);
192 cout << " * Iteration "<<FieldIteration[j].dt<<" and order number "<<FieldIteration[j].it<<" ) is written !" << endl;
195 FIELD<int> *myIField = (MEDMEM::FIELD<int>*)myField;
196 FIELD<int> *newIField = new FIELD<int>(newSup,NumberOfComponents);
197 newIField->setName(myField->getName());
198 newIField->setIterationNumber(FieldIteration[j].dt);
199 newIField->setOrderNumber(FieldIteration[j].it);
200 newIField->setTime(myIField->getTime());
201 double *val = new double[NumberOfComponents];
202 if( newSup->isOnAllElements() ){
203 for (int k=1; k<NumberOfNodes+1; k++){
204 // listElements contains elements which contains a node of element i
205 listElements.clear();
206 for(int l=indC[k-1];l<indC[k];l++){
207 // c element contains node i
209 listElements.insert(c);
212 // calculate field value on node k
214 for(int l=0;l<NumberOfComponents;l++)
216 for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
218 double vol = volume[ivol]->getValueIJ(elem,1);
221 for(int l=1;l<=NumberOfComponents;l++)
222 val[l-1] += ((double)myIField->getValueIJ(elem,l))/vol;
225 for(int l=1;l<=NumberOfComponents;l++)
226 newIField->setValueIJ(k,l,(int)(val[l-1]/sigmaV));
230 const int *numb = newSup->getNumber(MED_ALL_ELEMENTS);
231 for (int k=1; k<=NumberOfNodes; k++){
233 // listElements contains elements which contains a node of element i
234 listElements.clear();
235 for(int l=indC[kk-1];l<indC[kk];l++){
236 // c element contains node k
238 // add c element only if it is in partial support
239 ssIt = supset.find(c);
240 if(ssIt!=supset.end())
241 listElements.insert(c);
244 // calculate field value on node k
246 for(int l=0;l<NumberOfComponents;l++)
248 for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
250 double vol = volume[ivol]->getValueIJ(elem,1);
253 for(int l=1;l<=NumberOfComponents;l++)
254 val[l-1] += myIField->getValueIJ(elem,l)/vol;
257 for(int l=1;l<=NumberOfComponents;l++)
258 newIField->setValueIJ(k,l,(int)(val[l-1]/sigmaV));
262 int id = newIField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
263 newIField->write(id);
266 cout << " * Iteration "<<FieldIteration[j].dt<<" and order number "<<FieldIteration[j].it<<" ) is written !" << endl;
271 cout << "*************************** CHAMP AUX FACES" << endl;
274 cout << "*************************** CHAMP AUX ARETES" << endl;
277 cout << "*************************** CHAMP AUX NOEUDS" << endl;
278 int id = myField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
280 cout << " * Iteration "<<FieldIteration[j].dt<<" and order number "<<FieldIteration[j].it<<" ) is written !" << endl;