Salome HOME
Fix compilation error due API modification (returned type) of ReadField
[modules/med.git] / src / MEDCalculator / MEDCalculatorDBSliceField.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 "MEDCalculatorDBSliceField.hxx"
22 #include "MEDCalculatorDBRangeSelection.hxx"
23
24 #include "MEDLoader.hxx"
25
26 #include "MEDCouplingFieldDouble.hxx"
27 #include "MCAuto.hxx"
28
29 using namespace MEDCoupling;
30
31 MEDCalculatorDBSliceField::MEDCalculatorDBSliceField(int iter, int order):_iteration(iter),_order(order),_field(0),_work(0)
32 {
33 }
34
35 MEDCalculatorDBSliceField::MEDCalculatorDBSliceField(MEDCouplingFieldDouble *f):_field(f),_work(0)
36 {
37 }
38
39 std::size_t MEDCalculatorDBSliceField::getHeapMemorySizeWithoutChildren() const
40 {
41   return 0;
42 }
43
44 std::vector<const BigMemoryObject *> MEDCalculatorDBSliceField::getDirectChildrenWithNull() const
45 {
46   return std::vector<const BigMemoryObject *>();
47 }
48
49 void MEDCalculatorDBSliceField::setField(MEDCouplingFieldDouble *f) const
50 {
51   if(_field!=f)
52     {
53       if(_field)
54         _field->decrRef();
55       _field=f;
56     }
57 }
58
59 void MEDCalculatorDBSliceField::setName(const char *name)
60 {
61   _field->setName(name);
62 }
63
64 void MEDCalculatorDBSliceField::setDescription(const char *descr)
65 {
66   _field->setDescription(descr);
67 }
68
69 void MEDCalculatorDBSliceField::write(const char *fName, const std::string& n, const std::string& d) const throw(INTERP_KERNEL::Exception)
70 {
71   std::string kn=_field->getName();
72   std::string kd=_field->getDescription();
73   MEDCouplingFieldDouble *myF=const_cast<MEDCouplingFieldDouble *>(_field);
74   myF->setName(n.c_str());
75   myF->setDescription(d.c_str());
76   WriteFieldUsingAlreadyWrittenMesh(fName,_field);
77   myF->setName(kn.c_str());
78   myF->setDescription(kd.c_str());
79 }
80
81 const MEDCouplingMesh *MEDCalculatorDBSliceField::getMesh(TypeOfField type, const std::string& fname, const std::string& mname, const std::string& fieldName) const
82 {
83   MEDCouplingFieldDouble *f=getField(type,fname,mname,fieldName);
84   return f->getMesh();
85 }
86
87 MEDCalculatorDBSliceField::~MEDCalculatorDBSliceField()
88 {
89   if(_field)
90     _field->decrRef();
91   if(_work)
92     _work->decrRef();
93 }
94
95 MEDCouplingFieldDouble *MEDCalculatorDBSliceField::getField(TypeOfField type, const std::string& fname, const std::string& mname, const std::string& fieldName) const
96 {
97   if(!_field)
98     {
99       MCAuto<MEDCouplingFieldDouble> tmp(ReadField(type,fname.c_str(),mname.c_str(),0,fieldName.c_str(),_iteration,_order));
100       _field=tmp.retn();
101     }
102   return _field;
103 }
104
105 /*!
106  * _field is expected to be already loaded !
107  */
108 MEDCouplingFieldDouble *MEDCalculatorDBSliceField::getFieldWithoutQuestion(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const
109 {
110   std::vector<int> tIds=thisC.getIds(sizeCThis);
111   return _field->keepSelectedComponents(tIds);
112 }
113
114 MEDCouplingFieldDouble *MEDCalculatorDBSliceField::buildCstFromThis(double val, int nbOfComp, const MEDCouplingFieldDouble *f) const
115 {
116   MEDCouplingFieldDouble *ret=MEDCouplingFieldDouble::New(f->getTypeOfField(),ONE_TIME);
117   ret->setMesh(f->getMesh());
118   ret->applyFunc(nbOfComp,val);
119   ret->copyTinyAttrFrom(f);
120   return ret;
121 }
122
123 void MEDCalculatorDBSliceField::assign(const MEDCalculatorDBSliceField* other, int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
124                                     int sizeCOther, const MEDCalculatorDBRangeSelection& otherC)
125 {
126   std::vector<int> tIds=thisC.getIds(sizeCThis);
127   std::vector<int> oIds=otherC.getIds(sizeCOther);
128   MCAuto<MEDCouplingFieldDouble> f1=other->_field->keepSelectedComponents(oIds);
129   _field->setSelectedComponents(f1,tIds);
130 }
131
132 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::add(const MEDCalculatorDBSliceField* other, const DataArrayInt *cc, const DataArrayInt *nc,
133                                                     int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
134                                                     int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
135 {
136   if(cc!=0 || nc!=0)
137     throw INTERP_KERNEL::Exception("Slice::add : not implemented yet node/cell permutation !");
138   std::vector<int> tIds=thisC.getIds(sizeCThis);
139   std::vector<int> oIds=otherC.getIds(sizeCOther);
140   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
141   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
142   f2->setMesh(f1->getMesh());
143   MEDCouplingFieldDouble *f3=(*f1)+(*f2);
144   return new MEDCalculatorDBSliceField(f3);
145 }
146
147 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::substract(const MEDCalculatorDBSliceField* other, const DataArrayInt *cc, const DataArrayInt *nc,
148                                                           int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
149                                                           int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
150 {
151   if(cc!=0 || nc!=0)
152     throw INTERP_KERNEL::Exception("Slice::substract : not implemented yet node/cell permutation !");
153   std::vector<int> tIds=thisC.getIds(sizeCThis);
154   std::vector<int> oIds=otherC.getIds(sizeCOther);
155   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
156   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
157   f2->setMesh(f1->getMesh());
158   MEDCouplingFieldDouble *f3=(*f1)-(*f2);
159   return new MEDCalculatorDBSliceField(f3);
160 }
161
162 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::multiply(const MEDCalculatorDBSliceField* other, const DataArrayInt *cc, const DataArrayInt *nc,
163                                                          int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
164                                                          int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
165 {
166   if(cc!=0 || nc!=0)
167     throw INTERP_KERNEL::Exception("Slice::multiply : not implemented yet node/cell permutation !");
168   std::vector<int> tIds=thisC.getIds(sizeCThis);
169   std::vector<int> oIds=otherC.getIds(sizeCOther);
170   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
171   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
172   f2->setMesh(f1->getMesh());
173   MEDCouplingFieldDouble *f3=(*f1)*(*f2);
174   return new MEDCalculatorDBSliceField(f3);
175 }
176
177 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::divide(const MEDCalculatorDBSliceField* other, const DataArrayInt *cc, const DataArrayInt *nc,
178                                                        int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
179                                                        int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
180 {
181   if(cc!=0 || nc!=0)
182     throw INTERP_KERNEL::Exception("Slice::divide : not implemented yet node/cell permutation !");
183   std::vector<int> tIds=thisC.getIds(sizeCThis);
184   std::vector<int> oIds=otherC.getIds(sizeCOther);
185   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
186   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
187   f2->setMesh(f1->getMesh());
188   MEDCouplingFieldDouble *f3=(*f1)/(*f2);
189   return new MEDCalculatorDBSliceField(f3);
190 }
191
192 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::dot(const MEDCalculatorDBSliceField* other, int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
193                                                     int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
194 {
195   std::vector<int> tIds=thisC.getIds(sizeCThis);
196   std::vector<int> oIds=otherC.getIds(sizeCOther);
197   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
198   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
199   f2->setMesh(f1->getMesh());
200   MEDCouplingFieldDouble *f3=f1->dot(*f2);
201   return new MEDCalculatorDBSliceField(f3);
202 }
203
204 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::crossProduct(const MEDCalculatorDBSliceField* other, int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
205                                                              int sizeCOther, const MEDCalculatorDBRangeSelection& otherC) const
206 {
207   std::vector<int> tIds=thisC.getIds(sizeCThis);
208   std::vector<int> oIds=otherC.getIds(sizeCOther);
209   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
210   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
211   f2->setMesh(f1->getMesh());
212   MEDCouplingFieldDouble *f3=f1->crossProduct(*f2);
213   return new MEDCalculatorDBSliceField(f3);
214 }
215
216 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::doublyContractedProduct(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
217 {
218   std::vector<int> tIds=thisC.getIds(sizeCThis);
219   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
220   MEDCouplingFieldDouble *f2=f1->doublyContractedProduct();
221   return new MEDCalculatorDBSliceField(f2);
222 }
223
224 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::determinant(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
225 {
226   std::vector<int> tIds=thisC.getIds(sizeCThis);
227   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
228   MEDCouplingFieldDouble *f2=f1->determinant();
229   return new MEDCalculatorDBSliceField(f2);
230 }
231
232 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::eigenValues(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
233 {
234   std::vector<int> tIds=thisC.getIds(sizeCThis);
235   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
236   MEDCouplingFieldDouble *f2=f1->eigenValues();
237   return new MEDCalculatorDBSliceField(f2);
238 }
239
240 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::eigenVectors(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
241 {
242   std::vector<int> tIds=thisC.getIds(sizeCThis);
243   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
244   MEDCouplingFieldDouble *f2=f1->eigenVectors();
245   return new MEDCalculatorDBSliceField(f2);
246 }
247
248 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::inverse(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
249 {
250   std::vector<int> tIds=thisC.getIds(sizeCThis);
251   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
252   MEDCouplingFieldDouble *f2=f1->inverse();
253   return new MEDCalculatorDBSliceField(f2);
254 }
255
256 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::trace(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
257 {
258   std::vector<int> tIds=thisC.getIds(sizeCThis);
259   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
260   MEDCouplingFieldDouble *f2=f1->trace();
261   return new MEDCalculatorDBSliceField(f2);
262 }
263
264 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::deviator(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
265 {
266   std::vector<int> tIds=thisC.getIds(sizeCThis);
267   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
268   MEDCouplingFieldDouble *f2=f1->deviator();
269   return new MEDCalculatorDBSliceField(f2);
270 }
271
272 MEDCalculatorDBSliceField *MEDCalculatorDBSliceField::magnitude(int sizeCThis, const MEDCalculatorDBRangeSelection& thisC) const throw(INTERP_KERNEL::Exception)
273 {
274   std::vector<int> tIds=thisC.getIds(sizeCThis);
275   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
276   MEDCouplingFieldDouble *f2=f1->magnitude();
277   return new MEDCalculatorDBSliceField(f2);
278 }
279
280 void MEDCalculatorDBSliceField::applyFunc(const char *func, int sizeCThis, const MEDCalculatorDBRangeSelection& thisC)
281 {
282   std::vector<int> tIds=thisC.getIds(sizeCThis);
283   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
284   f1->applyFunc(func);
285   _field->setSelectedComponents(f1,tIds);
286 }
287
288 bool MEDCalculatorDBSliceField::isEqual(const MEDCalculatorDBSliceField* other, const DataArrayInt *cc, const DataArrayInt *nc,
289                                      int sizeCThis, const MEDCalculatorDBRangeSelection& thisC,
290                                      int sizeCOther, const MEDCalculatorDBRangeSelection& otherC, double prec) const
291 {
292   if(cc!=0 || nc!=0)
293     throw INTERP_KERNEL::Exception("Slice::isEqual : not implemented yet node/cell permutation !");
294   std::vector<int> tIds=thisC.getIds(sizeCThis);
295   std::vector<int> oIds=otherC.getIds(sizeCOther);
296   MCAuto<MEDCouplingFieldDouble> f1=_field->keepSelectedComponents(tIds);
297   MCAuto<MEDCouplingFieldDouble> f2=other->_field->keepSelectedComponents(oIds);
298   f2->setMesh(f1->getMesh());
299   return f1->isEqualWithoutConsideringStr(f2,0,prec);
300 }