]> SALOME platform Git repositories - tools/solverlab.git/blob - CDMATH/mesh/inc/Field.hxx
Salome HOME
Improved writing of meshes and fields
[tools/solverlab.git] / CDMATH / mesh / inc / Field.hxx
1 /*
2  * field.hxx
3  *
4  *  Created on: 07 fevrier. 2012
5  *      Authors: CDMAT groups
6  */
7
8 #ifndef FIELD_HXX_
9 #define FIELD_HXX_
10
11 namespace MEDCoupling
12 {
13   class MEDCouplingFieldDouble;
14   class DataArrayDouble;
15   class MEDFileField1TS;
16 }
17
18 #include "Vector.hxx"
19 #include "Mesh.hxx"
20
21 #include <MCAuto.hxx>
22
23 /**
24  * Field class is defined by
25  * - ........
26  */
27
28 class Field
29 {
30     public: //----------------------------------------------------------------
31     /**
32      * default constructor
33      */
34     Field ( EntityType typeField = CELLS ) ;
35
36     /**
37     * constructor with data:
38     * @param fieldName : name of the field
39     * @param type : type of the field
40     * @param mesh : mesh of the field
41     * @param numberOfComponents : number of the component
42     * @param time : time of the field
43     */
44     Field(const std::string fieldName, EntityType type, const Mesh& mesh, int numberOfComponents=1, double time=0.0) ;
45
46     /**
47     * destructor
48     */
49     ~Field ( void ) ;
50
51     /**
52     * constructor by copy
53     * @param field : The Field object to be copied
54     */
55     Field ( const Field & field ) ;
56
57     /**
58      * deep copy of a field (values are copied)
59      * @param field : The Field object to be copied
60      */
61     Field deepCopy( ) const;
62
63     /**
64      * constructor with data
65      * @param filename : file name of field med file
66      * @param fieldType: field type
67      * @param fieldName: field name
68      * @param iteration: iteration number (optional)
69      * @param order:     order inside an iteration (optional)
70      * @param numberOfComponents:     number of components of the field (optional)
71      * @param time:     time index of the field (optional)
72      */
73     Field( const std::string filename, EntityType fieldType,
74            const std::string & fieldName = "",
75            int iteration = -1, int order = -1, int meshLevel=0);
76   
77     /**
78      * constructor with data
79          * \brief defines a constant field on a mesh stored in a med file
80          * \details
81          * \param [in] string : the mesh file name
82      * \param fieldType: field type
83          * \param [in] vector<double> : the value in each cell
84      * \param [in] fieldName: field name
85          * \param [in] meshLevel : relative mesh dimension : 0->cells, 1->Faces etc
86          *  */
87         Field(const std::string meshfileName, EntityType fieldType, 
88                   const std::vector<double> Vconstant,const std::string & fieldName = "",
89                    int meshLevel=0, double time=0.0, std::string meshName="");
90
91     /**
92      * constructor with data
93          * \brief defines a constant field 
94          * \details
95          * \param [in] Mesh 
96      * \param [in] fieldType: field type
97          * \param [in] Vector
98      * \param [in] fieldName: field name
99          *  */
100         Field(const Mesh& M, EntityType fieldType, const Vector Vconstant,
101                   const std::string & fieldName = "", double time=0.0);
102
103     /**
104      * constructor with data
105          * \brief defines a constant field 
106          * \details
107          * \param [in] Mesh
108      * \param [in] fieldType: field type
109          * \param [in] vector<double>
110      * \param [in] fieldName: field name
111          *  */
112         Field(const Mesh& M, EntityType fieldType, const std::vector<double> Vconstant, const std::string & fieldName = "", double time=0.0);
113
114     /**
115      * constructor with data
116          * \brief Builds a rectangular mesh M and defines a constant field on M
117          * \details
118          * \param [in] int the space dimension
119          * \param [in] vector<double> the value in each cell
120      * \param [in] fieldType: field type
121      * \param [in] fieldName: field name
122          * \param [in] double the lowest value in the x direction
123          * \param [in] double the highest value in the x direction
124          * \param [in] string name of the left boundary
125          * \param [in] string name of the right boundary
126          * \param [in] double the lowest value in the y direction
127          * \param [in] double the highest value in the y direction
128          * \param [in] string name of the back boundary
129          * \param [in] string name of the front boundary
130          * \param [in] double the lowest value in the z direction
131          * \param [in] double the highest value in the z direction
132          * \param [in] string name of the bottom boundary
133          * \param [in] string name of the top boundary
134          *  */
135         Field( int nDim, const std::vector<double> Vconstant, EntityType type, 
136                         double xmin, double xmax,int nx, std::string leftSide, std::string rightSide,
137                         double ymin=0, double ymax=0, int ny=0, std::string backSide="", std::string frontSide="",
138                         double zmin=0, double zmax=0, int nz=0, std::string bottomSide="", std::string topSide="",
139                         const std::string & fieldName="", double time=0.0,double epsilon=1e-6);
140
141     /**
142      * constructor with data
143          * \brief Builds a step function field on the mesh M. The direction of the discontinuity is determined by the parameter "direction". The field takes value VV_left for x,y or z<disc_pos and VV_right for x,y or z>disc_pos
144          * \details
145          * \param [in] Mesh
146          * \param [in] Vector
147          * \param [in] Vector
148          * \param [in] double position of the discontinuity on one of the three axis
149          * \param [in] int direction (axis carrying the discontinuity) : 0 for x, 1 for y, 2 for z
150      * \param [in] fieldType: field type
151      * \param [in] fieldName: field name
152          *  */
153         Field(const Mesh M, const Vector VV_left, const Vector VV_right, double disc_pos, 
154                         EntityType type, int direction=0, const std::string & fieldName="", double time=0.0);
155
156     /**
157      * constructor with data
158          * \brief Builds a rectangular mesh M and defines a step function field on M that takes values VV_left for x<xstep and VV_right for x>xstep
159          * \param [in] int the space dimension
160          * \param [in] vector<double> the value left of the discontinuity
161          * \param [in] vector<double> the value right of the discontinuity
162          * \param [in] double the position of the discontinuity in the x direction
163      * \param [in] fieldType: field type
164      * \param [in] fieldName: field name
165          * \param [in] double the lowest value in the x direction
166          * \param [in] double the highest value in the x direction
167          * \param [in] string name of the left boundary
168          * \param [in] string name of the right boundary
169          * \param [in] double the lowest value in the y direction
170          * \param [in] double the highest value in the y direction
171          * \param [in] string name of the back boundary
172          * \param [in] string name of the front boundary
173          * \param [in] double the lowest value in the z direction
174          * \param [in] double the highest value in the z direction
175          * \param [in] string name of the bottom boundary
176          * \param [in] string name of the top boundary
177          * \param [out] void
178          *  */
179         Field( int nDim, const std::vector<double> VV_Left, std::vector<double> VV_Right, 
180                         double xstep, EntityType type,
181                         double xmin, double xmax,int nx, std::string leftSide, std::string rightSide,
182                         double ymin=0, double ymax=0, int ny=0, std::string backSide="", std::string frontSide="",
183                         double zmin=0, double zmax=0, int nz=0, std::string bottomSide="", std::string topSide="",
184                         int direction=0, const std::string & fieldName="", double time=0.0, double epsilon=1e-6);
185
186     /**
187      * constructor with data
188          * \brief builds a step function field on mesh M with values Vin inside the ball with radius Radius and Vout outside
189          * \details
190          * \param [in] Mesh
191          * \param [in] Vector Vin, value inside the ball
192          * \param [in] Vector Vout, value outside the ball
193          * \param [in] double radius of the ball
194          * \param [in] Vector Center, coordinates of the ball center
195      * \param [in] fieldType: field type
196      * \param [in] fieldName: field name
197          *  */
198         Field(const Mesh M, const Vector Vin, const Vector Vout, double Radius, 
199                         Vector Center, EntityType type, const std::string & fieldName="", double time=0.0);
200
201     void readFieldMed( const std::string & fileNameRadical,
202                        EntityType type,
203                        const std::string & fieldName = "",
204                        int iteration = -1,
205                        int order = -1) ;
206
207     void buildFieldMemoryStructure();
208
209     MEDCoupling::DataArrayDouble * getArray();
210
211     double& operator[] ( int ielem ) ;
212
213     double operator[] ( int ielem ) const;
214
215     double& operator() ( int ielem ) ;
216
217     double operator() ( int ielem ) const;
218
219     double& operator() ( int ielem, int jcomp ) ;
220
221     double operator() ( int ielem, int jcomp ) const ;
222
223     int getNumberOfComponents ( void ) const ;
224
225     const double* getValues ( void ) const ;
226
227     void getValues ( Vector myVector ) const ;
228
229     void setValues ( Vector values ) ;
230
231     void setValues ( double * values, int nbValues ) ;
232
233     const std::string getName ( void ) const;
234
235     const Mesh& getMesh ( void ) const ;
236
237     int getNumberOfElements ( void ) const ;
238
239     EntityType getTypeOfField ( void ) const ;
240
241         // returns the x, y or z component of the element (node cell or face) with number i
242     double getElementComponent(int i, int comp) const;
243     /**
244      * return the MEDCouplingField pointer
245      * return _field
246      */
247     MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> getField ( void )  const ;
248
249     void setFieldByMEDCouplingFieldDouble ( const MEDCoupling::MEDCouplingFieldDouble* field );
250
251     void setFieldByDataArrayDouble ( const MEDCoupling::DataArrayDouble* array );
252
253     /** 
254          * \brief Delete the medcoupling mesh to save memory space
255      */
256     void deleteMEDCouplingMesh();
257     
258     /** 
259      * \brief Returns true iff an unstructured mesh has been loaded
260      */
261      bool meshNotDeleted() const {return _mesh.meshNotDeleted();}
262     
263     Vector getNormEuclidean( void ) const ;
264
265     double max( int component=0 ) const ;
266
267     double min( int component=0 ) const ;
268
269     void setTime ( double time, int iter );
270
271     std::vector< double > getFieldValues(int compo=0) const ;
272
273     Vector getValuesOnComponent(int compo) const ;
274
275     Vector getValuesOnAllComponents(int elem) const ;
276
277     int getSpaceDimension( void ) const;
278
279     double getTime ( void ) const;
280
281     void setName ( const std::string fieldName ) ;
282
283     void setInfoOnComponent(int icomp, std::string nameCompo) ;
284
285     std::string getInfoOnComponent(int icomp) const;
286
287     /**
288      * Computes all the components of the sum of values of the field multiplied by dual cell measures. In case of a field on cells, the dual mesh coincides with the underlying mesh
289      * The field may be multicomponent so the result of the integral should be a vector
290      * return the vector of numerical value of the integral of the field
291      */
292     Vector integral() const;
293
294     /**
295      * Computes the sum of values of a given component of the field multiplied by dual cell measures. In case of a field on cells, the dual mesh coincides with the underlying mesh
296      * @param the index of the component of interest
297      * return the numerical value of the integral of the field
298      */
299     double integral(int compId) const;
300
301     /**
302      * Computes for each component the sum of the absolute values of the field components multiplied by dual cell measures. In case of a field on cells, the dual mesh coincides with the underlying mesh.
303      * The field may be multicomponent so the result of the integral should be a vector
304      * return the vector of numerical value of the L1 norm of each component of the field
305      */
306     Vector normL1() const;
307
308     /**
309      * Computes all the components of the sum of squares of the values of the field components multiplied by dual cell measures. In case of a field on cells, the dual mesh coincides with the underlying mesh
310      * The field may be multicomponent so the result of the integral should be a vector
311      * return the vector of numerical value of the L2 norm of each component of the field
312      */
313     Vector normL2() const;
314
315     /**
316      * Computes the maximum of each component of the field
317      * The field may be multicomponent so the result of the function is a vector
318      * return the vector of numerical value of the Linfinity norm of each component of the field
319      */
320     Vector normMax() const;
321
322     /**
323      * Computes the maximum of each component of the field as well as the index where the maximum was found
324      * The field may be multicomponent so the result of the function is a vector of values and a vector of indices
325      * return the vector of numerical value of the Linfinity norm of each component of the field AND the corresponding vector of indices
326      */
327     Vector componentMax(Vector & Indices) const;
328
329     const Field& operator= ( const Field& f ) ;
330
331     Field operator+ ( const Field& f ) const ;
332
333     Field operator- ( const Field& f ) const ;
334
335     const Field& operator+= ( const Field& f ) ;
336
337     const Field& operator-= ( const Field& f ) ;
338
339     const Field& operator*= ( double s ) ;
340
341     const Field& operator/= ( double s ) ;
342
343     const Field& operator-= ( double s ) ;
344
345     const Field& operator+= ( double s ) ;
346
347     void writeVTK ( const std::string fileName, bool fromScratch=true ) const ;
348
349     void writeMED ( const std::string fileName, bool fromScratch=true ) ;
350
351     void writeCSV ( const std::string fileName ) const ;
352
353     friend Field operator* (double value , const Field& field ) ;
354
355     friend Field operator* (const Field& field, double value ) ;
356
357     friend Field operator/ (const Field& field, double value) ;
358
359     friend std::ostream& operator<<(std::ostream& out, const Field& field ) ;
360
361     protected: //----------------------------------------------------------------
362
363     MEDCoupling::MCAuto<MEDCoupling::MEDCouplingFieldDouble> _field;
364     Mesh _mesh ;
365     EntityType _typeField;
366         int _numberOfComponents;
367         double _time;
368         std::string _fieldName;
369         
370     private:
371
372 };
373
374 #endif /* Field_HXX_ */