C++ main module for emicrom Package  1.0
EMM_MatterField.hpp
Go to the documentation of this file.
1 #ifndef EMM_MatterField_HPP
2 #define EMM_MatterField_HPP
3 
4 #include "OMP_Thread.h"
5 
6 template<class T>
8 
9 
10 
11 
12  //get the indices of matters at each cell if any
13  const EMM_UIndexArray &matterIndices=mMatterIndices;
14  const SV::EMM_Matter &matters=mMatters;
15 
16  //size of the indices
17  tUIndex iCell,nCells=matterIndices.getSize();
18  if ((nCells==0) || (nCells==1)) {
19  //get the value
20  const tReal* parameters=&(matters[0]->getParameter(k));
21  const tUSInt& dim=matters[0]->getParameterDimension(k);
22 
23  //uniform
24  vs.setSize(dim);
25  T *v=&vs[0];
26 
27  tUSInt d;
28  if (mMatters.getSize()>0) {
29  //there is at least one matter
30  for (d=0;d<dim;d++) {
31  *v=(T) (*parameters);
32  v++;
33  parameters++;
34  }
35  } else {
36  //there is no matter
37  memset(v,0,dim*sizeof(T));
38  }
39  return true;
40  }
41 
42  const tUSInt& dim=matters[0]->getParameterDimension(k);
43  tUSInt d;
44 
45  //set the size of vs
46  vs.setSize(nCells*dim);
47 
48  T *v;
49 
50  OMP_PARALLEL_PRIVATE_SHARED(private(d,iCell,v),shared(cout,dim,nCells,vs,matterIndices,matters,k)) {
51 
52 
53  // thread id
54  tUInteger threadId=OMP_GET_THREAD_ID();
55 
56  // threads number
57  tUInteger nThreads=1;
58  nThreads=OMP_GET_THREADS_NUMBER();
59 
60  //start index
61  tUIndex start=threadId*nCells/nThreads;
62  //end index
63  tUIndex end=(threadId+1)*nCells/nThreads;
64 
65  const tUIndex *matterIndex=&matterIndices[start];
66 
67  tUIndex nMatters=matters.getSize();
68 
69  const tReal* parameters;
70 
71  //iterator on value of the parameter k
72  v=&vs[start*dim];
73 
74  for (iCell=start;iCell<end;iCell++) {//loop on cell
75  if ((*matterIndex)==EMM_MatterField::NO_MATTER) {
76  //no matter at iCell
77  memset(v,0,dim*sizeof(T));
78  v+=dim;
79  } else {
80  //get the matter at iCell
81  if (*matterIndex<nMatters) {
82  //set the value
83  parameters=&matters[*matterIndex]->getParameter(k);
84  for (d=0;d<dim;d++) {
85  *v=(T) (*parameters);
86  v++;
87  parameters++;
88  }
89  } else {//no matter so empty
90  memset(v,0,dim*sizeof(T));
91  v+=dim;
92  }
93  }
94  //next value pointer
95  matterIndex++;
96  }//end loop on cell
97  } //end OMP parallel loop
98 
99 
100  //test if the field is uniform
101  T vs_old[dim];
102  T *vold;
103  vold=vs_old;
104  for (d=0;d<dim;d++) {
105  *vold=(T) 0;
106  vold++;
107  }
108  tBoolean isInitialized=false;
109 
110  tBoolean isUniform=true;
111  iCell=0;
112 
113  v=&vs[0];
114  tReal eps=1.e-12;
115 
116  do {
117  if (!isInitialized) {
118  //initialize not null uniform value
119  vold=vs_old;
120  for (d=0;d<dim;d++) {
121  (*vold)=*v;
122  if ((*v)!=0) isInitialized=true;
123 
124  vold++;
125  v++;
126  }
127  } else {
128  vold=vs_old;
129  for (d=0;d<dim;d++) {
130  if (fabs(1-(*v)/(*vold))>eps) {
131  //v!=v_old
132  isUniform=false;
133  }
134  v++;
135  vold++;
136  }
137  }
138  //next iter
139  iCell++;
140  } while ((iCell<nCells) && (isUniform));
141 
142  if (isUniform) {
143  //resize the array to size dim
144  vs.setSize(dim);
145  v=&vs[0];
146  vold=vs_old;
147  for (d=0;d<dim;d++) {
148  (*v)=(*vold);
149  v++;
150  vold++;
151  }
152  //set capacity to size
153  vs.fitToSize();
154  }
155  return isUniform;
156 
157 }
158 
159 template<class T>
161 
162 
163 
164 
165  //get the indices of matters at each cell if any
166  const EMM_UIndexArray &matterIndices=mMatterIndices;
167  const SV::EMM_Matter &matters=mMatters;
168 
169  //size of the indices
170  tUIndex iCell,nCells=matterIndices.getSize();
171  if ((nCells==0) || (nCells==1)) {
172  //mater is uniform on all cells
173  //dimension of the parameter
174  const tUSInt& dim=matters[0]->getParameterDimension(k);
175  tUSInt d;
176  //the morse array is uniform
177  vs.setUniformSize(1,dim);
178  //copy the values of the parameter of size dim
179  T *v=vs[0];
180  const tReal* parameters=&(matters[0]->getParameter(k));
181 
182  if (mMatters.getSize()>0) {
183  //there is at least one matter
184  for (d=0;d<dim;d++) {
185  *v=(T) (*parameters);
186  parameters++;
187  v++;
188  }
189  } else {
190  //no matter
191  memset(v,0,dim*sizeof(T));
192  }
193  //the parameter is uniform
194  return true;
195  }
196 
197 
198  //get the number of threads
199  tUInteger threadId,nThreads=1;
200  OMP_PARALLEL_SHARED(shared(nThreads)) {
201  if( OMP_GET_THREAD_ID()==0)
202  nThreads=OMP_GET_THREADS_NUMBER();
203  }
204 
205 
206  //dimension of the parameter
207  const tUSInt& dim=matters[0]->getParameterDimension(k);
208  tUSInt d;
209 
210  //set the morse array size
211  vs.setSize(nCells,dim,nThreads);
212  vs.reset();
213 
214  vector<CORE_MorseArrayIterator<T> > iterVs(nThreads);
215 
216  OMP_PARALLEL_PRIVATE_SHARED(private(iCell,d,threadId),shared(nThreads,cout,dim,nCells,vs,iterVs,matterIndices,matters,k)) {
217 
218 
219 
220  // thread id
221  threadId=OMP_GET_THREAD_ID();
222 
223 
224  //start index
225  tUIndex start=threadId*nCells/nThreads;
226  //end index
227  tUIndex end=(threadId+1)*nCells/nThreads;
228 
229  const tUIndex *matterIndex=&matterIndices[start];
230 
231  tUIndex nMatters=matters.getSize();
232 
233  //matter of the cell i
234  const EMM_Matter *matter=null;
235  const tReal* parameters=null;
236 
237  //get the iterator of the morse array for the thread threadId
238  CORE_MorseArrayIterator<T> &iterV=iterVs[threadId];
239  T *v=null;
240  vs.begin(start,threadId,iterV);
241 
242 
243  //memory size of the value for the cell (1 or 0)
244  tUIndex sV;
245 
246  for (iCell=start;iCell<end;iCell++) {//loop on cell
247  //memory size 0 by default
248  sV=0;
249 
250  //register the parameter in case of magnetized cell
251  if ( (*matterIndex)<nMatters) {
252  matter=matters.get(*matterIndex);
253  if (matter!=null) {
254  //not null matter
255  sV=dim;
256  v=iterV.values();
257  parameters=&matter->getParameter(k);
258  for (d=0;d<dim;d++) {
259  (*v)=(T) (*parameters);
260  v++;
261  parameters++;
262  }
263 
264  }
265  }
266  //next value pointer
267  iterV.setSize(sV);
268  ++iterV;
269  //matter of next cell
270  matterIndex++;
271  }//end loop on cell
272  } //end OMP parallel loop
273 
274 
275  //merge the packs
276  vs.merge();
277 
278  //uniformize the morse array
279  return vs.uniformize();
280 
281 }
282 #endif
EMM_UIndexArray mMatterIndices
list of indices of matters element index -> index of matter in mMatters list (NO_MATTER if no matter)...
Definition: EMM_MatterField.h:49
const tUIndex & getSize() const
return the size of the array for reading
Definition: CORE_Array.h:1018
tBoolean getMatterParameterDistribution(const tFlag &k, EMM_Array< T > &vs) const
get the values of a parameter of matters in a field for each cell
Definition: EMM_MatterField.hpp:7
#define OMP_GET_THREAD_ID()
Definition: openMP.h:76
void setUniformSize(const tUIndex &n, const tUIndex &ld)
set the morse array to be uniform of size n x ld
Definition: CORE_MorseArray.h:328
#define tUInteger
Definition: types.h:91
const tReal & getParameter(const tUSInt &k) const
get the parameter at index k for reading
Definition: EMM_Matter.h:377
This class describes a general morse array of size N. A morse array is composed by.
Definition: CORE_MorseArray.h:31
#define OMP_PARALLEL_SHARED(S)
Definition: openMP.h:85
void reset()
init the array to 0
Definition: CORE_MorseArray.h:381
#define tUSInt
Definition: types.h:28
#define tBoolean
Definition: types.h:139
void setSize(const tUIndex &n)
set the size
Definition: CORE_Array.h:292
tBoolean uniformize()
uniformize the array
Definition: CORE_MorseArray.h:558
#define null
Definition: types.h:144
void setSize(const tUIndex &n)
set the size of the element
Definition: CORE_MorseArrayIterator.h:153
This class describes a matter for E-MicroM package.
Definition: EMM_Matter.h:34
void merge()
merge the morse array built by P packs
Definition: CORE_MorseArray.h:530
T * values() const
return the current values
Definition: CORE_MorseArrayIterator.h:129
tBoolean isUniform() const
return true if the field is uniform
Definition: EMM_MatterField.h:195
virtual void fitToSize()
fit the array alocation exactly to size fit the allocation of the array to its size ...
Definition: CORE_Array.hpp:128
SV::EMM_Matter mMatters
list of matters
Definition: EMM_MatterField.h:53
#define tUIndex
Definition: types.h:126
#define OMP_GET_THREADS_NUMBER()
Definition: openMP.h:74
void begin(const tUIndex &start, const tUInteger &iPack, CORE_MorseArrayIterator< T > &iter)
create an iterator to index start for the pack at iPack for creating the morse array ...
Definition: CORE_MorseArray.h:232
#define OMP_PARALLEL_PRIVATE_SHARED(P, S)
Definition: openMP.h:86
This class describes a general array.
Definition: EMM_Array.h:18
#define tReal
Definition: types.h:118
static const tUIndex NO_MATTER
Definition: EMM_MatterField.h:40
This class describes a more array iterator.
Definition: CORE_MorseArrayIterator.h:15
void setSize(const tUIndex &n, const tUIndex &m, const tUInteger &p)
set the size of the morse array
Definition: CORE_MorseArray.h:342
#define tFlag
Definition: types.h:74