Salome HOME
remove a reference to the $MED_ROOT_DIR in the Makefile.in wich is useless
[modules/med.git] / src / MEDMEM / MEDMEM_Field.cxx
1 #include "MEDMEM_Field.hxx"
2 #include "MEDMEM_Mesh.hxx"
3
4 using namespace std;
5 using namespace MEDMEM;
6 using namespace MED_EN;
7
8 // ---------------------------------
9 // FIELD_ : Constructors
10 // ---------------------------------
11 FIELD_::FIELD_(): 
12   _isRead(false),
13   _name(""), _description(""), _support((SUPPORT *)NULL),
14   _numberOfComponents(0), _numberOfValues(0),_componentsTypes((int *)NULL),
15   _componentsNames((string *)NULL), 
16   _componentsDescriptions((string *)NULL),
17   _componentsUnits((UNIT*)NULL),
18   _MEDComponentsUnits((string *)NULL),
19   _iterationNumber(-1),_time(0.0),_orderNumber(-1)
20 {
21   MESSAGE("Constructeur FIELD_ sans parametre");
22 }
23
24 FIELD_::FIELD_(const SUPPORT * Support, const int NumberOfComponents):
25   _isRead(false),
26   _name(""), _description(""), _support(Support),
27   _numberOfComponents(NumberOfComponents),
28   _iterationNumber(-1),_time(0.0),_orderNumber(-1)
29 {
30   MESSAGE("FIELD_(const SUPPORT * Support, const int NumberOfComponents)");
31
32   _numberOfValues = Support->getNumberOfElements(MED_ALL_ELEMENTS);
33   _componentsTypes = new int[NumberOfComponents] ;
34   _componentsNames = new string[NumberOfComponents];
35   _componentsDescriptions = new string[NumberOfComponents];
36   _componentsUnits = new UNIT[NumberOfComponents];
37   _MEDComponentsUnits = new string[NumberOfComponents];
38   for(int i=0;i<NumberOfComponents;i++) {
39     _componentsTypes[i] = 0 ;
40   }
41   if(_support)
42     _support->addReference();
43 }
44
45 FIELD_::FIELD_(const FIELD_ &m)
46 {
47   _isRead = m._isRead ;
48   _name = m._name;
49   _description = m._description;
50   _support = m._support;
51   if(_support)
52     _support->addReference();
53   _numberOfComponents = m._numberOfComponents;
54   _numberOfValues = m._numberOfValues;
55   copyGlobalInfo(m);
56   _valueType = m._valueType;
57   //_drivers = m._drivers ; // PG : Well, same driver, what about m destructor !
58 }
59
60 FIELD_::~FIELD_()
61 {   
62   MESSAGE("~FIELD_()");
63   if ( _componentsTypes !=NULL)
64     delete[] _componentsTypes ;
65   if ( _componentsNames !=NULL)
66     delete[] _componentsNames ;
67   if ( _componentsDescriptions !=NULL)
68     delete[] _componentsDescriptions ;
69   if ( _componentsUnits !=NULL)
70     delete[] _componentsUnits ;
71   if ( _MEDComponentsUnits !=NULL)
72     delete[] _MEDComponentsUnits ;
73   // delete driver
74 //   vector<GENDRIVER *>::const_iterator it ;
75 //   SCRUTE(_drivers.size());
76 //   int i=0;
77 //   for (it=_drivers.begin();it!=_drivers.end();it++) {
78 //     i++;
79 //     SCRUTE(i);
80 //     delete (*it) ;
81
82
83   MESSAGE("In this object FIELD_ there is(are) " << _drivers.size() << " driver(s)");
84
85   for (unsigned int index=0; index < _drivers.size(); index++ )
86     {
87       SCRUTE(_drivers[index]);
88       if ( _drivers[index] != NULL) delete _drivers[index];
89     }
90 }
91
92 /*! 
93   \if developper
94   PROVISOIRE : retourne des volumes, surfaces ou longueurs suivant les cas
95   \endif
96 */
97 FIELD<double>* FIELD_::_getFieldSize() const
98 {
99     FIELD<double>* p_field_size;
100     switch (getSupport()->getEntity())
101     {
102         case MED_CELL :
103             switch (getSupport()->getMesh()->getSpaceDimension() ) 
104             {
105                 case 1:
106                     p_field_size=getSupport()->getMesh()->getLength(getSupport() );
107                     break;
108                 case 2:
109                     p_field_size=getSupport()->getMesh()->getArea(getSupport() );
110                     break;
111                 case 3:
112                     p_field_size=getSupport()->getMesh()->getVolume(getSupport() );
113                     break;
114             }
115             break;
116             
117         case MED_FACE :
118             p_field_size=getSupport()->getMesh()->getArea(getSupport() );
119             break;
120
121         case MED_EDGE :
122             p_field_size=getSupport()->getMesh()->getLength(getSupport() );
123             break;
124     }
125     return p_field_size;
126 }
127
128
129 /*! 
130   \if developper
131   Check up the compatibility of field before computing sobolev norm 
132   \endif
133 */
134 void FIELD_::_checkNormCompatibility(const FIELD<double>* support_volume) const throw (MEDEXCEPTION)
135 {
136     string diagnosis;
137     if( getSupport()->getEntity() == MED_NODE )
138     {
139         diagnosis="Cannot compute sobolev norm on a field "+getName()+
140             " : it has support on nodes!";
141         throw MEDEXCEPTION(diagnosis.c_str());
142     }
143         
144     if (getNumberOfValues()*getNumberOfComponents()<= 0) // Size of array has to be strictly positive
145     {
146         diagnosis="Cannot compute the norm of "+getName()+
147             " : it size is non positive!";
148         throw MEDEXCEPTION(diagnosis.c_str());
149     }
150
151     const int* nbGauss=getSupport()->getNumberOfGaussPoint();
152     for (int i=0; i<getSupport()->getNumberOfTypes(); ++i)
153         if(nbGauss[i]!=1)
154         {
155             diagnosis="Cannot compute Lnorm of "+getName()+
156             " : Gauss numbers greater than one are not yet implemented!";
157             throw MEDEXCEPTION(diagnosis.c_str());
158         }
159
160     if(support_volume) // if the user has supplied the volume
161     {
162         if(support_volume->getSupport()!=getSupport())
163         {
164             diagnosis="Cannot compute Lnorm of "+getName()+
165             " : the volume furnished has not the same support!";
166             throw MEDEXCEPTION(diagnosis.c_str());
167         }
168         if(support_volume->getNumberOfValues()!=getNumberOfValues())
169         {
170             diagnosis="Cannot compute Lnorm of "+getName()+
171             " : the volume furnished has not the same number of values!";
172             throw MEDEXCEPTION(diagnosis.c_str());
173         }
174     }
175
176 }
177
178 /*! 
179   \if developper
180    Check up the compatibility of fields before performing an arithmetic operation
181   \endif
182 */
183 void FIELD_::_checkFieldCompatibility(const FIELD_& m, const FIELD_& n, bool checkUnit) throw (MEDEXCEPTION)
184 {
185     string diagnosis;
186
187     // check-up, fill diagnosis if some incompatibility is found.
188     if(m._support != n._support)
189       {
190         if(!(*m._support==*n._support))
191           diagnosis+="They don't have the same support!";
192       }
193     else if(m._numberOfComponents != n._numberOfComponents)
194       diagnosis+="They don't have the same number of components!";
195     else if(m._numberOfValues != n._numberOfValues)
196       diagnosis+="They don't have the same number of values!";
197     else
198       {
199         if(checkUnit)
200           {
201             for(int i=0; i<m._numberOfComponents; i++)
202               {
203                 // Not yet implemented   
204                 //          if(m._componentsTypes[i] != n._componentsTypes[i])
205                 //          {
206                 //              diagnosis+="Components don't have the same types!";
207                 //              break;
208                 //          }
209                 if(m._MEDComponentsUnits[i] != n._MEDComponentsUnits[i])
210                   {
211                     diagnosis+="Components don't have the same units!";
212                     break;
213                   }
214               }
215           }
216       }
217
218     if(diagnosis.size()) // if fields are not compatible : complete diagnosis and throw exception
219     {
220         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
221                  + n._name + " are not compatible.\n" + diagnosis;
222         throw MEDEXCEPTION(diagnosis.c_str());
223     }
224
225     if( m.getNumberOfValues()<=0 || m.getNumberOfComponents()<=0) // check up the size is strictly positive
226     {
227         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
228                  + n._name + " are empty! (size<=0).\n";
229         throw MEDEXCEPTION(diagnosis.c_str());
230     }
231
232 }
233
234 void FIELD_::_deepCheckFieldCompatibility(const FIELD_& m, const FIELD_& n , bool checkUnit ) throw (MEDEXCEPTION)
235 {
236   string diagnosis;
237
238     // check-up, fill diagnosis if some incompatibility is found.
239     if(m._support != n._support)
240       {
241         if(!(m._support->deepCompare(*n._support)))
242           diagnosis+="They don't have the same support!";
243       }
244     else if(m._numberOfComponents != n._numberOfComponents)
245       diagnosis+="They don't have the same number of components!";
246     else if(m._numberOfValues != n._numberOfValues)
247       diagnosis+="They don't have the same number of values!";
248     else
249       {
250         if(checkUnit)
251           {
252             for(int i=0; i<m._numberOfComponents; i++)
253               {
254                 if(m._MEDComponentsUnits[i] != n._MEDComponentsUnits[i])
255                   {
256                     diagnosis+="Components don't have the same units!";
257                     break;
258                   }
259               }
260           }
261       }
262
263     if(diagnosis.size()) // if fields are not compatible : complete diagnosis and throw exception
264     {
265         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
266                  + n._name + " are not compatible.\n" + diagnosis;
267         throw MEDEXCEPTION(diagnosis.c_str());
268     }
269
270     if( m.getNumberOfValues()<=0 || m.getNumberOfComponents()<=0) // check up the size is strictly positive
271     {
272         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
273                  + n._name + " are empty! (size<=0).\n";
274         throw MEDEXCEPTION(diagnosis.c_str());
275     }
276
277          
278 void     FIELD_::rmDriver      (int index)
279 {
280   MESSAGE("void FIELD_::rmDriver(int index) : removing the driver " << index);
281 };
282 int      FIELD_::addDriver     (driverTypes driverType, 
283                                 const string & fileName,
284                                 const string & driverFieldName,
285                                 med_mode_acces access)
286 {
287   MESSAGE("int FIELD_::addDriver(driverTypes driverType, const string & fileName, const string & driverFieldName) : adding the driver " << driverType << " fileName = " << fileName.c_str() << " driverFieldName = " << driverFieldName.c_str());
288   return 0;
289 };
290
291 int      FIELD_::addDriver     (GENDRIVER & driver)
292 {
293   MESSAGE("int FIELD_::addDriver(GENDRIVER & driver) : driver " << driver);
294   return 0;
295 };
296
297 void     FIELD_::openAppend    ( void )                               {};
298 void     FIELD_::write         (const GENDRIVER &)                    {};
299 void     FIELD_::writeAppend   (const GENDRIVER &)                    {};
300 void     FIELD_::read          (const GENDRIVER &)                    {};
301 void     FIELD_::write         (int , const string & ) {};
302 void     FIELD_::writeAppend   (int , const string & ) {};
303 void     FIELD_::read          (int )                                  {};
304 void     FIELD_::copyGlobalInfo(const FIELD_& m)
305 {  
306   if (m._componentsTypes != NULL)
307     {
308       _componentsTypes = new int[m._numberOfComponents] ;
309       memcpy(_componentsTypes,m._componentsTypes,sizeof(int)*m._numberOfComponents);
310     }
311   else
312     _componentsTypes = (int *) NULL;
313
314   _componentsNames = new string[m._numberOfComponents];
315   for (int i=0; i<m._numberOfComponents; i++)
316     _componentsNames[i]=m._componentsNames[i];
317   _componentsDescriptions = new string[m._numberOfComponents];
318   for (int i=0; i<m._numberOfComponents; i++)
319     _componentsDescriptions[i]=m._componentsDescriptions[i];
320
321   if (m._componentsUnits != NULL)
322     {
323       _componentsUnits = new UNIT[m._numberOfComponents];
324       for (int i=0; i<m._numberOfComponents; i++)
325         _componentsUnits[i] = m._componentsUnits[i];
326     }
327   else
328     _componentsUnits=(UNIT*)NULL;
329   
330   // L'operateur '=' est defini dans la classe UNIT
331   _MEDComponentsUnits = new string[m._numberOfComponents];
332   for (int i=0; i<m._numberOfComponents; i++)
333     {_MEDComponentsUnits[i] = m._MEDComponentsUnits[i];}
334   _iterationNumber = m._iterationNumber;
335   _time = m._time;
336   _orderNumber = m._orderNumber;
337 }