Salome HOME
24eea36547c4b20edcb970ad34b5bc58bb17e519
[tools/medcoupling.git] / doc / user / doxygen / doxfiles / reference / arrays / arrays.dox
1
2 /*!
3
4 \page arrays Arrays of data
5
6 [TOC]
7
8 \section MEDCouplingArrayIntro Introduction
9
10 One of the most basic concept mainly used all over MEDCoupling is the
11 MEDCoupling array.
12
13 This concept is used all over the
14 MEDCoupling, \ref parallel "ParaMEDMEM", and \ref medloader "MEDLoader" modules so it should be correctly
15 understood to efficiently deal with \ref meshes "Meshes" and \ref fields "Fields".
16
17 \ref ParaMEDMEM::DataArray "DataArrays" are the atomic element of potentially heavy-memory objects in 
18 the 3 modules mentionned above.
19
20 There are for the moment two types of arrays :
21  - double precision float (64 bits) array incarnated by \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble class".
22  - signed integer (32 bits) array incarnated by \ref ParaMEDMEM::DataArrayInt "DataArrayInt class".
23
24 \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble" and \ref ParaMEDMEM::DataArrayInt "DataArrayInt" classes inherits from
25 \ref ParaMEDMEM::DataArray "DataArray" \b non \b instantiable \b class that factorizes some common methods of inherited instantiable classes.
26
27 In the rest of the documentation \b DataArray will be used for both \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble" and \ref ParaMEDMEM::DataArrayInt "DataArrayInt".
28
29 \section MEDCouplingArrayBasics Basics concepts of the DataArrays.
30
31 It will be presented in this section common concept shared by the two classes to \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble" and \ref ParaMEDMEM::DataArrayInt "DataArrayInt".
32
33 \subsection MEDCouplingArrayBasicsName Name
34
35 A \ref ParaMEDMEM::DataArray "DataArray" instance has an attribute **name**.
36
37 **name** is particularly useful for \ref ParaMEDMEM::DataArray "DataArray" representing profiles, families, groups, fields in MEDLoader.
38 But excepted these useful usecases, **name** attribute is often ignored when \ref ParaMEDMEM::DataArray "DataArrays" are aggregated (field array, connectivity, coordinates) in a bigger object.
39 Whatever the usage of the **name** attribute of \ref ParaMEDMEM::DataArray "DataArrays", all methods in ParaMEDMEM::DataArrayDouble and ParaMEDMEM::DataArrayInt class deal with **name** as they do for components names.
40
41 \subsection MEDCouplingArrayBasicsTuplesAndCompo Raw data, tuples and components of DataArrays.
42
43 The main goal of \ref ParaMEDMEM::DataArray "DataArray" is to store contiguous vector of atomical elements with same basic datatype (signed integers, double precision...). This vector of atomical elements is called **raw data** of \ref ParaMEDMEM::DataArray "DataArray".
44
45 The size of this vector of data is called <em>"number of elements"</em>. So the number of bytes stored by a \ref ParaMEDMEM::DataArray "DataArray" instance, is equal to
46 the  product of the __number of elements__ * __constant size of DataType__ .
47
48 As \ref ParaMEDMEM::DataArray "DataArray" instances are designed to store vector fields, tensor fields, coordinate of nodes, the notion of _components_ has been added.
49
50 So, \ref ParaMEDMEM::DataArray "DataArrays" have an additional attribute that is number of components that represent the size of a contiguous set of atomical elements.
51 The vector of atomical elements stored into \ref ParaMEDMEM::DataArray "DataArrays" are grouped in contiguous memory set of atomical elements having each same size.
52
53 The contiguous set of atomical elements is called **tuple**. And each **tuple** stored in raw data, has a length exactly equal to the number of components of
54 \ref ParaMEDMEM::DataArray "DataArray" storing it.
55
56 Thus :
57
58 \f[
59  N_{elements}=N_{tuples}*N_{components}.
60 \f]
61
62 \f[
63  N_{bytes}=N_{elements}*sizeof(DataType)=N_{tuples}*N_{components}*sizeof(DataType).
64 \f]
65
66 In other words, **raw data** of \ref ParaMEDMEM::DataArray "DataArrays" can be seen as a dense matrix, whose number of components would be the row size and number of tuples
67 would be the column size. In this point of view of \ref ParaMEDMEM::DataArray "DataArrays" a **tuple** is represented by the corresponding row in the dense matrix.
68
69 Typically in the **raw data** of  \ref ParaMEDMEM::DataArray "DataArrays" **number of tuples** is highly bigger than **number of components** !
70
71 To finish, raw data is stored tuples by tuples, in another words, in **full interlace mode**, which is the natural storage strategy in C/C++ world.
72
73 For example, let's consider a DataArray having 3 components (called *x* for the first component, *y* for the second, and *z* for the third) and composed by 5 tuples.
74 \n The *raw data* of the DataAarray instance will be organized in memory like that : \f$ x_0,y_0,z_0,x_1,y_1,z_1,x_2,y_2,z_2,x_3,y_3,z_3,x_4,y_4,z_4 \f$.
75
76
77 \subsection MEDCouplingArrayBasicsCompoName Information on components name.
78
79 As seen in the sub section above, a \ref ParaMEDMEM::DataArray "DataArray" instance has a defined number of components.
80
81 There is an information attached to each of these components constituting the \ref ParaMEDMEM::DataArray "DataArray".
82
83 This information is concretely a string of characters that allows, if needed, to give information about the corresponding component.
84
85 The format chosen in **MEDCoupling** for information on is "MY_COMPO_INFO [MYUNIT]". If needed, the unit attached to the component
86 should be put between "[" and "]" after the information of the components after one space character.
87
88 \subsection MEDCouplingArrayBasicsTimeLabel DataArrays and TimeLabel.
89
90 \ref ParaMEDMEM::DataArray "DataArrays instances" can consume big amount of data in memory so they inherit from \ref MEDCouplingTimeLabelPage "TimeLabel".
91 So in C++ it is a good practice to use :
92 - \c getConstPointer method in readonly access.
93 - \c getPointer method only if write is needed.
94
95 If the user in C++ or Python wants to modify intensively its **big** \ref ParaMEDMEM::DataArray "DataArray" instance **not** using raw data pointer it is better to invoke
96 \c setIJSilent just after invocation of \c declareAsNew instead of calling \c setIJ method that will increment time label of \ref ParaMEDMEM::DataArray "DataArray" instance
97 on each call.
98
99 \c setIJ method usage should be reduced to little modification sessions.
100
101 \section MEDCouplingArrayBuildFromScratch Building an array from scratch
102
103 Here is a description of typical usages of \ref ParaMEDMEM::DataArrayDouble "MEDCoupling arrays".
104
105 \if ENABLE_EXAMPLES
106 \ref MEDCouplingArraySteps1 "Here is a C++ example."<br>
107 \ref MEDCouplingArraySteps0 "Here is a Python example."<br>
108 \endif
109
110 \section MEDCouplingArrayBasicsCopy Copy DataArrays
111
112 As \ref ParaMEDMEM::DataArray "DataArrays" are the atomic entity of potentially big memory objects into \ref medcoupling "MEDCoupling"
113 , \ref ParaMEDMEM::DataArray "DataArrays" introduces concepts of copy and comparison that will be used by aggregating classes.
114
115 For more complex objects (that aggregate themselves big objects)
116 like ParaMEDMEM::MEDCouplingFieldDouble the concept of copy (shallow or deep) is less straight forward because which aggregated subobjects are copied or not.
117
118 \subsection MEDCouplingArrayBasicsCopyDeep Deep copy of DataArray
119
120 As for all potentially heavy memory consumer objects in \ref medcoupling "MEDCoupling", \ref ParaMEDMEM::DataArray "DataArrays" implement
121  method \c deepCpy. This method deeply copies an instance. The life cycle of the returned object is *fully* independent from the instance on which the method
122 \c deepCpy has been invoked.
123
124 \if ENABLE_EXAMPLES
125 \ref cpp_mcdataarray_deepcopy "Here is a C++ example."<br>
126 \endif
127
128 \subsection MEDCouplingArrayBasicsCopyShallow Shallow copy of DataArray
129
130 As \ref ParaMEDMEM::DataArray "DataArrays" are the atomic entity of potentially big memory objects into \ref medcoupling "MEDCoupling", the shallow copy
131 simply returns the same object with the reference counter incremented.
132
133 \if ENABLE_EXAMPLES
134 \ref cpp_mcdataarray_shallowcopy "Here is a C++ example."<br>
135 \endif
136
137 \section MEDCouplingArrayBasicsCompare Compare DataArrays
138
139 Comparison is \ref medcoupling "MEDCoupling" is a concept highly sensitive because big amount of tests uses this to state about the success or the fail of these tests.
140 There are two types of comparison :
141
142 - strict, that compares strictly all the non mutable attributes (state sensitive). Methods to perform this strict comparison are :
143   - ParaMEDMEM::DataArrayInt::isEqual
144   - ParaMEDMEM::DataArrayDouble::isEqual.
145
146 - less strict, that focus only on non string attributes. Methods to perform less strict comparison are :
147   - ParaMEDMEM::DataArrayInt::isEqualWithoutConsideringStr
148   - ParaMEDMEM::DataArrayDouble::isEqualWithoutConsideringStr
149
150 \section MEDCouplingArrayFill Filling DataArray with values
151
152 Both DataArrayDouble and DataArrayInt provide comfort methods that
153 fill the array with some values. These methods are:
154 - ParaMEDMEM::DataArrayInt::fillWithZero and
155   ParaMEDMEM::DataArrayDouble::fillWithZero which assigns zero to all
156   values in array.
157 - ParaMEDMEM::DataArrayInt::fillWithValue and
158   ParaMEDMEM::DataArrayDouble::fillWithValue which assigns a certain value to all
159   values in array.
160 - ParaMEDMEM::DataArrayInt::iota() and
161   ParaMEDMEM::DataArrayDouble::iota() which assigns incrementing values to all
162   values in array.
163
164 \section MEDCouplingArrayApplyFunc Application of a function on DataArrayDouble instances.
165
166 This section is only dedicated for \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble instances".
167
168 It is possible to apply to \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble instance" a function given by a string.
169
170 There are different API for applyFunc* methods of \ref ParaMEDMEM::DataArrayDouble "DataArrayDouble class".
171
172 \subsection MEDCouplingArrayApplyFuncExpr Expressions supported
173
174 In order to reduce as much as possible dependencies, a little dynamic formula interpreter has been developed into INTERP_KERNEL.
175 This dynamic expression evaluator can deal the following exhaustive list :
176
177 - +,-,*,^ (^ for exponent 3^2==9)
178 - sin,cos,tan,sqrt,abs,exp,max,min,ln (neper logarithm), log (neper logarithm), log10 (decimal logarithm),
179 - >,<
180 - if
181
182 The expression evaluator is also sensitive to the following var pattern : IVec, JVec, KVec, LVec,... ,ZVec
183
184 - IVec stands for unitary vector [1,0,0,0,...]
185 - JVec stands for unitary vector [0,1,0,0,...]
186 - KVec stands for unitary vector [0,0,1,0,...]
187 - ...
188
189 The dynamic expression evaluator works tuple by tuple through the *raw data* of DataArrayDouble instance.
190
191 The principle of the dynamic expression evaluator is the following :
192
193 - Given the input string a compilation tree is built whose leaves are either constants or variables.
194   At this phase only syntax errors are thrown.
195 \anchor MEDCouplingArrayApplyFuncExprA1
196 - Then given the computed tree, a link phase is performed to accelerate evaluation. At this phase the incoherence between the number of
197   components and the number of variables are detected.
198
199 - The given the preprocessed tree given an input tuple a preallocated tuple is fed with the result of the evaluation.
200   At this last phase only mathematical errors are thrown (division by 0, log(0), sqrt of a negative number ...)
201
202 \subsection MEDCouplingArrayApplyFunc0 applyFunc method with only one parameter
203
204 This method produces a newly allocated DataArrayDouble instance having exactly the same number of components **and** number of tuples than the instance on which the
205 \ref ParaMEDMEM::DataArrayDouble::applyFunc(const std::string &, bool) const applyFunc method is applied.
206
207 **This method is useful when the evaluation expression do not need to consider the components of each tuple separately**.
208
209 That's why this method of \ref ParaMEDMEM::DataArrayDouble::applyFunc(const std::string &, bool) const applyFunc method with one parameter accepts at most only one variable.
210
211 If it is not the case an exception is thrown as seen here :
212
213 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_1
214
215 Let's take a very simple example on a DataArrayDouble instance \c d having 4 tuples and 2 components.
216
217 In the next example the expression contains only one variable : \c smth.
218 So \c smth represent a tuple of size 2.
219
220 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_2
221
222 As the example shows, the output \c d1 has 2 components as \c d.
223
224 Whereas all the components of the input of \c d be not considered separately, it is also, possible with \ref ParaMEDMEM::DataArrayDouble::applyFunc(const std::string &, bool) const applyFunc method with one parameter
225 to build an output having same number of components than input but where components in input are treated separately.
226
227 Let's build an example using DataArrayDouble instance \c d defined just above.
228
229 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_3
230
231 In this example using IVec and JVec it is possible to differentiate output in component #0 and output in component #1 for DataArrayDouble instance \c d2.
232
233 \subsection MEDCouplingArrayApplyFunc1 applyFunc method with only two parameters
234
235 This method also returns a newly allocated DataArrayDouble instance having the same number of tuples than the DataArrayDouble instance on which \ref ParaMEDMEM::DataArrayDouble::applyFunc(int,const std::string &, bool) const applyFunc method is called, but the contrary to previous \ref MEDCouplingArrayApplyFunc0 "applyFunc with one parameter version" here the number of components is set by the user.
236
237 The big difference with \ref MEDCouplingArrayApplyFunc0 "applyFunc method with one parameter" seen above is that here components of tuples are treated separately.
238
239 The method that implements it is \ref ParaMEDMEM::DataArrayDouble::applyFunc(int,const std::string &, bool) const here.
240
241 Here the number of variables appearing in the expression should be equal at most to the number of component of the DataArrayDouble instance on which \ref ParaMEDMEM::DataArrayDouble::applyFunc(int,const std::string &, bool) const applyFunc method is called.
242
243 Let's consider the following DataArrayDouble having 4 tuples with 3 components called dd.
244
245 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_4
246
247 If you intend to create a new DataArrayDouble instance called \c dd1 having only one component that is the result of the sum of first component and the square root of the second component and the third component
248 the invocation should be something like this :
249
250 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_5
251
252 \warning In the expression \c "f+sqrt(g)+h", there are 3 variables \c {"g","h","f"}. As seen \ref MEDCouplingArrayApplyFuncExprA1 "in link phase in expression evaluator" it is needed to match a variable to
253 the component id. The strategy of expression evaluator is the following. Sort ascendingly variables using their names and affect component id following this sorted list. It leads to :
254 - \c f will be attached to component #0 of \c dd
255 - \c g will be attached to component #1 of \c dd
256 - \c h will be attached to component #2 of \c dd
257
258 Considering the previous warning, let's try to perform an application of function to compute in a DataArrayDouble instance called \c dd2 starting by adding component #0 and component #2
259 of \c dd.
260 \n
261 The expression \c "a+c" will add component #0 to component #1 as seen in warning section !!!! It can appear silly, but this strategy has been chosen in order to support different set of variables.
262 \n \ref ParaMEDMEM::DataArrayDouble::applyFunc2 "applyFunc2" and \ref ParaMEDMEM::DataArrayDouble::applyFunc3 "applyFunc3" methods have been developed to remedy to that feature that can be surprising.
263 \n These two methods are explained respectively \ref MEDCouplingArrayApplyFunc2 "here for applyFunc2" and \ref MEDCouplingArrayApplyFunc3 "here for applyFunc3".
264
265 Whatever it is possible to find a workaround using \ref ParaMEDMEM::DataArrayDouble::applyFunc(int,const std::string &, bool) const applyFunc with 2 parameters.
266 \n Here is a solution to compute \c dd2 :
267
268 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_6
269
270 \subsection MEDCouplingArrayApplyFunc2 applyFunc2 method
271
272 The method that implements it is \ref ParaMEDMEM::DataArrayDouble::applyFunc2 here.
273
274 This method is very close to \ref MEDCouplingArrayApplyFunc1 "applyFunc method with only two parameters".
275
276 The only difference is the mapping between variables found in expression and tuple id. Rather than using rank in string sorting as \ref MEDCouplingArrayApplyFunc1 "applyFunc method with only two parameters uses" here the component information are considered.
277
278 Let's consider DataArrayDouble instance \c ddd constituted with 4 tuples containing each 3 components. The components are named respectively \c {"Y","AA","GG"} with following different units attached on them.
279
280 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_7
281
282 To compute the sum of the first component (component #0) and the third component (component #2) simply do that :
283
284 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_8
285
286 \subsection MEDCouplingArrayApplyFunc3 applyFunc3 method
287
288 The method that implements it is \ref ParaMEDMEM::DataArrayDouble::applyFunc3 here.
289
290 This method is very close to \ref MEDCouplingArrayApplyFunc1 "applyFunc method with only two parameters" and \ref MEDCouplingArrayApplyFunc2 "applyFunc2".
291
292 The only difference is the mapping between variables found in expression and tuple id. Rather than using rank in string sorting as in \ref MEDCouplingArrayApplyFunc1 "applyFunc method with only two parameters uses" or the component information as in \ref MEDCouplingArrayApplyFunc2 "applyFunc2", here an explicit vector is given in input.
293
294 Let's consider DataArrayDouble instance \c ddd constituted with 4 tuples containing each 3 components. To add first component (component #0) and the third component (component #2) simply do that :
295
296 \snippet MEDCouplingExamplesTest.py PySnippetDataArrayApplyFunc1_9
297
298 */