Salome HOME
Merge from MrgToV7main1804
[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 void MEDCouplingStructuredMesh::getNodeIdsOfCell(int cellId, std::vector<int>& conn) const
113 {
114   int meshDim=getMeshDimension();
115   int tmpCell[3],tmpNode[3];
116   getSplitCellValues(tmpCell);
117   getSplitNodeValues(tmpNode);
118   int tmp2[3];
119   GetPosFromId(cellId,meshDim,tmpCell,tmp2);
120   switch(meshDim)
121     {
122     case 1:
123       conn.push_back(tmp2[0]); conn.push_back(tmp2[0]+1);
124       break;
125     case 2:
126       conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]); conn.push_back(tmp2[1]*tmpCell[1]+tmp2[0]+1);
127       conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]+1); conn.push_back((tmp2[1]+1)*(tmpCell[1]+1)+tmp2[0]);
128       break;
129     case 3:
130       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]);
131       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]);
132       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]);
133       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]);
134       break;
135     default:
136       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::getNodeIdsOfCell : big problem spacedim must be in 1,2 or 3 !");
137     };
138 }
139
140 /*!
141  * See MEDCouplingUMesh::getDistributionOfTypes for more information
142  */
143 std::vector<int> MEDCouplingStructuredMesh::getDistributionOfTypes() const throw(INTERP_KERNEL::Exception)
144 {
145   //only one type of cell
146   std::vector<int> ret(3);
147   ret[0]=getTypeOfCell(0);
148   ret[1]=getNumberOfCells();
149   ret[2]=0; //ret[3*k+2]==0 because it has no sense here
150   return ret;
151 }
152
153 /*!
154  * See MEDCouplingUMesh::checkTypeConsistencyAndContig for more information
155  */
156 DataArrayInt *MEDCouplingStructuredMesh::checkTypeConsistencyAndContig(const std::vector<int>& code, const std::vector<const DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
157 {
158   if(code.empty())
159     throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code is empty, should not !");
160   std::size_t sz=code.size();
161   if(sz!=3)
162     throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code should be of size 3 exactly !");
163
164   int nbCells=getNumberOfCellsWithType((INTERP_KERNEL::NormalizedCellType)code[0]);
165   if(code[2]==-1)
166     {
167       if(code[1]==nbCells)
168         return 0;
169       else
170         throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : number of cells mismatch !");
171     }
172   else
173     {
174       if(code[2]<-1) 
175         throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]<-1 mismatch !");
176       if(code[2]>=(int)idsPerType.size()) 
177         throw INTERP_KERNEL::Exception("MEDCouplingCurveLinearMesh::checkTypeConsistencyAndContig : code[2]>size idsPerType !");
178       return idsPerType[code[2]]->deepCpy();
179     }
180 }
181
182 /*!
183  * See MEDCouplingUMesh::splitProfilePerType for more information
184  */
185 void MEDCouplingStructuredMesh::splitProfilePerType(const DataArrayInt *profile, std::vector<int>& code, std::vector<DataArrayInt *>& idsInPflPerType, std::vector<DataArrayInt *>& idsPerType) const throw(INTERP_KERNEL::Exception)
186 {
187   int nbCells=getNumberOfCells();
188   code.resize(3);
189   code[0]=(int)getTypeOfCell(0);
190   code[1]=nbCells;
191   code[2]=0;
192   idsInPflPerType.push_back(profile->deepCpy());
193   idsPerType.push_back(profile->deepCpy());
194 }
195
196 /*!
197  * Creates a new unstructured mesh (MEDCouplingUMesh) from \a this structured one.
198  *  \return MEDCouplingUMesh * - a new instance of MEDCouplingUMesh. The caller is to
199  * delete this array using decrRef() as it is no more needed. 
200  *  \throw If \a this->getMeshDimension() is not among [1,2,3].
201  */
202 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
203 {
204   int meshDim=getMeshDimension();
205   MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
206   DataArrayDouble *coords=getCoordinatesAndOwner();
207   ret->setCoords(coords);
208   coords->decrRef();
209   switch(meshDim)
210     {
211     case 1:
212       fill1DUnstructuredMesh(ret);
213       break;
214     case 2:
215       fill2DUnstructuredMesh(ret);
216       break;
217     case 3:
218       fill3DUnstructuredMesh(ret);
219       break;
220     default:
221       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
222     };
223   return ret;
224 }
225
226 /*!
227  * Creates a new MEDCouplingUMesh containing a part of cells of \a this mesh.
228  * The cells to include to the
229  * result mesh are specified by an array of cell ids.
230  *  \param [in] start - an array of cell ids to include to the result mesh.
231  *  \param [in] end - specifies the end of the array \a start, so that
232  *              the last value of \a start is \a end[ -1 ].
233  *  \return MEDCouplingMesh * - a new instance of MEDCouplingUMesh. The caller is to
234  *         delete this mesh using decrRef() as it is no more needed. 
235  */
236 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
237 {
238   MEDCouplingUMesh *um=buildUnstructured();
239   MEDCouplingMesh *ret=um->buildPart(start,end);
240   um->decrRef();
241   return ret;
242 }
243
244 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
245 {
246   MEDCouplingUMesh *um=buildUnstructured();
247   MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
248   um->decrRef();
249   return ret;
250 }
251
252 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
253 {
254   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
255 }
256
257 /*!
258  * Returns a new MEDCouplingFieldDouble holding normal vectors to cells of \a this
259  * 2D mesh. The computed vectors have 3 components and are normalized.
260  *  \return MEDCouplingFieldDouble * - a new instance of MEDCouplingFieldDouble on
261  *          cells and one time. The caller is to delete this field using decrRef() as
262  *          it is no more needed.
263  *  \throw If \a this->getMeshDimension() != 2.
264  */
265 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
266 {
267   if(getMeshDimension()!=2)
268     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
269   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
270   DataArrayDouble *array=DataArrayDouble::New();
271   int nbOfCells=getNumberOfCells();
272   array->alloc(nbOfCells,3);
273   double *vals=array->getPointer();
274   for(int i=0;i<nbOfCells;i++)
275     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
276   ret->setArray(array);
277   array->decrRef();
278   ret->setMesh(this);
279   return ret;
280 }
281
282 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
283 {
284   int nbOfCells=-1;
285   getNodeGridStructure(&nbOfCells);
286   nbOfCells--;
287   DataArrayInt *connI=DataArrayInt::New();
288   connI->alloc(nbOfCells+1,1);
289   int *ci=connI->getPointer();
290   DataArrayInt *conn=DataArrayInt::New();
291   conn->alloc(3*nbOfCells,1);
292   ci[0]=0;
293   int *cp=conn->getPointer();
294   for(int i=0;i<nbOfCells;i++)
295     {
296       cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
297       cp[3*i+1]=i;
298       cp[3*i+2]=i+1;
299       ci[i+1]=3*(i+1);
300     }
301   m->setConnectivity(conn,connI,true);
302   conn->decrRef();
303   connI->decrRef();
304 }
305
306 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
307 {
308   int ns[2];
309   getNodeGridStructure(ns);
310   int n1=ns[0]-1;
311   int n2=ns[1]-1;
312   DataArrayInt *connI=DataArrayInt::New();
313   connI->alloc(n1*n2+1,1);
314   int *ci=connI->getPointer();
315   DataArrayInt *conn=DataArrayInt::New();
316   conn->alloc(5*n1*n2,1);
317   ci[0]=0;
318   int *cp=conn->getPointer();
319   int pos=0;
320   for(int j=0;j<n2;j++)
321     for(int i=0;i<n1;i++,pos++)
322       {
323         cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
324         cp[5*pos+1]=i+1+j*(n1+1);
325         cp[5*pos+2]=i+j*(n1+1);
326         cp[5*pos+3]=i+(j+1)*(n1+1);
327         cp[5*pos+4]=i+1+(j+1)*(n1+1);
328         ci[pos+1]=5*(pos+1);
329     }
330   m->setConnectivity(conn,connI,true);
331   conn->decrRef();
332   connI->decrRef();
333 }
334
335 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
336 {
337   int ns[3];
338   getNodeGridStructure(ns);
339   int n1=ns[0]-1;
340   int n2=ns[1]-1;
341   int n3=ns[2]-1;
342   DataArrayInt *connI=DataArrayInt::New();
343   connI->alloc(n1*n2*n3+1,1);
344   int *ci=connI->getPointer();
345   DataArrayInt *conn=DataArrayInt::New();
346   conn->alloc(9*n1*n2*n3,1);
347   ci[0]=0;
348   int *cp=conn->getPointer();
349   int pos=0;
350   for(int k=0;k<n3;k++)
351     for(int j=0;j<n2;j++)
352       for(int i=0;i<n1;i++,pos++)
353         {
354           cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
355           int tmp=(n1+1)*(n2+1);
356           cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
357           cp[9*pos+2]=i+j*(n1+1)+k*tmp;
358           cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
359           cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
360           cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
361           cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
362           cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
363           cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
364           ci[pos+1]=9*(pos+1);
365         }
366   m->setConnectivity(conn,connI,true);
367   conn->decrRef();
368   connI->decrRef();
369 }
370
371 /*!
372  * Returns a cell id by its (i,j,k) index. The cell is located between the i-th and
373  * ( i + 1 )-th nodes along X axis etc.
374  *  \param [in] i - a index of node coordinates array along X axis.
375  *  \param [in] j - a index of node coordinates array along Y axis.
376  *  \param [in] k - a index of node coordinates array along Z axis.
377  *  \return int - a cell id in \a this mesh.
378  */
379 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
380 {
381   int tmp[3]={i,j,k};
382   int tmp2[3];
383   int meshDim=getMeshDimension();
384   getSplitCellValues(tmp2);
385   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
386   return std::accumulate(tmp,tmp+meshDim,0);
387 }
388
389 /*!
390  * Returns a node id by its (i,j,k) index.
391  *  \param [in] i - a index of node coordinates array along X axis.
392  *  \param [in] j - a index of node coordinates array along Y axis.
393  *  \param [in] k - a index of node coordinates array along Z axis.
394  *  \return int - a node id in \a this mesh.
395  */
396 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
397 {
398   int tmp[3]={i,j,k};
399   int tmp2[3];
400   int meshDim=getMeshDimension();
401   getSplitNodeValues(tmp2);
402   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
403   return std::accumulate(tmp,tmp+meshDim,0);
404 }
405
406 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
407 {
408   int work=nodeId;
409   for(int i=meshDim-1;i>=0;i--)
410     {
411       int pos=work/split[i];
412       work=work%split[i];
413       res[i]=pos;
414     }
415 }