Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_API.cxx
1 // Copyright (C) 2007-2012  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 /*!
21  * API for the MEDSPLITTER tool 
22  * 
23  * API has a C binding so that it can be used
24  * in a C/C++ program and it can easily be wrapped 
25  * in a Fortran program
26  */
27  
28 #include "MEDSPLITTER_API.hxx"
29
30 #include <string>
31 #include "MEDMEM_define.hxx"
32 #include "MEDMEM_Mesh.hxx"
33 #include "MEDMEM_Family.hxx"
34 #include "MEDSPLITTER_Graph.hxx"
35 #include "MEDSPLITTER_MESHCollection.hxx"
36 #include "MEDSPLITTER_Topology.hxx"
37 #include "MEDSPLITTER_ParaDomainSelector.hxx"
38
39 using namespace std;
40 /*!
41  * MEDSPLITTER high-level API
42  * 
43  * \param inputfilename name of the input file
44  * \param mesh name of the input mesh (only used for the sequential input file case)
45  * \param outputfilename name out the master output file
46  * \param is_distributed sequential input (0) or distributed input (1)
47  * \param nbdomains number of subdomains
48  * \param method METIS(0) or SCOTCH(1)
49  * \param meshonly projects mesh and fields (0) or only the mesh (1) 
50  * \param plainmaster creates a plain masterfile (1) instead of an XML file (0),
51  * \param createboundaryfaces creates the necessary faces so that face joints are created
52  * \param familysplitting preserves the family names (1) instead of focusing on the groups (0)
53  * \param emptygroups creates empty groups in zones that do not contain a group from the original domain
54 */
55  
56 int medsplitter(const char* inputfilename, 
57                 const char* mesh,
58                 const char* outputfilename,  
59                 int is_distributed,
60                 int nbdomains,
61                 int method,
62                 int meshonly,
63                 int plainmaster,
64                 int createboundaryfaces,
65                 int familysplitting,
66                 int emptygroups)
67 {                        
68
69   //Pointer to the initial collection
70   MEDSPLITTER::MESHCollection* collection; 
71
72   // Loading the mesh collection
73   string input(inputfilename);
74
75   if (is_distributed ==0)
76   {
77     string meshname (mesh);
78     collection=new MEDSPLITTER::MESHCollection(input,meshname);
79   }
80   else
81     collection = new MEDSPLITTER::MESHCollection(input);
82
83   // Creating the graph and partitioning it
84   MEDSPLITTER::Topology* new_topo;
85   if (method==0)
86     new_topo = collection->createPartition(nbdomains,MEDSPLITTER::Graph::METIS);
87   else
88     new_topo = collection->createPartition(nbdomains,MEDSPLITTER::Graph::SCOTCH);
89   
90   // Creating a new mesh collection from the partitioning 
91   MEDSPLITTER::MESHCollection new_collection(*collection, new_topo, familysplitting, emptygroups);
92  
93   //Writing the output files (master + MED files)
94   if (plainmaster)
95     new_collection.setDriverType(MEDSPLITTER::MedAscii);
96   new_collection.setSubdomainBoundaryCreates(createboundaryfaces);
97   string output(outputfilename);
98   new_collection.write(output);
99
100   // Casting the fields on the new collection
101   if (meshonly!=0)
102     new_collection.castAllFields(*collection);
103   delete collection;
104
105   return 0;
106 }
107
108
109 /*!
110  * Parallel MEDSPLITTER high-level API
111  * 
112  * \param inputfilename name of the input distributed MED file
113  * \param outputfilename name out the master output file
114  * \param nprocs number of subdomains
115  * \param method METIS(0) or SCOTCH(1)
116  * \param create_boundary_faces creates the necessary faces so that faces joints are created in the output files
117  * \param family_splitting preserves the family names instead of focusing on the groups
118  */
119
120 int medsplitter_para(const char* inputfilename, 
121                      const char* outputfilename,  
122                      const int   nprocs,
123                      const int   method,
124                      const       bool create_boundary_faces,
125                      const       bool family_splitting)
126 {
127   // Parallelizer
128   MEDSPLITTER::ParaDomainSelector parallelizer;
129
130   // Loading the mesh collection
131   MEDSPLITTER::MESHCollection collection (inputfilename, parallelizer);
132
133   // Creating the graph and partitioning it   
134   auto_ptr<MEDSPLITTER::Topology> new_topo;
135   if (method==0)
136     new_topo.reset( collection.createPartition(nprocs,MEDSPLITTER::Graph::METIS));
137   else
138     new_topo.reset( collection.createPartition(nprocs,MEDSPLITTER::Graph::SCOTCH));
139
140   // Creating a new mesh collection from the partitioning 
141   MEDSPLITTER::MESHCollection new_collection(collection, new_topo.get(),family_splitting);
142   new_collection.setSubdomainBoundaryCreates(create_boundary_faces);
143
144   //Writing the output files (master + MED files)
145   string output(outputfilename);
146   new_collection.write(output);
147
148   return 0;
149 }