Salome HOME
Merge last changes for 0021856: [CEA 663] Documenting API of MEDCoupling and MEDLoade...
[modules/med.git] / src / ParaMEDMEM / ICoCoTrioField.cxx
1 // Copyright (C) 2007-2013  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 // ICoCo file common to several codes
21 // ICoCoTrioField.cxx
22 // version 1.2 10/05/2010
23
24 #include <ICoCoTrioField.hxx>
25 #include <string.h>
26 #include <iostream>
27 #include <iomanip>
28
29 using namespace ICoCo;
30 using namespace std;
31
32 // Default constructor
33 TrioField::TrioField() :
34   _type(0),
35   _mesh_dim(0),
36   _space_dim(0),
37   _nbnodes(0),
38   _nodes_per_elem(0),
39   _nb_elems(0),
40   _itnumber(0),
41   _connectivity(0),
42   _coords(0),
43   _time1(0.),
44   _time2(0.),
45   _nb_field_components(0),
46   _field(0),
47   _has_field_ownership(false) { }
48
49 // Copy constructor
50 TrioField::TrioField(const TrioField& OtherField):_connectivity(0),_coords(0),_field(0) {
51   (*this)=OtherField;
52 }
53
54 // Destructor
55 TrioField::~TrioField() {
56   clear();
57 }
58
59 // After the call to clear(), all pointers are null and field ownership is false.
60 // Arrays are deleted if necessary
61 void TrioField::clear() {
62   if (_connectivity)
63     delete[] _connectivity;
64   if (_coords)
65     delete[] _coords;
66   if (_field && _has_field_ownership)
67     delete[] _field;
68   _connectivity=0;
69   _coords=0;
70   _field=0;
71   _has_field_ownership=false;
72 }
73
74 // Returns the number of value locations
75 // The size of field is nb_values()*_nb_field_components
76 int TrioField::nb_values() const {
77   if (_type==0)
78     return _nb_elems;
79   else if (_type==1)
80     return _nbnodes;
81   throw 0;
82   return -1;
83 }
84
85 // Save field to a .field file (loadable by visit!)
86 void TrioField::save(ostream& os) const{
87
88   os << setprecision(12);
89   os << getName() << endl;
90   os << _type << endl;
91   os << _mesh_dim << endl;
92   os << _space_dim << endl;
93   os << _nbnodes << endl;
94   os << _nodes_per_elem << endl;
95   os << _nb_elems << endl;
96     
97   os<< _itnumber<<endl;
98   for (int i=0;i<_nb_elems;i++) {
99     for (int j=0;j<_nodes_per_elem;j++)
100       os << " " << _connectivity[i*_nodes_per_elem+j];
101     os<<endl;
102   }
103   
104   for (int i=0;i<_nbnodes;i++) {
105     for (int j=0;j<_space_dim;j++)
106       os << " " << _coords[i*_space_dim+j] ;
107     os << endl;
108   }
109   
110   os << _time1 << endl;
111   os << _time2 << endl;
112   os << _nb_field_components << endl;
113
114   if (_field) {
115     os << 1 << endl;
116     for (int i=0;i<nb_values();i++) {
117       for (int j=0;j<_nb_field_components;j++)
118         os << " " << _field[i*_nb_field_components+j];
119       os << endl;
120     }
121   }
122   else
123     os << 0 << endl;
124
125   os << _has_field_ownership << endl;
126
127 }
128
129 // Restore field from a .field file
130 void TrioField::restore(istream& in) {
131
132   string name;
133   in >> name; 
134   setName(name);
135   in >> _type;
136   in >> _mesh_dim;
137   in >> _space_dim;
138   in >> _nbnodes;
139   in >> _nodes_per_elem;
140   in >> _nb_elems;
141     
142   in >> _itnumber;
143   if (_connectivity)
144     delete [] _connectivity;
145   _connectivity=new int[_nodes_per_elem*_nb_elems];
146   for (int i=0;i<_nb_elems;i++) {
147     for (int j=0;j<_nodes_per_elem;j++)
148       in >> _connectivity[i*_nodes_per_elem+j];
149   }
150   if (_coords)
151     delete [] _coords;
152   _coords=new double[_nbnodes*_space_dim];
153   for (int i=0;i<_nbnodes;i++) {
154     for (int j=0;j<_space_dim;j++)
155       in >> _coords[i*_space_dim+j];
156   }
157   
158   in >> _time1;
159   in >> _time2;
160   in >> _nb_field_components;
161   int test;
162   in >> test;
163   if (test) {
164     if (_field)
165       delete [] _field;
166     _field=new double[_nb_field_components*nb_values()];
167     for (int i=0;i<nb_values();i++) {
168       for (int j=0;j<_nb_field_components;j++)
169         in>> _field[i*_nb_field_components+j];
170     }
171   }
172   else
173     _field=0;
174
175   in >> _has_field_ownership;
176 }
177
178
179 // After the call to set_standalone(), field ownership is true and field is allocated
180 // to the size _nb_field_components*nb_values().
181 // The values of the field have been copied if necessary.
182 void TrioField::set_standalone() {
183   if (!_field) {
184     _field=new double[_nb_field_components*nb_values()];
185     _has_field_ownership=true;
186     
187   }
188   else if (!_has_field_ownership) {
189     double *tmp_field=new double[_nb_field_components*nb_values()];
190     memcpy(tmp_field,_field,_nb_field_components*nb_values()*sizeof(double));
191     _field=tmp_field;
192     _has_field_ownership=true;
193   }
194 }
195
196 // Used to simulate a 0D geometry (Cathare/Trio for example).
197 void TrioField::dummy_geom() {
198   _type=0;
199   _mesh_dim=2;
200   _space_dim=2;
201   _nbnodes=3;
202   _nodes_per_elem=3;
203   _nb_elems=1;
204   _itnumber=0;
205   if (_connectivity)
206     delete[] _connectivity;
207   _connectivity=new int[3];
208   _connectivity[0]=0;
209   _connectivity[1]=1;
210   _connectivity[2]=2;
211   if (_coords)
212     delete[] _coords;
213   _coords=new double[6];
214   _coords[0]=0;
215   _coords[1]=0;
216   _coords[2]=1;
217   _coords[3]=0;
218   _coords[4]=0;
219   _coords[5]=1;
220   _time1=0;
221   _time2=1;
222   _nb_field_components=1;
223   if (_field && _has_field_ownership)
224     delete[] _field;
225   _has_field_ownership=false;
226   _field=0;
227 }
228
229 // Overloading operator = for TrioField
230 // This becomes an exact copy of NewField.
231 // If NewField._has_field_ownership is false, they point to the same values.
232 // Otherwise the values are copied.
233 TrioField& TrioField::operator=(const TrioField& NewField){
234
235   clear();
236
237   _type=NewField._type;
238   _mesh_dim=NewField._mesh_dim;
239   _space_dim=NewField._space_dim;
240   _nbnodes=NewField._nbnodes;
241   _nodes_per_elem=NewField._nodes_per_elem;
242   _nb_elems=NewField._nb_elems;
243   _itnumber=NewField._itnumber;
244   _time1=NewField._time1;
245   _time2=NewField._time2;
246   _nb_field_components=NewField._nb_field_components;
247
248   if (!NewField._connectivity)
249     _connectivity=0;
250   else {
251     _connectivity=new int[_nodes_per_elem*_nb_elems];
252     memcpy( _connectivity,NewField._connectivity,_nodes_per_elem*_nb_elems*sizeof(int));
253   }
254
255   if (!NewField._coords)
256     _coords=0;
257   else {
258     _coords=new double[_nbnodes*_space_dim];
259     memcpy( _coords,NewField._coords,_nbnodes*_space_dim*sizeof(double));
260   }
261
262   //Copie des valeurs du champ
263   _has_field_ownership=NewField._has_field_ownership;
264   if (_has_field_ownership) {
265     _field=new double[nb_values()*_nb_field_components];
266     memcpy(_field,NewField._field,nb_values()*_nb_field_components*sizeof(double));
267   }
268   else
269     _field=NewField._field;
270
271   return(*this);
272
273 }
274
275
276