7 #ifndef __spatial_UnitContinuum_h__ 8 #define __spatial_UnitContinuum_h__ 16 template<
typename T,
unsigned int D>
20 UnitContinuum(
void) { }
21 virtual ~UnitContinuum(
void) { }
23 UnitContinuum<T,D> &operator=(
const UnitContinuum<T,D> &other)
25 m_count = other.m_count;
26 for(
unsigned int i = 0; i < D; i++)
28 m_periodic[i] = other.m_periodic[i];
29 m_size[i] = other.m_size[i];
30 m_halfsize[i] = other.m_halfsize[i];
32 m_cells = other.m_cells;
33 m_totalCount = other.m_totalCount;
37 bool operator==(
const UnitContinuum<T,D> &other)
39 if(m_count != other.m_count) {
return false; }
40 if(m_periodic != other.m_periodic) {
return false; }
41 if(m_size != other.m_size) {
return false; }
42 if(m_totalCount != other.m_totalCount) {
return false; }
43 if(m_cells == other.m_cells) {
return true; }
47 bool operator!=(
const UnitContinuum<T,D> &other)
52 typedef Vector<D,bool> t_periodic;
53 typedef Vector<D,int> t_index;
54 typedef Vector<D,Real> t_span;
55 typedef std::vector<T> t_cells;
57 void create(
const t_index &a_count,
58 const t_periodic &a_periodic,
61 void create(
const t_index &a_count,
65 const T &operator[](
const t_index &a_index)
const;
66 void set(
const t_index &a_index,
const T &value);
67 void set(
const T &value);
70 T &access(
const t_index &a_index);
71 const T &
get(
const t_index &a_index)
const;
74 void index( t_index &a_index,
75 const t_span &a_location)
const;
76 void lower( t_index &a_index,
77 const t_span &a_location)
const;
80 void location( t_span &a_location,
81 const t_index &a_index)
const;
84 const t_span &cellsize(
void)
const {
return m_size; }
85 const t_index &count(
void)
const {
return m_count; }
86 const t_periodic &periodic(
void)
const {
return m_periodic; }
87 unsigned int totalCount(
void)
const {
return m_totalCount; }
91 void iterate(
void (*a_function)(UnitContinuum<T,D> &,
96 std::list<t_index> &a_neighbors,
97 const t_index &a_index)
const;
100 t_index &safe( t_index &a_index)
const;
101 t_span &safe( t_span &a_span)
const;
104 bool safe(
unsigned int a_d,
105 t_index &a_index)
const;
106 unsigned int calc_array_index(
const t_index &a_index)
const;
108 void rIterate(
unsigned int a_d,
110 void (*a_function)(UnitContinuum<T,D> &,
117 unsigned int m_totalCount;
119 t_periodic m_periodic;
124 template<
typename T,
unsigned int D>
125 inline bool UnitContinuum<T,D>::safe(
unsigned int a_d, t_index &a_index)
const 129 if(m_count[a_d] == 1)
131 if(a_index[a_d] != 0)
140 else if(m_count[a_d] == 2)
150 else if(a_index[a_d] > 1)
165 a_index[a_d] = m_count[a_d] - (-a_index[a_d])%m_count[a_d];
176 else if(a_index[a_d] >= m_count[a_d])
180 a_index[a_d] = a_index[a_d]%m_count[a_d];
188 a_index[a_d] = m_count[a_d]-1;
196 template<
typename T,
unsigned int D>
197 inline typename UnitContinuum<T,D>::t_index &UnitContinuum<T,D>::safe(t_index &a_index)
const 199 for(
unsigned int i = 0; i < D; i++)
206 template<
typename T,
unsigned int D>
207 inline typename UnitContinuum<T,D>::t_span &UnitContinuum<T,D>::safe(
208 t_span &a_span)
const 210 for(
unsigned int i = 0; i < D; i++)
215 a_span[i] = modff(a_span[i], &integer);
218 a_span[i] = 1.0 + a_span[i];
227 else if(a_span[i] > 1.0)
237 template<
typename T,
unsigned int D>
239 inline void UnitContinuum<T,D>::iterate(
void (*a_function)(UnitContinuum<T,D> &,
240 t_index &, A), A a_userarg)
243 rIterate(D-1, index, a_function, a_userarg);
246 template<
typename T,
unsigned int D>
248 inline void UnitContinuum<T,D>::rIterate(
unsigned int a_d, t_index &a_index,
249 void (*a_function)(UnitContinuum<T,D> &, t_index &, A), A a_userarg)
251 for(
int i = 0; i < count()[a_d]; i++)
256 a_function(*
this, a_index, a_userarg);
260 rIterate(a_d-1, a_index, a_function, a_userarg);
265 template<
typename T,
unsigned int D>
266 inline void UnitContinuum<T,D>::index(t_index &a_index,
const t_span &a_location)
const 268 for(
unsigned int i = 0; i < D; i++)
270 a_index[i] = (int)(a_location[i] / m_size[i]);
274 template<
typename T,
unsigned int D>
275 inline void UnitContinuum<T,D>::lower(t_index &a_index,
const t_span &a_location)
const 277 for(
unsigned int i = 0; i < D; i++)
279 a_index[i] = (int)((a_location[i] - m_halfsize[i]) / m_size[i]);
283 template<
typename T,
unsigned int D>
284 inline unsigned int UnitContinuum<T,D>::calc_array_index(
285 const t_index &a_index)
const 287 unsigned int index = 0;
288 unsigned int step = 1;
289 for(
int d = D-1; d >= 0; d--)
291 index += a_index[d] * step;
297 template<
typename T,
unsigned int D>
298 inline void UnitContinuum<T,D>::location(t_span &a_location,
const t_index &a_index)
const 300 for(
unsigned int i = 0; i < D; i++)
302 a_location[i] = (Real)a_index[i] * cellsize()[i] + m_halfsize[i];
306 template<
typename T,
unsigned int D>
307 inline void UnitContinuum<T,D>::create(
const t_index &a_count,
311 for(
unsigned int i = 0; i < D; i++)
313 m_periodic[i] =
false;
314 m_count[i] = a_count[i];
315 m_size[i] = 1.0/m_count[i];
316 m_totalCount *= m_count[i];
317 m_halfsize[i] = m_size[i]/2.0;
320 m_cells.resize(m_totalCount, a_init);
323 template<
typename T,
unsigned int D>
324 inline void UnitContinuum<T,D>::create(
const t_index &a_count,
325 const t_periodic &a_periodic,
const T &a_init)
328 for(
unsigned int i = 0; i < D; i++)
330 m_periodic[i] = a_periodic[i];
331 m_count[i] = a_count[i];
332 m_size[i] = 1.0/m_count[i];
333 m_totalCount *= m_count[i];
334 m_halfsize[i] = m_size[i]/2.0;
337 m_cells.resize(m_totalCount, a_init);
340 template<
typename T,
unsigned int D>
341 inline const T &UnitContinuum<T,D>::operator[](
const t_index &a_index)
const 343 t_index safe_index(a_index);
345 return m_cells[calc_array_index(safe_index)];
348 template<
typename T,
unsigned int D>
349 inline T &UnitContinuum<T,D>::access(
const t_index &a_index)
351 return m_cells[calc_array_index(a_index)];
354 template<
typename T,
unsigned int D>
355 inline const T &UnitContinuum<T,D>::get(
const t_index &a_index)
const 357 return m_cells[calc_array_index(a_index)];
360 template<
typename T,
unsigned int D>
361 inline void UnitContinuum<T,D>::set(
const t_index &a_index,
const T &a_value)
363 t_index safe_index(a_index);
365 m_cells[calc_array_index(safe_index)] = a_value;
368 template<
typename T,
unsigned int D>
369 inline void UnitContinuum<T,D>::set(
const T &a_value)
371 for(
unsigned int i = 0; i < m_cells.size(); i++)
373 m_cells[i] = a_value;
377 template<
typename T,
unsigned int D>
378 inline void UnitContinuum<T,D>::addNeighbors(std::list<t_index> &a_neighbors,
379 const t_index &a_index)
const 381 t_index candidate = a_index;
383 if(!(candidate == a_index))
385 feX(
"UnitContinuum<T,D>::addNeighbors",
386 "invalid reference index");
388 for(
unsigned int i = 0; i < D; i++)
390 candidate[i] = a_index[i] + 1;
392 if(candidate[i] != a_index[i])
394 a_neighbors.push_back(candidate);
397 candidate[i] = a_index[i] - 1;
399 if(candidate[i] != a_index[i])
401 a_neighbors.push_back(candidate);
404 candidate[i] = a_index[i];
415 template<
typename T,
unsigned int D>
416 const T &directSample(
const UnitContinuum<T,D> &a_continuum,
const typename UnitContinuum<T,D>::t_span &a_location)
418 typename UnitContinuum<T,D>::t_index idx;
419 a_continuum.index(idx, a_location);
420 return a_continuum[idx];
429 template<
typename T,
unsigned int D>
430 const T &rLinearSample(
unsigned int a_d,
const UnitContinuum<T,3> &a_continuum,
typename UnitContinuum<T,D>::t_index &a_index,
typename UnitContinuum<T,D>::t_index &a_lower,
float a_mult, t_bnd *a_bnd)
434 a_index[a_d] = a_lower[a_d];
435 a_continuum.safe(a_index);
436 m = a_bnd[a_d].m_hi * a_mult;
441 accum += m * a_continuum.access(a_index);
445 accum += rLinearSample<T,D>(a_d-1, a_continuum, a_index, a_lower,
450 a_index[a_d] = a_lower[a_d] + 1;
451 a_continuum.safe(a_index);
452 m = a_bnd[a_d].m_lo * a_mult;
457 accum += m * a_continuum.access(a_index);
461 accum += rLinearSample<T,D>(a_d-1, a_continuum, a_index, a_lower,
471 template<
typename T,
unsigned int D>
472 const T &linearSample(
const UnitContinuum<T,D> &a_continuum,
const typename UnitContinuum<T,D>::t_span &a_location)
476 typename UnitContinuum<T,D>::t_index lower;
477 a_continuum.lower(lower, a_location);
480 for(
unsigned int i = 0; i < D; i++)
482 bnd[i].m_lo = a_location[i]/a_continuum.cellsize()[i]
483 - (Real)lower[i] - 0.5;
484 bnd[i].m_hi = 1.0 - bnd[i].m_lo;
489 typename UnitContinuum<T,D>::t_index index;
490 return rLinearSample<T,D>(D-1, a_continuum, index, lower, r, bnd);
497 template<
unsigned int D>
498 void gradient(UnitContinuum<Real,D> &a_continuum,
typename UnitContinuum<Real,D>::t_span &a_gradient,
const typename UnitContinuum<Real,D>::t_span &a_location, Real a_h = 0.1)
500 for(
unsigned int i = 0; i < D; i++)
502 typename UnitContinuum<Real,D>::t_span lo(a_location);
503 typename UnitContinuum<Real,D>::t_span hi(a_location);
506 a_continuum.safe(lo);
507 a_continuum.safe(hi);
508 Real lo_smpl = linearSample(a_continuum, lo);
509 Real hi_smpl = linearSample(a_continuum, hi);
510 a_gradient[i] = (hi_smpl - lo_smpl)/(2.0*a_h);
514 template<
typename T,
unsigned int D>
517 UnitContinuum<T,D> *m_vectors;
520 template<
typename T,
unsigned int D>
521 void derive_fun(UnitContinuum<Real,D> &a_continuum,
typename UnitContinuum<Real,D>::t_index &a_index, t_unit_derive<T,D> *a_data)
523 for(
unsigned int d = 0; d < D; d++)
525 typename UnitContinuum<Real,D>::t_index dn(a_index);
527 typename UnitContinuum<Real,D>::t_index up(a_index);
529 Real value = (a_continuum[up] - a_continuum[dn]) /
530 (2.0*a_continuum.cellsize()[d]);
531 setAt(a_data->m_vectors->access(a_index), d, value);
537 template<
typename T,
unsigned int D>
538 void derive(UnitContinuum<T,D> &a_vector, UnitContinuum<Real,D> &a_scalar)
541 a_vector.create(a_scalar.count(), a_scalar.periodic(), dummy);
542 t_unit_derive<T,D> derive_data;
543 derive_data.m_vectors = &a_vector;
544 a_scalar.iterate(derive_fun<T,D>, &derive_data);
kernel
Definition: namespace.dox:3
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString's (reverse logic)
Definition: DualString.h:229
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString's.
Definition: DualString.h:208