]> SALOME platform Git repositories - modules/filter.git/blobdiff - src/FILTER/field2nodes.cxx
Salome HOME
new version of ensight driver
[modules/filter.git] / src / FILTER / field2nodes.cxx
diff --git a/src/FILTER/field2nodes.cxx b/src/FILTER/field2nodes.cxx
new file mode 100644 (file)
index 0000000..0639478
--- /dev/null
@@ -0,0 +1,188 @@
+// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either 
+// version 2.1 of the License.
+// 
+// This library is distributed in the hope that it will be useful 
+// but WITHOUT ANY WARRANTY; without even the implied warranty of 
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public  
+// License along with this library; if not, write to the Free Software 
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+#include<string>
+#include<deque>
+
+#include "MEDMEM_Exception.hxx"
+#include "MEDMEM_define.hxx"
+
+#include "MEDMEM_Med.hxx"
+#include "MEDMEM_Mesh.hxx"
+#include "MEDMEM_Family.hxx"
+#include "MEDMEM_Support.hxx"
+#include "MEDMEM_Field.hxx"
+
+using namespace std;
+using namespace MEDMEM;
+
+void usage(char * name)
+{
+  cout << " ERROR ABOUT SYNTAX " << endl ;
+  cout << "  " << name << " <input med file> <output med file> " << endl ;
+  exit(-1);
+}
+
+int main (int argc, char ** argv) {
+
+  string filenameIN ;
+  string filenameOUT;
+  set <int> listElements;
+  set <int>::iterator elemIt ;
+  
+  if ( argc == 3 ) {
+    filenameIN  = argv[1] ;
+    filenameOUT = argv[2] ;
+    cout << "-> reading all into the Med file " << filenameIN << " and writing all into the Ensight file " << filenameOUT <<  endl ;
+
+    MED myMed(MED_DRIVER,filenameIN) ;
+
+    cout << "-> Read all meshes "  ;
+    int NumberOfMeshes = myMed.getNumberOfMeshes() ;
+    cout << "( "<<NumberOfMeshes << " ) :" << endl ;
+    deque<string> MeshName = myMed.getMeshNames() ;
+    for (int i=0; i<NumberOfMeshes; i++) {
+      myMed.getMesh(MeshName[i])->read() ;
+      cout << "-> Mesh "<<i+1<<", named "<<MeshName[i]<<" is read !" << endl;
+      int id = myMed.getMesh(MeshName[i])->addDriver(MED_DRIVER,filenameOUT,MeshName[i],MED_EN::MED_ECRI);
+      myMed.getMesh(MeshName[i])->write(id);
+      cout << "-> Mesh "<<i+1<<", named "<<MeshName[i]<<" is written !" << endl;
+    }
+
+    myMed.updateSupport() ;
+    
+    cout << "-> Read all fields " ;
+    int NumberOfFields = myMed.getNumberOfFields() ;
+    cout << "( "<<NumberOfFields << " ) :" << endl;
+    deque<string> FieldName = myMed.getFieldNames() ;
+    for (int i=0; i<NumberOfFields; i++) {
+      deque<DT_IT_> FieldIteration = myMed.getFieldIteration(FieldName[i]) ;
+      cout << "-> Field "<<i+1<<", named "<<FieldName[i] << " :" << endl ;
+      int NumberOfIteration = FieldIteration.size() ;
+      cout << "    Number of iteration pair : "<< NumberOfIteration << endl;
+      for (int j=0; j<NumberOfIteration; j++) {
+       FIELD_ * myField = myMed.getField(FieldName[i],FieldIteration[j].dt,FieldIteration[j].it) ;
+       
+       myField->read() ;
+       cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is read !" << endl;
+
+       int NumberOfComponents = myField->getNumberOfComponents();
+       switch(myField->getSupport()->getEntity()){
+       case MED_CELL:
+         cout << "*************************** CHAMP AUX CELLULES" << endl;
+         MESH *mesh = myMed.getField(FieldName[i],FieldIteration[j].dt,FieldIteration[j].it)->getSupport()->getMesh();
+         SUPPORT *sup = new SUPPORT(mesh,"Support",MED_NODE);
+         // read number of nodes
+         int NumberOfNodes = sup->getNumberOfElements(MED_ALL_ELEMENTS);
+         // calculate reverse connectivity to have the list of elements which contains node i
+         const int *revC = myField->getSupport()->getMesh()->getReverseConnectivity(MED_NODAL,MED_CELL);
+         const int *indC = myField->getSupport()->getMesh()->getReverseConnectivityIndex(MED_NODAL,MED_CELL);
+         // calculate volume field on mesh
+         FIELD<double> *volume = myField->getSupport()->getMesh()->getVolume(myField->getSupport());
+         if (dynamic_cast<MEDMEM::FIELD<double>*>(myField)){
+           FIELD<double> *myDField = (MEDMEM::FIELD<double>*)myField;
+           FIELD<double> *newDField = new FIELD<double>(sup,NumberOfComponents);
+           newDField->setName(myField->getName());
+           double *val = new double[NumberOfComponents];
+           for (int k=1; k<NumberOfNodes+1; k++){
+             // listElements contains elements which contains a node of element i
+             listElements.clear();
+             for(int j=indC[k-1];j<indC[k];j++){
+               // c element contains node i
+               int c=revC[j-1];
+               listElements.insert(c);
+             }
+
+             // calculate field value on node i 
+             double sigmaV = 0.;
+             for(int j=0;j<NumberOfComponents;j++)
+               val[j] = 0.;
+             for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
+               int elem = *elemIt;
+               double vol = volume->getValueIJ(elem,1);
+               if( vol != 0. ){
+                 sigmaV += 1./vol;
+                 for(int j=1;j<=NumberOfComponents;j++)
+                   val[j] += myDField->getValueIJ(elem,j)/vol;
+               }
+             }
+             for(int j=1;j<=NumberOfComponents;j++)
+               newDField->setValueIJ(k,j,val[j]/sigmaV);
+           }
+           delete [] val;
+           int id = newDField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
+           newDField->write(id);
+           cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is written !" << endl;
+         }
+         else{
+           FIELD<int> *myIField = (MEDMEM::FIELD<int>*)myField;
+           FIELD<int> *newIField = new FIELD<int>(sup,NumberOfComponents);
+           newIField->setName(myField->getName());
+           double *val = new double[NumberOfComponents];
+           for (int k=1; k<NumberOfNodes+1; k++){
+             // listElements contains elements which contains a node of element i
+             listElements.clear();
+             for(int j=indC[k-1];j<indC[k];j++){
+               // c element contains node i
+               int c=revC[j-1];
+               listElements.insert(c);
+             }
+
+             // calculate field value on node i 
+             double sigmaV = 0.;
+             for(int j=0;j<NumberOfComponents;j++)
+               val[j] = 0.;
+             for(elemIt=listElements.begin();elemIt!=listElements.end();elemIt++){
+               int elem = *elemIt;
+               double vol = volume->getValueIJ(elem,1);
+               if( vol != 0. ){
+                 sigmaV += 1./vol;
+                 for(int j=1;j<=NumberOfComponents;j++)
+                   val[j] += ((double)myIField->getValueIJ(elem,j))/vol;
+               }
+             }
+             for(int j=1;j<=NumberOfComponents;j++)
+               newIField->setValueIJ(k,j,(int)(val[j]/sigmaV));
+           }
+           delete [] val;
+           int id = newIField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
+           newIField->write(id);
+           cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is written !" << endl;
+         }
+         break;
+       case MED_FACE:
+         cout << "*************************** CHAMP AUX FACES" << endl;
+         break;
+       case MED_EDGE:
+         cout << "*************************** CHAMP AUX ARETES" << endl;
+         break;
+       case MED_NODE:
+         cout << "*************************** CHAMP AUX NOEUDS" << endl;
+         int id = myField->addDriver(MED_DRIVER,filenameOUT,FieldName[i],MED_EN::MED_ECRI);
+         myField->write(id);
+         cout << "    * Iteration "<<FieldIteration[j].dt<<" and  order number "<<FieldIteration[j].it<<" ) is written !" << endl;
+         break;
+       }
+
+      }
+    }
+
+  }
+  else usage(argv[0]);
+}