Salome HOME
update due to bugs PAL8113 and another I do not remember the number ;) .
[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 }
42
43 FIELD_::FIELD_(const FIELD_ &m)
44 {
45   _isRead = m._isRead ;
46   _name = m._name;
47   _description = m._description;
48   _support = m._support;
49   _numberOfComponents = m._numberOfComponents;
50   _numberOfValues = m._numberOfValues;
51
52   if (m._componentsTypes != NULL)
53     {
54       _componentsTypes = new int[m._numberOfComponents] ;
55       memcpy(_componentsTypes,m._componentsTypes,sizeof(int)*m._numberOfComponents);
56       /*
57       _componentsTypes = new int[m._numberOfComponents] ;
58       for(int i=0;i<m._numberOfComponents;i++) {
59         _componentsTypes[i] = m._componentsTypes[i] ;
60       }
61       */
62     }
63   else _componentsTypes = (int *) NULL;
64
65   _componentsNames = new string[m._numberOfComponents];
66   for (int i=0; i<m._numberOfComponents; i++)
67     {_componentsNames[i]=m._componentsNames[i];}
68   _componentsDescriptions = new string[m._numberOfComponents];
69   for (int i=0; i<m._numberOfComponents; i++)
70     {_componentsDescriptions[i]=m._componentsDescriptions[i];}
71   _componentsUnits = new UNIT[m._numberOfComponents];
72   for (int i=0; i<m._numberOfComponents; i++)
73     {_componentsUnits[i] = m._componentsUnits[i];}
74   // L'operateur '=' est defini dans la classe UNIT
75   _MEDComponentsUnits = new string[m._numberOfComponents];
76   for (int i=0; i<m._numberOfComponents; i++)
77     {_MEDComponentsUnits[i] = m._MEDComponentsUnits[i];}
78   _iterationNumber = m._iterationNumber;
79   _time = m._time;
80   _orderNumber = m._orderNumber;
81   _valueType = m._valueType;
82   //_drivers = m._drivers ; // PG : Well, same driver, what about m destructor !
83
84 }
85
86 FIELD_::~FIELD_()
87 {   
88   MESSAGE("~FIELD_()");
89   if ( _componentsTypes !=NULL)
90     delete[] _componentsTypes ;
91   if ( _componentsNames !=NULL)
92     delete[] _componentsNames ;
93   if ( _componentsDescriptions !=NULL)
94     delete[] _componentsDescriptions ;
95   if ( _componentsUnits !=NULL)
96     delete[] _componentsUnits ;
97   if ( _MEDComponentsUnits !=NULL)
98     delete[] _MEDComponentsUnits ;
99
100   // delete driver
101 //   vector<GENDRIVER *>::const_iterator it ;
102 //   SCRUTE(_drivers.size());
103 //   int i=0;
104 //   for (it=_drivers.begin();it!=_drivers.end();it++) {
105 //     i++;
106 //     SCRUTE(i);
107 //     delete (*it) ;
108
109
110   MESSAGE("In this object FIELD_ there is(are) " << _drivers.size() << " driver(s)");
111
112   for (unsigned int index=0; index < _drivers.size(); index++ )
113     {
114       SCRUTE(_drivers[index]);
115       if ( _drivers[index] != NULL) delete _drivers[index];
116     }
117 }
118
119 /*! 
120   \if developper
121   PROVISOIRE : retourne des volumes, surfaces ou longueurs suivant les cas
122   \endif
123 */
124 FIELD<double>* FIELD_::_getFieldSize() const
125 {
126     FIELD<double>* p_field_size;
127     switch (getSupport()->getEntity())
128     {
129         case MED_CELL :
130             switch (getSupport()->getMesh()->getSpaceDimension() ) 
131             {
132                 case 1:
133                     p_field_size=getSupport()->getMesh()->getLength(getSupport() );
134                     break;
135                 case 2:
136                     p_field_size=getSupport()->getMesh()->getArea(getSupport() );
137                     break;
138                 case 3:
139                     p_field_size=getSupport()->getMesh()->getVolume(getSupport() );
140                     break;
141             }
142             break;
143             
144         case MED_FACE :
145             p_field_size=getSupport()->getMesh()->getArea(getSupport() );
146             break;
147
148         case MED_EDGE :
149             p_field_size=getSupport()->getMesh()->getLength(getSupport() );
150             break;
151     }
152     return p_field_size;
153 }
154
155
156 /*! 
157   \if developper
158   Check up the compatibility of field before computing sobolev norm 
159   \endif
160 */
161 void FIELD_::_checkNormCompatibility(const FIELD<double>* support_volume) const throw (MEDEXCEPTION)
162 {
163     string diagnosis;
164     if( getSupport()->getEntity() == MED_NODE )
165     {
166         diagnosis="Cannot compute sobolev norm on a field "+getName()+
167             " : it has support on nodes!";
168         throw MEDEXCEPTION(diagnosis.c_str());
169     }
170         
171     if (getNumberOfValues()*getNumberOfComponents()<= 0) // Size of array has to be strictly positive
172     {
173         diagnosis="Cannot compute the norm of "+getName()+
174             " : it size is non positive!";
175         throw MEDEXCEPTION(diagnosis.c_str());
176     }
177
178     const int* nbGauss=getSupport()->getNumberOfGaussPoint();
179     for (int i=0; i<getSupport()->getNumberOfTypes(); ++i)
180         if(nbGauss[i]!=1)
181         {
182             diagnosis="Cannot compute Lnorm of "+getName()+
183             " : Gauss numbers greater than one are not yet implemented!";
184             throw MEDEXCEPTION(diagnosis.c_str());
185         }
186
187     if(support_volume) // if the user has supplied the volume
188     {
189         if(support_volume->getSupport()!=getSupport())
190         {
191             diagnosis="Cannot compute Lnorm of "+getName()+
192             " : the volume furnished has not the same support!";
193             throw MEDEXCEPTION(diagnosis.c_str());
194         }
195         if(support_volume->getNumberOfValues()!=getNumberOfValues())
196         {
197             diagnosis="Cannot compute Lnorm of "+getName()+
198             " : the volume furnished has not the same number of values!";
199             throw MEDEXCEPTION(diagnosis.c_str());
200         }
201     }
202
203 }
204
205 /*! 
206   \if developper
207    Check up the compatibility of fields before performing an arithmetic operation
208   \endif
209 */
210 void FIELD_::_checkFieldCompatibility(const FIELD_& m, const FIELD_& n ) throw (MEDEXCEPTION)
211 {
212     string diagnosis;
213
214     // check-up, fill diagnosis if some incompatibility is found.
215     if(m._support != n._support)
216       {
217         if(!(*m._support==*n._support))
218           diagnosis+="They don't have the same support!";
219       }
220     else if(m._numberOfComponents != n._numberOfComponents)
221       diagnosis+="They don't have the same number of components!";
222     else if(m._numberOfValues != n._numberOfValues)
223       diagnosis+="They don't have the same number of values!";
224     else
225       {
226         for(int i=0; i<m._numberOfComponents; i++)
227         {
228 // Not yet implemented   
229 //          if(m._componentsTypes[i] != n._componentsTypes[i])
230 //          {
231 //              diagnosis+="Components don't have the same types!";
232 //              break;
233 //          }
234           if(m._MEDComponentsUnits[i] != n._MEDComponentsUnits[i])
235             {
236               diagnosis+="Components don't have the same units!";
237               break;
238             }
239         }
240       }
241
242     if(diagnosis.size()) // if fields are not compatible : complete diagnosis and throw exception
243     {
244         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
245                  + n._name + " are not compatible.\n" + diagnosis;
246         throw MEDEXCEPTION(diagnosis.c_str());
247     }
248
249     if( m.getNumberOfValues()<=0 || m.getNumberOfComponents()<=0) // check up the size is strictly positive
250     {
251         diagnosis="Field's operation not allowed!\nThe fields " + m._name + " and " 
252                  + n._name + " are empty! (size<=0).\n";
253         throw MEDEXCEPTION(diagnosis.c_str());
254     }
255
256 }
257
258 //  void     FIELD_::setIterationNumber (int IterationNumber)           {};
259 //  void     FIELD_::setOrderNumber     (int OrderNumber)               {}; 
260 //  void     FIELD_::setFieldName       (string& fieldName)             {}; 
261          
262 void     FIELD_::rmDriver      (int index)
263 {
264   MESSAGE("void FIELD_::rmDriver(int index) : removing the driver " << index);
265 };
266 int      FIELD_::addDriver     (driverTypes driverType, 
267                                 const string & fileName,
268                                 const string & driverFieldName,
269                                 med_mode_acces access)
270 {
271   MESSAGE("int FIELD_::addDriver(driverTypes driverType, const string & fileName, const string & driverFieldName) : adding the driver " << driverType << " fileName = " << fileName.c_str() << " driverFieldName = " << driverFieldName.c_str());
272   return 0;
273 };
274 int      FIELD_::addDriver     (GENDRIVER & driver)
275 {
276   MESSAGE("int FIELD_::addDriver(GENDRIVER & driver) : driver " << driver);
277   return 0;
278 };
279 void     FIELD_::openAppend    ( void )                               {};
280 void     FIELD_::write         (const GENDRIVER &)                    {};
281 void     FIELD_::writeAppend   (const GENDRIVER &)                    {};
282 void     FIELD_::read          (const GENDRIVER &)                    {};
283 void     FIELD_::write         (int , const string & ) {};
284 void     FIELD_::writeAppend   (int , const string & ) {};
285 void     FIELD_::read          (int )                                  {};
286
287 //  void                     FIELD_::setValueType(med_type_champ ValueType) {};
288 //  med_type_champ FIELD_::getValueType() {};