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