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