Salome HOME
Merge from V6_main (dev of Anthony GEAY) 11/06/2013
[tools/medcoupling.git] / src / RENUMBER / renumbering.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDFileData.hxx"
21 #include "MEDFileMesh.hxx"
22 #include "MEDFileField.hxx"
23
24 #include "MEDCouplingUMesh.hxx"
25
26 #include "RenumberingFactory.hxx"
27
28 #include <time.h>
29 #include <string>
30 #include <cstdlib>
31 #include <fstream>
32 #include <iostream>
33
34 using namespace std;
35 using namespace ParaMEDMEM;
36 using namespace MED_RENUMBER;
37
38 int main(int argc, char** argv)
39 {
40   double t_begin,t_read_st,t_compute_graph,t_family,t_field;
41   t_begin=clock();
42   if (argc <5)
43     {
44       cerr << "Usage : " << argv[0] 
45            << " filename_in meshname method[BOOST/METIS] filename_out" << endl << endl;
46       return -1;
47     }
48   string filename_in = argv[1];
49   string meshname = argv[2];
50   string type_renum = argv[3];
51   string filename_out = argv[4];
52
53   if(type_renum!="METIS" && type_renum!="BOOST")
54     {
55       cout << "The method has to be METIS or BOOST!" << endl;
56       return -1;
57     }
58   // Reading file structure
59   cout << "Reading : " << flush;
60   MEDCouplingAutoRefCountObjectPtr<MEDFileData> fd=MEDFileData::New(filename_in.c_str());
61   MEDFileMesh *m=fd->getMeshes()->getMeshWithName(meshname.c_str());
62   MEDFileUMesh *mc=dynamic_cast<MEDFileUMesh *>(m);
63   if(!mc)
64     {
65       std::ostringstream oss; oss << "In file \"" << filename_in << "\" the mesh name \"" << meshname<< "\" exists but is not unstructured !";
66       throw INTERP_KERNEL::Exception(oss.str().c_str());
67     }
68   t_read_st=clock();
69   cout << (t_read_st-t_begin)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
70   // Reading mesh
71   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> workMesh=mc->getMeshAtLevel(0);
72   std::vector<int> code=workMesh->getDistributionOfTypes();
73   cout << "Building the graph : " << flush;
74   DataArrayInt *neighb=0,*neighbI=0;
75   workMesh->computeNeighborsOfCells(neighb,neighbI);
76   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> neighbSafe(neighb),neighbISafe(neighbI);
77   const int *graph=neighbSafe->begin();
78   const int *graph_index=neighbISafe->begin();
79   // Compute permutation iperm->new2old perm->old2new
80   vector<int> iperm,perm;
81   Renumbering *renumb=RenumberingFactory(type_renum);
82   renumb->renumber(graph,graph_index,workMesh->getNumberOfCells(),iperm,perm);
83   delete renumb;
84   iperm.clear();//erase new2old, we are using only old 2 new
85   t_compute_graph=clock();
86   cout << " : " << (t_compute_graph-t_read_st)/(double) CLOCKS_PER_SEC << "s" << endl;
87   cout.flush();
88   // Connectivity
89   cout << "Reordering connectivity & families and writing : " << flush;
90   workMesh->renumberCells(&perm[0],false);
91   mc->setMeshAtLevel(0,workMesh);
92   const DataArrayInt *famField=mc->getFamilyFieldAtLevel(0);
93   if(famField)
94     {
95       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> famField2=famField->renumber(&perm[0]);
96       mc->setFamilyFieldArr(0,famField2);
97     }
98   mc->write(filename_out.c_str(),2);
99   t_family=clock();
100   cout << " : " << (t_family-t_compute_graph)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
101   // Fields
102   cout << "Reordering fields and writing : " << flush;
103   MEDFileFields *fs=fd->getFields();
104   if(fs)
105     {
106       for(int i=0;i<fs->getNumberOfFields();i++)
107         {
108           MEDFileFieldMultiTS *fmts=dynamic_cast<MEDFileFieldMultiTS *>(fs->getFieldAtPos(i));
109           if(!fmts) continue;
110           if(fmts->getMeshName()==meshname)
111             {
112               for(int j=0;j<fmts->getNumberOfTS();j++)
113                 {
114                   MEDFileField1TS *f1ts=dynamic_cast<MEDFileField1TS*>(fmts->getTimeStepAtPos(j));
115                   if(!f1ts) continue;
116                   DataArrayDouble *arr=f1ts->getUndergroundDataArray();
117                   arr->renumberInPlace(&perm[0]);
118                 }
119             }
120         }
121       fs->write(filename_out.c_str(),0);
122       //fs->renumberEntitiesLyingOnMesh(meshname.c_str(),code,code,o2n); bugged
123     }
124   t_field=clock();
125   cout << " : " << (t_field-t_family)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
126   return 0;
127 }