Salome HOME
OK thanks to INV !
[modules/med.git] / src / MEDCoupling / MEDCouplingStructuredMesh.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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingStructuredMesh.hxx"
22 #include "MEDCouplingFieldDouble.hxx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDCouplingUMesh.hxx"
25
26 #include <numeric>
27
28 using namespace ParaMEDMEM;
29
30 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh()
31 {
32 }
33
34 MEDCouplingStructuredMesh::MEDCouplingStructuredMesh(const MEDCouplingStructuredMesh& other, bool deepCopy):MEDCouplingMesh(other)
35 {
36 }
37
38 MEDCouplingStructuredMesh::~MEDCouplingStructuredMesh()
39 {
40 }
41
42 std::size_t MEDCouplingStructuredMesh::getHeapMemorySize() const
43 {
44   return MEDCouplingMesh::getHeapMemorySize();
45 }
46
47 void MEDCouplingStructuredMesh::copyTinyStringsFrom(const MEDCouplingMesh *other) throw(INTERP_KERNEL::Exception)
48 {
49   MEDCouplingMesh::copyTinyStringsFrom(other);
50 }
51
52 bool MEDCouplingStructuredMesh::isEqualIfNotWhy(const MEDCouplingMesh *other, double prec, std::string& reason) const throw(INTERP_KERNEL::Exception)
53 {
54   return MEDCouplingMesh::isEqualIfNotWhy(other,prec,reason);
55 }
56
57 INTERP_KERNEL::NormalizedCellType MEDCouplingStructuredMesh::getTypeOfCell(int cellId) const
58 {
59   switch(getMeshDimension())
60     {
61     case 3:
62       return INTERP_KERNEL::NORM_HEXA8;
63     case 2:
64       return INTERP_KERNEL::NORM_QUAD4;
65     case 1:
66       return INTERP_KERNEL::NORM_SEG2;
67     default:
68       throw INTERP_KERNEL::Exception("Unexpected dimension for MEDCouplingCurveLinearMesh::getTypeOfCell !");
69     }
70 }
71
72 std::set<INTERP_KERNEL::NormalizedCellType> MEDCouplingStructuredMesh::getAllGeoTypes() const
73 {
74   std::set<INTERP_KERNEL::NormalizedCellType> ret2;
75   ret2.insert(getTypeOfCell(0));
76   return ret2;
77 }
78
79 int MEDCouplingStructuredMesh::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType type) const
80 {
81   int ret=getNumberOfCells();
82   if(type==getTypeOfCell(0))
83     return ret;
84   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
85   std::ostringstream oss; oss << "MEDCouplingStructuredMesh::getNumberOfCellsWithType : no specified type ! Type available is " << cm.getRepr() << " !";
86   throw INTERP_KERNEL::Exception(oss.str().c_str());
87 }
88
89 DataArrayInt *MEDCouplingStructuredMesh::giveCellsWithType(INTERP_KERNEL::NormalizedCellType type) const throw(INTERP_KERNEL::Exception)
90 {
91   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
92   if(getTypeOfCell(0)==type)
93     {
94       ret->alloc(getNumberOfCells(),1);
95       ret->iota(0);
96     }
97   else
98     ret->alloc(0,1);
99   return ret.retn();
100 }
101
102 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfNodesPerCell() const throw(INTERP_KERNEL::Exception)
103 {
104   int nbCells=getNumberOfCells();
105   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
106   ret->alloc(nbCells,1);
107   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
108   ret->fillWithValue((int)cm.getNumberOfNodes());
109   return ret.retn();
110 }
111
112 DataArrayInt *MEDCouplingStructuredMesh::computeNbOfFacesPerCell() const throw(INTERP_KERNEL::Exception)
113 {
114   int nbCells=getNumberOfCells();
115   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
116   ret->alloc(nbCells,1);
117   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(getTypeOfCell(0));
118   ret->fillWithValue((int)cm.getNumberOfSons());
119   return ret.retn();
120 }
121
122 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
123 {
124   int meshDim=getMeshDimension();
125   int tmpCell[3],tmpNode[3];
126   getSplitCellValues(tmpCell);
127   getSplitNodeValues(tmpNode);
128   int tmp2[3];
129   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
130   switch(meshDim)
131     {
132     case 1:
133       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
134       break;
135     case 2:
136       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
137       conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
138       break;
139     case 3:
140       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+tmp2[2]*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]);
141       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+tmp2[2]*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+tmp2[2]*tmpNode[2]);
142       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]);
143       conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+1+(tmp2[2]+1)*tmpNode[2]); conn.push_back((tmp2[1]+1)*tmpNode[1]+tmp2[0]+(tmp2[2]+1)*tmpNode[2]);
144       break;
145     default:
146       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
147     };
148 }
149
150 /*!
151  * See MEDCouplingUMesh::getDistributionOfTypes for more information
152  */
153 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
154 {
155   //only one type of cell
156   std::vector<int> ret(3);
157   ret[0]=getTypeOfCell(0);
158   ret[1]=getNumberOfCells();
159   ret[2]=-1; //ret[3*k+2]==-1 because it has no sense here
160   return ret;
161 }
162
163 /*!
164  * This method tries to minimize at most the number of deep copy.
165  * So if \a idsPerType is not empty it can be returned directly (without copy, but with ref count incremented) in return.
166  * 
167  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
168  */
169 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
170 {
171   int nbOfCells=getNumberOfCells();
172   if(code.size()!=3)
173     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : invalid input code should be exactly of size 3 !");
174   if(code[0]!=(int)getTypeOfCell(0))
175     {
176       std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : Mismatch of geometric type ! Asking for " << code[0] << " whereas the geometric type is \a this is " << getTypeOfCell(0) << " !";
177       throw INTERP_KERNEL::Exception(oss.str().c_str());
178     }
179   if(code[2]==-1)
180     {
181       if(code[1]==nbOfCells)
182         return 0;
183       else
184         {
185           std::ostringstream oss; oss << "MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : mismatch between the number of cells in this (" << nbOfCells << ") and the number of non profile (" << code[1] << ") !";
186           throw INTERP_KERNEL::Exception(oss.str().c_str());
187         }
188     }
189   if(code[2]!=0)
190     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : single geo type mesh ! 0 or -1 is expected at pos #2 of input code !");
191   if(idsPerType.size()!=1)
192     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input code points to DataArrayInt #0 whereas the size of idsPerType is not equal to 1 !");
193   const DataArrayInt *pfl=idsPerType[0];
194   if(!pfl)
195     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : the input code points to a NULL DataArrayInt at rank 0 !");
196   if(pfl->getNumberOfComponents()!=1)
197     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::checkTypeConsistencyAndContig : input profile should have exactly one component !");
198   pfl->checkAllIdsInRange(0,nbOfCells);
199   pfl->incrRef();
200   return const_cast<DataArrayInt *>(pfl);
201 }
202
203 /*!
204  * This method is the opposite of MEDCouplingUMesh::checkTypeConsistencyAndContig method. Given a list of cells in \a profile it returns a list of sub-profiles sorted by geo type.
205  * The result is put in the array \a idsPerType. In the returned parameter \a code, foreach i \a code[3*i+2] refers (if different from -1) to a location into the \a idsPerType.
206  * This method has 1 input \a profile and 3 outputs \a code \a idsInPflPerType and \a idsPerType.
207  * 
208  * \param [out] code is a vector of size 3*n where n is the number of different geometric type in \a this \b reduced to the profile \a profile. \a code has exactly the same semantic than in MEDCouplingUMesh::checkTypeConsistencyAndContig method.
209  * \param [out] idsInPflPerType is a vector of size of different geometric type in the subpart defined by \a profile of \a this ( equal to \a code.size()/3). For each i,
210  *              \a idsInPflPerType[i] stores the tuple ids in \a profile that correspond to the geometric type code[3*i+0]
211  * \param [out] idsPerType is a vector of size of different sub profiles needed to be defined to represent the profile \a profile for a given geometric type.
212  *              This vector can be empty in case of all geometric type cells are fully covered in ascending in the given input \a profile.
213  * 
214  * \warning for performance reasons no deep copy will be performed, if \a profile can been used as this in output parameters \a idsInPflPerType and \a idsPerType.
215  *
216  * \throw if \a profile has not exactly one component. It throws too, if \a profile contains some values not in [0,getNumberOfCells()) or if \a this is not fully defined
217  *
218  *  \b Example1: <br>
219  *          - Before \a this has 3 cells \a profile contains [0,1,2]
220  *          - After \a code contains [NORM_...,nbCells,-1], \a idsInPflPerType [[0,1,2]] and \a idsPerType is empty <br>
221  * 
222  *  \b Example2: <br>
223  *          - Before \a this has 3 cells \a profile contains [1,2]
224  *          - After \a code contains [NORM_...,nbCells,0], \a idsInPflPerType [[0,1]] and \a idsPerType is [[1,2]] <br>
225
226  */
227 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
228 {
229   if(!profile)
230     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile is NULL !");
231   if(profile->getNumberOfComponents()!=1)
232     throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::splitProfilePerType : input profile should have exactly one component !");
233   int nbTuples=profile->getNumberOfTuples();
234   int nbOfCells=getNumberOfCells();
235   code.resize(3); idsInPflPerType.resize(1);
236   code[0]=(int)getTypeOfCell(0); code[1]=nbOfCells;
237   idsInPflPerType.resize(1);
238   if(profile->isIdentity() && nbTuples==nbOfCells)
239     {
240       code[2]=-1;
241       idsInPflPerType[0]=const_cast<DataArrayInt *>(profile); idsInPflPerType[0]->incrRef();
242       idsPerType.clear(); 
243     }
244   code[2]=0;
245   profile->checkAllIdsInRange(0,nbOfCells);
246   idsPerType.resize(1);
247   idsPerType[0]=const_cast<DataArrayInt *>(profile); idsPerType[0]->incrRef();
248   idsInPflPerType[0]=DataArrayInt::Range(0,nbTuples,1);
249 }
250
251 /*!
252  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
253  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
254  * delete this array using decrRef() as it is no more needed. 
255  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
256  */
257 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
258 {
259   int meshDim=getMeshDimension();
260   MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
261   DataArrayDouble *coords=getCoordinatesAndOwner();
262   ret->setCoords(coords);
263   coords->decrRef();
264   switch(meshDim)
265     {
266     case 1:
267       fill1DUnstructuredMesh(ret);
268       break;
269     case 2:
270       fill2DUnstructuredMesh(ret);
271       break;
272     case 3:
273       fill3DUnstructuredMesh(ret);
274       break;
275     default:
276       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
277     };
278   return ret;
279 }
280
281 /*!
282  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
283  * The cells to include to the
284  * result mesh are specified by an array of cell ids.
285  *  \param [in] start - an array of cell ids to include to the result mesh.
286  *  \param [in] end - specifies the end of the array \a start, so that
287  *              the last value of \a start is \a end[ -1 ].
288  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
289  *         delete this mesh using decrRef() as it is no more needed. 
290  */
291 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
292 {
293   MEDCouplingUMesh *um=buildUnstructured();
294   MEDCouplingMesh *ret=um->buildPart(start,end);
295   um->decrRef();
296   return ret;
297 }
298
299 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
300 {
301   MEDCouplingUMesh *um=buildUnstructured();
302   MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
303   um->decrRef();
304   return ret;
305 }
306
307 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
308 {
309   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
310 }
311
312 /*!
313  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
314  * 2D mesh. The computed vectors have 3 components and are normalized.
315  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
316  *          cells and one time. The caller is to delete this field using decrRef() as
317  *          it is no more needed.
318  *  \throw If \a this->getMeshDimension() != 2.
319  */
320 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
321 {
322   if(getMeshDimension()!=2)
323     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
324   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
325   DataArrayDouble *array=DataArrayDouble::New();
326   int nbOfCells=getNumberOfCells();
327   array->alloc(nbOfCells,3);
328   double *vals=array->getPointer();
329   for(int i=0;i<nbOfCells;i++)
330     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
331   ret->setArray(array);
332   array->decrRef();
333   ret->setMesh(this);
334   return ret;
335 }
336
337 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
338 {
339   int nbOfCells=-1;
340   getNodeGridStructure(&nbOfCells);
341   nbOfCells--;
342   DataArrayInt *connI=DataArrayInt::New();
343   connI->alloc(nbOfCells+1,1);
344   int *ci=connI->getPointer();
345   DataArrayInt *conn=DataArrayInt::New();
346   conn->alloc(3*nbOfCells,1);
347   ci[0]=0;
348   int *cp=conn->getPointer();
349   for(int i=0;i<nbOfCells;i++)
350     {
351       cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
352       cp[3*i+1]=i;
353       cp[3*i+2]=i+1;
354       ci[i+1]=3*(i+1);
355     }
356   m->setConnectivity(conn,connI,true);
357   conn->decrRef();
358   connI->decrRef();
359 }
360
361 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
362 {
363   int ns[2];
364   getNodeGridStructure(ns);
365   int n1=ns[0]-1;
366   int n2=ns[1]-1;
367   DataArrayInt *connI=DataArrayInt::New();
368   connI->alloc(n1*n2+1,1);
369   int *ci=connI->getPointer();
370   DataArrayInt *conn=DataArrayInt::New();
371   conn->alloc(5*n1*n2,1);
372   ci[0]=0;
373   int *cp=conn->getPointer();
374   int pos=0;
375   for(int j=0;j<n2;j++)
376     for(int i=0;i<n1;i++,pos++)
377       {
378         cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
379         cp[5*pos+1]=i+1+j*(n1+1);
380         cp[5*pos+2]=i+j*(n1+1);
381         cp[5*pos+3]=i+(j+1)*(n1+1);
382         cp[5*pos+4]=i+1+(j+1)*(n1+1);
383         ci[pos+1]=5*(pos+1);
384     }
385   m->setConnectivity(conn,connI,true);
386   conn->decrRef();
387   connI->decrRef();
388 }
389
390 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
391 {
392   int ns[3];
393   getNodeGridStructure(ns);
394   int n1=ns[0]-1;
395   int n2=ns[1]-1;
396   int n3=ns[2]-1;
397   DataArrayInt *connI=DataArrayInt::New();
398   connI->alloc(n1*n2*n3+1,1);
399   int *ci=connI->getPointer();
400   DataArrayInt *conn=DataArrayInt::New();
401   conn->alloc(9*n1*n2*n3,1);
402   ci[0]=0;
403   int *cp=conn->getPointer();
404   int pos=0;
405   for(int k=0;k<n3;k++)
406     for(int j=0;j<n2;j++)
407       for(int i=0;i<n1;i++,pos++)
408         {
409           cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
410           int tmp=(n1+1)*(n2+1);
411           cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
412           cp[9*pos+2]=i+j*(n1+1)+k*tmp;
413           cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
414           cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
415           cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
416           cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
417           cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
418           cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
419           ci[pos+1]=9*(pos+1);
420         }
421   m->setConnectivity(conn,connI,true);
422   conn->decrRef();
423   connI->decrRef();
424 }
425
426 /*!
427  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
428  * ( i + 1 )-th nodes along X axis etc.
429  *  \param [in] i - a index of node coordinates array along X axis.
430  *  \param [in] j - a index of node coordinates array along Y axis.
431  *  \param [in] k - a index of node coordinates array along Z axis.
432  *  \return int - a cell id in \a this mesh.
433  */
434 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
435 {
436   int tmp[3]={i,j,k};
437   int tmp2[3];
438   int meshDim=getMeshDimension();
439   getSplitCellValues(tmp2);
440   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
441   return std::accumulate(tmp,tmp+meshDim,0);
442 }
443
444 /*!
445  * Returns a node id by its (i,j,k) index.
446  *  \param [in] i - a index of node coordinates array along X axis.
447  *  \param [in] j - a index of node coordinates array along Y axis.
448  *  \param [in] k - a index of node coordinates array along Z axis.
449  *  \return int - a node id in \a this mesh.
450  */
451 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
452 {
453   int tmp[3]={i,j,k};
454   int tmp2[3];
455   int meshDim=getMeshDimension();
456   getSplitNodeValues(tmp2);
457   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
458   return std::accumulate(tmp,tmp+meshDim,0);
459 }
460
461 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
462 {
463   int work=nodeId;
464   for(int i=meshDim-1;i>=0;i--)
465     {
466       int pos=work/split[i];
467       work=work%split[i];
468       res[i]=pos;
469     }
470 }