Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / RENUMBER / renumbering.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 MEDCoupling;
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=(double)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   MCAuto<MEDFileData> fd(MEDFileData::New(filename_in));
61   MEDFileMesh *m=fd->getMeshes()->getMeshWithName(meshname);
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=(double)clock();
69   cout << (t_read_st-t_begin)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
70   // Reading mesh
71   MCAuto<MEDCouplingUMesh> workMesh=mc->getMeshAtLevel(0);
72   //std::vector<mcIdType> code=workMesh->getDistributionOfTypes();
73   cout << "Building the graph : " << flush;
74   DataArrayIdType *neighb=0,*neighbI=0;
75   workMesh->computeNeighborsOfCells(neighb,neighbI);
76   MCAuto<DataArrayIdType> neighbSafe(neighb),neighbISafe(neighbI),ipermSafe,permSafe;
77   const mcIdType *graph=neighbSafe->begin();
78   const mcIdType *graph_index=neighbISafe->begin();
79   // Compute permutation iperm->new2old perm->old2new
80   DataArrayIdType *iperm(0),*perm(0);
81   Renumbering *renumb=RenumberingFactory(type_renum);
82   renumb->renumber(graph,graph_index,workMesh->getNumberOfCells(),iperm,perm);
83   ipermSafe=iperm; permSafe=perm;
84   delete renumb;
85   ipermSafe=0;//erase new2old, we are using only old 2 new
86   t_compute_graph=(double)clock();
87   cout << " : " << (t_compute_graph-t_read_st)/(double) CLOCKS_PER_SEC << "s" << endl;
88   cout.flush();
89   // Connectivity
90   cout << "Reordering connectivity & families and writing : " << flush;
91   workMesh->renumberCells(perm->begin(),false);
92   mc->setMeshAtLevel(0,workMesh);
93   const DataArrayIdType *famField=mc->getFamilyFieldAtLevel(0);
94   if(famField)
95     {
96       MCAuto<DataArrayIdType> famField2=famField->renumber(perm->begin());
97       mc->setFamilyFieldArr(0,famField2);
98     }
99   mc->write(filename_out,2);
100   t_family=(double)clock();
101   cout << " : " << (t_family-t_compute_graph)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
102   // Fields
103   cout << "Reordering fields and writing : " << flush;
104   MEDFileFields *fs=fd->getFields();
105   if(fs)
106     {
107       for(int i=0;i<fs->getNumberOfFields();i++)
108         {
109           MEDFileFieldMultiTS *fmts=dynamic_cast<MEDFileFieldMultiTS *>(fs->getFieldAtPos(i));
110           if(!fmts) continue;
111           if(fmts->getMeshName()==meshname)
112             {
113               for(int j=0;j<fmts->getNumberOfTS();j++)
114                 {
115                   MEDFileField1TS *f1ts=dynamic_cast<MEDFileField1TS*>(fmts->getTimeStepAtPos(j));
116                   if(!f1ts) continue;
117                   DataArrayDouble *arr=f1ts->getUndergroundDataArray();
118                   arr->renumberInPlace(perm->begin());
119                 }
120             }
121         }
122       fs->write(filename_out,0);
123       //fs->renumberEntitiesLyingOnMesh(meshname,code,code,o2n); bugged
124     }
125   t_field=(double)clock();
126   cout << " : " << (t_field-t_family)/(double) CLOCKS_PER_SEC << "s" << endl << flush;
127   return 0;
128 }