Salome HOME
Merge from V6_main 15/03/2013
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingStructuredMesh.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 // 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 MEDCouplingUMesh *MEDCouplingStructuredMesh::buildUnstructured() const throw(INTERP_KERNEL::Exception)
197 {
198   int meshDim=getMeshDimension();
199   MEDCouplingUMesh *ret=MEDCouplingUMesh::New(getName(),meshDim);
200   DataArrayDouble *coords=getCoordinatesAndOwner();
201   ret->setCoords(coords);
202   coords->decrRef();
203   switch(meshDim)
204     {
205     case 1:
206       fill1DUnstructuredMesh(ret);
207       break;
208     case 2:
209       fill2DUnstructuredMesh(ret);
210       break;
211     case 3:
212       fill3DUnstructuredMesh(ret);
213       break;
214     default:
215       throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::buildUnstructured : big problem spacedim must be in 1,2 or 3 !");
216     };
217   return ret;
218 }
219
220 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPart(const int *start, const int *end) const
221 {
222   MEDCouplingUMesh *um=buildUnstructured();
223   MEDCouplingMesh *ret=um->buildPart(start,end);
224   um->decrRef();
225   return ret;
226 }
227
228 MEDCouplingMesh *MEDCouplingStructuredMesh::buildPartAndReduceNodes(const int *start, const int *end, DataArrayInt*& arr) const
229 {
230   MEDCouplingUMesh *um=buildUnstructured();
231   MEDCouplingMesh *ret=um->buildPartAndReduceNodes(start,end,arr);
232   um->decrRef();
233   return ret;
234 }
235
236 DataArrayInt *MEDCouplingStructuredMesh::simplexize(int policy) throw(INTERP_KERNEL::Exception)
237 {
238   throw INTERP_KERNEL::Exception("MEDCouplingStructuredMesh::simplexize : not available for Cartesian mesh !");
239 }
240
241 MEDCouplingFieldDouble *MEDCouplingStructuredMesh::buildOrthogonalField() const
242 {
243   if(getMeshDimension()!=2)
244     throw INTERP_KERNEL::Exception("Expected a MEDCouplingStructuredMesh with meshDim == 2 !");
245   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(ON_CELLS,NO_TIME);
246   DataArrayDouble *array=DataArrayDouble::New();
247   int nbOfCells=getNumberOfCells();
248   array->alloc(nbOfCells,3);
249   double *vals=array->getPointer();
250   for(int i=0;i<nbOfCells;i++)
251     { vals[3*i]=0.; vals[3*i+1]=0.; vals[3*i+2]=1.; }
252   ret->setArray(array);
253   array->decrRef();
254   ret->setMesh(this);
255   return ret;
256 }
257
258 void MEDCouplingStructuredMesh::fill1DUnstructuredMesh(MEDCouplingUMesh *m) const
259 {
260   int nbOfCells=-1;
261   getNodeGridStructure(&nbOfCells);
262   nbOfCells--;
263   DataArrayInt *connI=DataArrayInt::New();
264   connI->alloc(nbOfCells+1,1);
265   int *ci=connI->getPointer();
266   DataArrayInt *conn=DataArrayInt::New();
267   conn->alloc(3*nbOfCells,1);
268   ci[0]=0;
269   int *cp=conn->getPointer();
270   for(int i=0;i<nbOfCells;i++)
271     {
272       cp[3*i]=(int)INTERP_KERNEL::NORM_SEG2;
273       cp[3*i+1]=i;
274       cp[3*i+2]=i+1;
275       ci[i+1]=3*(i+1);
276     }
277   m->setConnectivity(conn,connI,true);
278   conn->decrRef();
279   connI->decrRef();
280 }
281
282 void MEDCouplingStructuredMesh::fill2DUnstructuredMesh(MEDCouplingUMesh *m) const
283 {
284   int ns[2];
285   getNodeGridStructure(ns);
286   int n1=ns[0]-1;
287   int n2=ns[1]-1;
288   DataArrayInt *connI=DataArrayInt::New();
289   connI->alloc(n1*n2+1,1);
290   int *ci=connI->getPointer();
291   DataArrayInt *conn=DataArrayInt::New();
292   conn->alloc(5*n1*n2,1);
293   ci[0]=0;
294   int *cp=conn->getPointer();
295   int pos=0;
296   for(int j=0;j<n2;j++)
297     for(int i=0;i<n1;i++,pos++)
298       {
299         cp[5*pos]=(int)INTERP_KERNEL::NORM_QUAD4;
300         cp[5*pos+1]=i+1+j*(n1+1);
301         cp[5*pos+2]=i+j*(n1+1);
302         cp[5*pos+3]=i+(j+1)*(n1+1);
303         cp[5*pos+4]=i+1+(j+1)*(n1+1);
304         ci[pos+1]=5*(pos+1);
305     }
306   m->setConnectivity(conn,connI,true);
307   conn->decrRef();
308   connI->decrRef();
309 }
310
311 void MEDCouplingStructuredMesh::fill3DUnstructuredMesh(MEDCouplingUMesh *m) const
312 {
313   int ns[3];
314   getNodeGridStructure(ns);
315   int n1=ns[0]-1;
316   int n2=ns[1]-1;
317   int n3=ns[2]-1;
318   DataArrayInt *connI=DataArrayInt::New();
319   connI->alloc(n1*n2*n3+1,1);
320   int *ci=connI->getPointer();
321   DataArrayInt *conn=DataArrayInt::New();
322   conn->alloc(9*n1*n2*n3,1);
323   ci[0]=0;
324   int *cp=conn->getPointer();
325   int pos=0;
326   for(int k=0;k<n3;k++)
327     for(int j=0;j<n2;j++)
328       for(int i=0;i<n1;i++,pos++)
329         {
330           cp[9*pos]=(int)INTERP_KERNEL::NORM_HEXA8;
331           int tmp=(n1+1)*(n2+1);
332           cp[9*pos+1]=i+1+j*(n1+1)+k*tmp;
333           cp[9*pos+2]=i+j*(n1+1)+k*tmp;
334           cp[9*pos+3]=i+(j+1)*(n1+1)+k*tmp;
335           cp[9*pos+4]=i+1+(j+1)*(n1+1)+k*tmp;
336           cp[9*pos+5]=i+1+j*(n1+1)+(k+1)*tmp;
337           cp[9*pos+6]=i+j*(n1+1)+(k+1)*tmp;
338           cp[9*pos+7]=i+(j+1)*(n1+1)+(k+1)*tmp;
339           cp[9*pos+8]=i+1+(j+1)*(n1+1)+(k+1)*tmp;
340           ci[pos+1]=9*(pos+1);
341         }
342   m->setConnectivity(conn,connI,true);
343   conn->decrRef();
344   connI->decrRef();
345 }
346
347 int MEDCouplingStructuredMesh::getCellIdFromPos(int i, int j, int k) const
348 {
349   int tmp[3]={i,j,k};
350   int tmp2[3];
351   int meshDim=getMeshDimension();
352   getSplitCellValues(tmp2);
353   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
354   return std::accumulate(tmp,tmp+meshDim,0);
355 }
356
357 int MEDCouplingStructuredMesh::getNodeIdFromPos(int i, int j, int k) const
358 {
359   int tmp[3]={i,j,k};
360   int tmp2[3];
361   int meshDim=getMeshDimension();
362   getSplitNodeValues(tmp2);
363   std::transform(tmp,tmp+meshDim,tmp2,tmp,std::multiplies<int>());
364   return std::accumulate(tmp,tmp+meshDim,0);
365 }
366
367 void MEDCouplingStructuredMesh::GetPosFromId(int nodeId, int meshDim, const int *split, int *res)
368 {
369   int work=nodeId;
370   for(int i=meshDim-1;i>=0;i--)
371     {
372       int pos=work/split[i];
373       work=work%split[i];
374       res[i]=pos;
375     }
376 }