Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDCoupling / MEDCouplingFieldDouble.cxx
1 //  Copyright (C) 2007-2008  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 #include "MEDCouplingFieldDouble.hxx"
20 #include "MEDCouplingMesh.hxx"
21
22 #include <sstream>
23
24 using namespace ParaMEDMEM;
25
26 MEDCouplingFieldDouble *MEDCouplingFieldDouble::New(TypeOfField type)
27 {
28   return new MEDCouplingFieldDouble(type);
29 }
30
31 MEDCouplingFieldDouble::MEDCouplingFieldDouble(TypeOfField type):MEDCouplingField(type),_array(0)
32 {
33 }
34
35 MEDCouplingFieldDouble::~MEDCouplingFieldDouble()
36 {
37   if(_array)
38     _array->decrRef();
39 }
40
41 void MEDCouplingFieldDouble::checkCoherency() const throw(INTERP_KERNEL::Exception)
42 {
43   if(!_mesh)
44     throw INTERP_KERNEL::Exception("Field invalid because no mesh specified !");
45   if(!_array)
46     throw INTERP_KERNEL::Exception("Field invalid because no values set !");
47   if(_type==ON_CELLS)
48     {
49       if(_mesh->getNumberOfCells()!=_array->getNumberOfTuples())
50         {
51           std::ostringstream message;
52           message << "Field on cells invalid because there are " << _mesh->getNumberOfCells();
53           message << " cells in mesh and " << _array->getNumberOfTuples() << " tuples in field !";
54           throw INTERP_KERNEL::Exception(message.str().c_str());
55         }
56     }
57   else if(_type==ON_NODES)
58     {
59       if(_mesh->getNumberOfNodes()!=_array->getNumberOfTuples())
60         {
61           std::ostringstream message;
62           message << "Field on nodes invalid because there are " << _mesh->getNumberOfNodes();
63           message << " cells in mesh and " << _array->getNumberOfTuples() << " tuples in field !";
64           throw INTERP_KERNEL::Exception(message.str().c_str());
65         }
66     }
67   else
68     throw INTERP_KERNEL::Exception("Field of undifined type !!!");
69 }
70
71 void MEDCouplingFieldDouble::applyLin(double a, double b, int compoId)
72 {
73   double *ptr=_array->getPointer();
74   ptr+=compoId;
75   int nbOfComp=_array->getNumberOfComponents();
76   int nbOfTuple=_array->getNumberOfTuples();
77   for(int i=0;i<nbOfTuple;i++,ptr+=nbOfComp)
78     *ptr=a*(*ptr)+b;
79 }
80
81 int MEDCouplingFieldDouble::getNumberOfComponents() const
82 {
83   return _array->getNumberOfComponents();
84 }
85
86 int MEDCouplingFieldDouble::getNumberOfTuples() const throw(INTERP_KERNEL::Exception)
87 {
88   if(!_mesh)
89     throw INTERP_KERNEL::Exception("Impossible to retrieve number of tuples because no mesh specified !");
90   if(_type==ON_CELLS)
91     return _mesh->getNumberOfCells();
92   else if(_type==ON_NODES)
93     return _mesh->getNumberOfNodes();
94   else
95     throw INTERP_KERNEL::Exception("Impossible to retrieve number of tuples because type of entity not implemented yet !");
96 }
97
98 void MEDCouplingFieldDouble::updateTime()
99 {
100   MEDCouplingField::updateTime();
101   if(_array)
102     updateTimeWith(*_array);
103 }
104
105 void MEDCouplingFieldDouble::setArray(DataArrayDouble *array)
106 {
107   if(array!=_array)
108     {
109       if(_array)
110         _array->decrRef();
111       _array=array;
112       if(_array)
113         _array->incrRef();
114       declareAsNew();
115     }
116 }