Salome HOME
Merge from V6_main (04/10/2012)
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingGaussLocalization.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 "MEDCouplingGaussLocalization.hxx"
22 #include "CellModel.hxx"
23
24 #include <cmath>
25 #include <numeric>
26 #include <sstream>
27 #include <iterator>
28 #include <algorithm>
29
30 ParaMEDMEM::MEDCouplingGaussLocalization::MEDCouplingGaussLocalization(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
31                                                                        const std::vector<double>& gsCoo, const std::vector<double>& w) throw(INTERP_KERNEL::Exception)
32 try:_type(type),_ref_coord(refCoo),_gauss_coord(gsCoo),_weight(w)
33   {
34     checkCoherency();
35   }
36 catch(INTERP_KERNEL::Exception& e)
37   {
38     _type=INTERP_KERNEL::NORM_ERROR;
39     _ref_coord.clear();
40     _gauss_coord.clear();
41     _weight.clear();
42     throw e;
43   }
44
45 void ParaMEDMEM::MEDCouplingGaussLocalization::checkCoherency() const throw(INTERP_KERNEL::Exception)
46 {
47   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
48   int nbNodes=cm.getNumberOfNodes();
49   int dim=cm.getDimension();
50   if(!cm.isDynamic())
51     {
52       if((int)_ref_coord.size()!=nbNodes*dim)
53         {
54           std::ostringstream oss; oss << "Invalid size of refCoo : expecting to be : " << nbNodes << " (nbNodePerCell) * " << dim << " (dim) !";
55           throw INTERP_KERNEL::Exception(oss.str().c_str());
56         }
57     }
58   if(_gauss_coord.size()!=dim*_weight.size())
59     {
60        std::ostringstream oss; oss << "Invalid gsCoo size and weight size : gsCoo.size() must be equal to _weight.size() * " << dim << " (dim) !";
61        throw INTERP_KERNEL::Exception(oss.str().c_str());
62     }
63 }
64
65 int ParaMEDMEM::MEDCouplingGaussLocalization::getDimension() const
66 {
67   if(_weight.empty())
68     return -1;
69   return (int)_gauss_coord.size()/(int)_weight.size();
70 }
71
72 int ParaMEDMEM::MEDCouplingGaussLocalization::getNumberOfPtsInRefCell() const
73 {
74   int dim=getDimension();
75   if(dim==0)
76     return -1;
77   return (int)_ref_coord.size()/dim;
78 }
79
80 std::string ParaMEDMEM::MEDCouplingGaussLocalization::getStringRepr() const
81 {
82   std::ostringstream oss;
83   oss << "CellType : " << INTERP_KERNEL::CellModel::GetCellModel(_type).getRepr() << std::endl;
84   oss << "Ref coords : "; std::copy(_ref_coord.begin(),_ref_coord.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
85   oss << "Localization coords : "; std::copy(_gauss_coord.begin(),_gauss_coord.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
86   oss << "Weight : "; std::copy(_weight.begin(),_weight.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
87   return oss.str();
88 }
89
90 bool ParaMEDMEM::MEDCouplingGaussLocalization::isEqual(const MEDCouplingGaussLocalization& other, double eps) const
91 {
92   if(_type!=other._type)
93     return false;
94   if(!AreAlmostEqual(_ref_coord,other._ref_coord,eps))
95     return false;
96   if(!AreAlmostEqual(_gauss_coord,other._gauss_coord,eps))
97     return false;
98   if(!AreAlmostEqual(_weight,other._weight,eps))
99     return false;
100   return true;
101 }
102
103 double ParaMEDMEM::MEDCouplingGaussLocalization::getRefCoord(int ptIdInCell, int comp) const throw(INTERP_KERNEL::Exception)
104 {
105   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
106   int nbNodes=cm.getNumberOfNodes();
107   int dim=cm.getDimension();
108   if(ptIdInCell<0 || ptIdInCell>=nbNodes)
109     throw INTERP_KERNEL::Exception("ptIdInCell specified is invalid : must be in [0;nbNodesPerCell) !");
110   if(comp<0 || comp>=dim)
111     throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
112   return _ref_coord[ptIdInCell*dim+comp];
113 }
114
115 double ParaMEDMEM::MEDCouplingGaussLocalization::getGaussCoord(int gaussPtIdInCell, int comp) const throw(INTERP_KERNEL::Exception)
116 {
117   int dim=checkCoherencyOfRequest(gaussPtIdInCell,comp);
118   return _gauss_coord[gaussPtIdInCell*dim+comp];
119 }
120
121 double ParaMEDMEM::MEDCouplingGaussLocalization::getWeight(int gaussPtIdInCell, double newVal) const throw(INTERP_KERNEL::Exception)
122 {
123   checkCoherencyOfRequest(gaussPtIdInCell,0);
124   return _weight[gaussPtIdInCell];
125 }
126
127 /*!
128  * Completely useless method for end user. Only for CORBA MPI serialization/unserialization.
129  * push at the end of tinyInfo its basic serialization info. The size of pushed data is always the same.
130  * @param tinyInfo inout parameter.
131  */
132 void ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo(std::vector<int>& tinyInfo) const
133 {
134   tinyInfo.push_back((int)_type);
135   tinyInfo.push_back(getNumberOfPtsInRefCell());
136   tinyInfo.push_back(getNumberOfGaussPt());
137 }
138
139 /*!
140  * Completely useless method for end user. Only for CORBA MPI serialization/unserialization.
141  * push at the end of tinyInfo its basic serialization info. The size of pushed data is \b NOT always the same contrary to pushTinySerializationIntInfo.
142  * @param tinyInfo inout parameter.
143  */
144 void ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationDblInfo(std::vector<double>& tinyInfo) const
145 {
146   tinyInfo.insert(tinyInfo.end(),_ref_coord.begin(),_ref_coord.end());
147   tinyInfo.insert(tinyInfo.end(),_gauss_coord.begin(),_gauss_coord.end());
148   tinyInfo.insert(tinyInfo.end(),_weight.begin(),_weight.end());
149 }
150
151 /*!
152  * This method operates the exact inverse operation than ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationDblInfo method. This is one of the last step of unserialization process.
153  * This method should be called on an object resized by buildNewInstanceFromTinyInfo static method.
154  * This method takes in argument a pointer 'vals' that point to the begin of double data pushed remotely by pushTinySerializationDblInfo method.
155  * This method returns the pointer 'vals' with an offset of size what it has been read in this method.
156  */
157 const double *ParaMEDMEM::MEDCouplingGaussLocalization::fillWithValues(const double *vals)
158 {
159   const double *work=vals;
160   std::copy(work,work+_ref_coord.size(),_ref_coord.begin());
161   work+=_ref_coord.size();
162   std::copy(work,work+_gauss_coord.size(),_gauss_coord.begin());
163   work+=_gauss_coord.size();
164   std::copy(work,work+_weight.size(),_weight.begin());
165   work+=_weight.size();
166   return work;
167 }
168
169 /*!
170  * This method sets the comp_th component of ptIdInCell_th point coordinate of reference element of type this->_type.
171  * @throw if not 0<=ptIdInCell<nbOfNodePerCell or if not 0<=comp<dim
172  */
173 void ParaMEDMEM::MEDCouplingGaussLocalization::setRefCoord(int ptIdInCell, int comp, double newVal) throw(INTERP_KERNEL::Exception)
174 {
175   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
176   int nbNodes=cm.getNumberOfNodes();
177   int dim=cm.getDimension();
178   if(ptIdInCell<0 || ptIdInCell>=nbNodes)
179     throw INTERP_KERNEL::Exception("ptIdInCell specified is invalid : must be in [0;nbNodesPerCell) !");
180   if(comp<0 || comp>=dim)
181     throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
182   _ref_coord[ptIdInCell*dim+comp]=newVal;
183 }
184
185 void ParaMEDMEM::MEDCouplingGaussLocalization::setGaussCoord(int gaussPtIdInCell, int comp, double newVal) throw(INTERP_KERNEL::Exception)
186 {
187   int dim=checkCoherencyOfRequest(gaussPtIdInCell,comp);
188   _gauss_coord[gaussPtIdInCell*dim+comp]=newVal;
189 }
190
191 void ParaMEDMEM::MEDCouplingGaussLocalization::setWeight(int gaussPtIdInCell, double newVal) throw(INTERP_KERNEL::Exception)
192 {
193   checkCoherencyOfRequest(gaussPtIdInCell,0);
194   _weight[gaussPtIdInCell]=newVal;
195 }
196
197 /*!
198  * The format of 'tinyData' parameter is the same than pushed in method ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo.
199  */
200 ParaMEDMEM::MEDCouplingGaussLocalization ParaMEDMEM::MEDCouplingGaussLocalization::BuildNewInstanceFromTinyInfo(int dim, const std::vector<int>& tinyData)
201 {
202   std::vector<double> v1(dim*tinyData[1]),v2(dim*tinyData[2]),v3(tinyData[2]);
203   return ParaMEDMEM::MEDCouplingGaussLocalization((INTERP_KERNEL::NormalizedCellType)tinyData[0],v1,v2,v3);
204 }
205
206 int ParaMEDMEM::MEDCouplingGaussLocalization::checkCoherencyOfRequest(int gaussPtIdInCell, int comp) const throw(INTERP_KERNEL::Exception)
207 {
208   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
209   int dim=cm.getDimension();
210   int nbGsPts=getNumberOfGaussPt();
211   if(gaussPtIdInCell<0 || gaussPtIdInCell>=nbGsPts)
212     throw INTERP_KERNEL::Exception("gaussPtIdInCell specified is invalid : must be in [0:nbGsPts) !");
213   if(comp<0 || comp>=dim)
214     throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
215   return dim;
216 }
217
218 bool ParaMEDMEM::MEDCouplingGaussLocalization::AreAlmostEqual(const std::vector<double>& v1, const std::vector<double>& v2, double eps)
219 {
220   std::size_t sz=v1.size();
221   if(sz!=v2.size())
222     return false;
223   std::vector<double> tmp(sz);
224   std::transform(v1.begin(),v1.end(),v2.begin(),tmp.begin(),std::minus<double>());
225   std::transform(tmp.begin(),tmp.end(),tmp.begin(),std::ptr_fun<double,double>(fabs));
226   return *std::max_element(tmp.begin(),tmp.end())<eps;
227 }