1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCouplingGaussLocalization.hxx"
22 #include "CellModel.hxx"
30 ParaMEDMEM::MEDCouplingGaussLocalization::MEDCouplingGaussLocalization(INTERP_KERNEL::NormalizedCellType type, const std::vector<double>& refCoo,
31 const std::vector<double>& gsCoo, const std::vector<double>& w)
32 try:_type(type),_ref_coord(refCoo),_gauss_coord(gsCoo),_weight(w)
36 catch(INTERP_KERNEL::Exception& e)
38 _type=INTERP_KERNEL::NORM_ERROR;
45 ParaMEDMEM::MEDCouplingGaussLocalization::MEDCouplingGaussLocalization(INTERP_KERNEL::NormalizedCellType typ)
48 INTERP_KERNEL::CellModel::GetCellModel(_type);
50 catch(INTERP_KERNEL::Exception& e)
52 _type=INTERP_KERNEL::NORM_ERROR;
56 void ParaMEDMEM::MEDCouplingGaussLocalization::setType(INTERP_KERNEL::NormalizedCellType typ)
58 INTERP_KERNEL::CellModel::GetCellModel(typ);//throws if not found. This is a check
62 void ParaMEDMEM::MEDCouplingGaussLocalization::checkCoherency() const
64 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
65 int nbNodes=cm.getNumberOfNodes();
66 int dim=cm.getDimension();
69 if((int)_ref_coord.size()!=nbNodes*dim)
71 std::ostringstream oss; oss << "Invalid size of refCoo : expecting to be : " << nbNodes << " (nbNodePerCell) * " << dim << " (dim) !";
72 throw INTERP_KERNEL::Exception(oss.str().c_str());
75 if(_gauss_coord.size()!=dim*_weight.size())
77 std::ostringstream oss; oss << "Invalid gsCoo size and weight size : gsCoo.size() must be equal to _weight.size() * " << dim << " (dim) !";
78 throw INTERP_KERNEL::Exception(oss.str().c_str());
82 int ParaMEDMEM::MEDCouplingGaussLocalization::getDimension() const
86 return (int)_gauss_coord.size()/(int)_weight.size();
89 int ParaMEDMEM::MEDCouplingGaussLocalization::getNumberOfPtsInRefCell() const
91 int dim=getDimension();
94 return (int)_ref_coord.size()/dim;
97 std::string ParaMEDMEM::MEDCouplingGaussLocalization::getStringRepr() const
99 std::ostringstream oss;
100 oss << "CellType : " << INTERP_KERNEL::CellModel::GetCellModel(_type).getRepr() << std::endl;
101 oss << "Ref coords : "; std::copy(_ref_coord.begin(),_ref_coord.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
102 oss << "Localization coords : "; std::copy(_gauss_coord.begin(),_gauss_coord.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
103 oss << "Weight : "; std::copy(_weight.begin(),_weight.end(),std::ostream_iterator<double>(oss,", ")); oss << std::endl;
107 std::size_t ParaMEDMEM::MEDCouplingGaussLocalization::getMemorySize() const
110 ret+=_ref_coord.capacity()*sizeof(double);
111 ret+=_gauss_coord.capacity()*sizeof(double);
112 ret+=_weight.capacity()*sizeof(double);
116 bool ParaMEDMEM::MEDCouplingGaussLocalization::isEqual(const MEDCouplingGaussLocalization& other, double eps) const
118 if(_type!=other._type)
120 if(!AreAlmostEqual(_ref_coord,other._ref_coord,eps))
122 if(!AreAlmostEqual(_gauss_coord,other._gauss_coord,eps))
124 if(!AreAlmostEqual(_weight,other._weight,eps))
129 double ParaMEDMEM::MEDCouplingGaussLocalization::getRefCoord(int ptIdInCell, int comp) const
131 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
132 int nbNodes=cm.getNumberOfNodes();
133 int dim=cm.getDimension();
134 if(ptIdInCell<0 || ptIdInCell>=nbNodes)
135 throw INTERP_KERNEL::Exception("ptIdInCell specified is invalid : must be in [0;nbNodesPerCell) !");
136 if(comp<0 || comp>=dim)
137 throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
138 return _ref_coord[ptIdInCell*dim+comp];
141 double ParaMEDMEM::MEDCouplingGaussLocalization::getGaussCoord(int gaussPtIdInCell, int comp) const
143 int dim=checkCoherencyOfRequest(gaussPtIdInCell,comp);
144 return _gauss_coord[gaussPtIdInCell*dim+comp];
147 double ParaMEDMEM::MEDCouplingGaussLocalization::getWeight(int gaussPtIdInCell, double newVal) const
149 checkCoherencyOfRequest(gaussPtIdInCell,0);
150 return _weight[gaussPtIdInCell];
154 * Completely useless method for end user. Only for CORBA MPI serialization/unserialization.
155 * push at the end of tinyInfo its basic serialization info. The size of pushed data is always the same.
156 * @param tinyInfo inout parameter.
158 void ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo(std::vector<int>& tinyInfo) const
160 tinyInfo.push_back((int)_type);
161 tinyInfo.push_back(getNumberOfPtsInRefCell());
162 tinyInfo.push_back(getNumberOfGaussPt());
166 * Completely useless method for end user. Only for CORBA MPI serialization/unserialization.
167 * push at the end of tinyInfo its basic serialization info. The size of pushed data is \b NOT always the same contrary to pushTinySerializationIntInfo.
168 * @param tinyInfo inout parameter.
170 void ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationDblInfo(std::vector<double>& tinyInfo) const
172 tinyInfo.insert(tinyInfo.end(),_ref_coord.begin(),_ref_coord.end());
173 tinyInfo.insert(tinyInfo.end(),_gauss_coord.begin(),_gauss_coord.end());
174 tinyInfo.insert(tinyInfo.end(),_weight.begin(),_weight.end());
178 * This method operates the exact inverse operation than ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationDblInfo method. This is one of the last step of unserialization process.
179 * This method should be called on an object resized by buildNewInstanceFromTinyInfo static method.
180 * This method takes in argument a pointer 'vals' that point to the begin of double data pushed remotely by pushTinySerializationDblInfo method.
181 * This method returns the pointer 'vals' with an offset of size what it has been read in this method.
183 const double *ParaMEDMEM::MEDCouplingGaussLocalization::fillWithValues(const double *vals)
185 const double *work=vals;
186 std::copy(work,work+_ref_coord.size(),_ref_coord.begin());
187 work+=_ref_coord.size();
188 std::copy(work,work+_gauss_coord.size(),_gauss_coord.begin());
189 work+=_gauss_coord.size();
190 std::copy(work,work+_weight.size(),_weight.begin());
191 work+=_weight.size();
196 * This method sets the comp_th component of ptIdInCell_th point coordinate of reference element of type this->_type.
197 * @throw if not 0<=ptIdInCell<nbOfNodePerCell or if not 0<=comp<dim
199 void ParaMEDMEM::MEDCouplingGaussLocalization::setRefCoord(int ptIdInCell, int comp, double newVal)
201 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
202 int nbNodes=cm.getNumberOfNodes();
203 int dim=cm.getDimension();
204 if(ptIdInCell<0 || ptIdInCell>=nbNodes)
205 throw INTERP_KERNEL::Exception("ptIdInCell specified is invalid : must be in [0;nbNodesPerCell) !");
206 if(comp<0 || comp>=dim)
207 throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
208 _ref_coord[ptIdInCell*dim+comp]=newVal;
211 void ParaMEDMEM::MEDCouplingGaussLocalization::setGaussCoord(int gaussPtIdInCell, int comp, double newVal)
213 int dim=checkCoherencyOfRequest(gaussPtIdInCell,comp);
214 _gauss_coord[gaussPtIdInCell*dim+comp]=newVal;
217 void ParaMEDMEM::MEDCouplingGaussLocalization::setWeight(int gaussPtIdInCell, double newVal)
219 checkCoherencyOfRequest(gaussPtIdInCell,0);
220 _weight[gaussPtIdInCell]=newVal;
223 void ParaMEDMEM::MEDCouplingGaussLocalization::setRefCoords(const std::vector<double>& refCoo)
228 void ParaMEDMEM::MEDCouplingGaussLocalization::setGaussCoords(const std::vector<double>& gsCoo)
233 void ParaMEDMEM::MEDCouplingGaussLocalization::setWeights(const std::vector<double>& w)
239 * The format of 'tinyData' parameter is the same than pushed in method ParaMEDMEM::MEDCouplingGaussLocalization::pushTinySerializationIntInfo.
241 ParaMEDMEM::MEDCouplingGaussLocalization ParaMEDMEM::MEDCouplingGaussLocalization::BuildNewInstanceFromTinyInfo(int dim, const std::vector<int>& tinyData)
243 std::vector<double> v1(dim*tinyData[1]),v2(dim*tinyData[2]),v3(tinyData[2]);
244 return ParaMEDMEM::MEDCouplingGaussLocalization((INTERP_KERNEL::NormalizedCellType)tinyData[0],v1,v2,v3);
247 int ParaMEDMEM::MEDCouplingGaussLocalization::checkCoherencyOfRequest(int gaussPtIdInCell, int comp) const
249 const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_type);
250 int dim=cm.getDimension();
251 int nbGsPts=getNumberOfGaussPt();
252 if(gaussPtIdInCell<0 || gaussPtIdInCell>=nbGsPts)
253 throw INTERP_KERNEL::Exception("gaussPtIdInCell specified is invalid : must be in [0:nbGsPts) !");
254 if(comp<0 || comp>=dim)
255 throw INTERP_KERNEL::Exception("comp specified is invalid : must be in [0:dimOfCell) !");
259 bool ParaMEDMEM::MEDCouplingGaussLocalization::AreAlmostEqual(const std::vector<double>& v1, const std::vector<double>& v2, double eps)
261 std::size_t sz=v1.size();
264 std::vector<double> tmp(sz);
265 std::transform(v1.begin(),v1.end(),v2.begin(),tmp.begin(),std::minus<double>());
266 std::transform(tmp.begin(),tmp.end(),tmp.begin(),std::ptr_fun<double,double>(fabs));
267 return *std::max_element(tmp.begin(),tmp.end())<eps;