Memosa-FVM  0.2
MultiFieldReduction.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifdef FVM_PARALLEL
6 #include <mpi.h>
7 #endif
8 
9 #include "MultiFieldReduction.h"
10 #include "Array.h"
11 #include "OneToOneIndexMap.h"
12 
13 #include <iostream>
14 
16  _arrays()
17 {
18  logCtor();
19 }
20 
22 {
23  logDtor();
24 }
25 
26 ArrayBase&
28 {
29  ArrayMap::const_iterator pos = _arrays.find(&i);
30  if (pos != _arrays.end())
31  {
32  return *pos->second;
33  }
34 
35  ostringstream e;
36  e << "MultiFieldReduction::operator[] No array found";
37  throw CException(e.str());
38 }
39 
40 shared_ptr<ArrayBase>
42 {
43  ArrayMap::const_iterator pos = _arrays.find(&i);
44  if (pos != _arrays.end())
45  {
46  return pos->second;
47  }
48 
49  ostringstream e;
50  e << "MultiFieldReduction::operator[] No array found";
51  throw CException(e.str());
52 }
53 
54 const ArrayBase&
56 {
57  ArrayMap::const_iterator pos = _arrays.find(&i);
58  if (pos != _arrays.end())
59  {
60  return *pos->second;
61  }
62 
63  ostringstream e;
64  e << "MultiFieldReduction::operator[] No array found";
65  throw CException(e.str());
66 }
67 
70 {
71  for(ArrayMap::const_iterator pos=_arrays.begin();
72  pos!=_arrays.end();
73  ++pos)
74  {
75  ArrayBase& myArray = operator[](*pos->first);
76  const ArrayBase& oArray = ofield[*pos->first];
77  myArray += oArray;
78  }
79  return *this;
80 }
81 
82 bool
83 MultiFieldReduction::operator<(const double tolerance) const
84 {
85  foreach(const ArrayMap::value_type& pos, _arrays)
86  {
87  if (! (*pos.second < tolerance)) return false;
88  }
89  return true;
90 }
91 
92 MFRPtr
94 {
95  MFRPtr r(new MultiFieldReduction());
96 
97  foreach(const ArrayMap::value_type& pos, _arrays)
98  {
99  shared_ptr<ArrayBase> aptr = dynamic_pointer_cast<ArrayBase>(pos.second->newCopy());
100  *aptr *= o[*pos.first];
101  r->addArray(*pos.first,aptr);
102  }
103  return r;
104 }
105 
106 MFRPtr
108 {
109  MFRPtr r(new MultiFieldReduction());
110 
111  foreach(const ArrayMap::value_type& pos, _arrays)
112  {
113  shared_ptr<ArrayBase> aptr = dynamic_pointer_cast<ArrayBase>(pos.second->newCopy());
114  aptr->safeDivide(o[*pos.first]);
115  r->addArray(*pos.first,aptr);
116  }
117  return r;
118 }
119 
120 MFRPtr
122 {
123  MFRPtr r(new MultiFieldReduction());
124 
125  foreach(const ArrayMap::value_type& pos, _arrays)
126  {
127  shared_ptr<ArrayBase> aptr = -(*pos.second);
128  r->addArray(*pos.first,aptr);
129  }
130  return r;
131 }
132 
133 MFRPtr
135 {
136  MFRPtr r(new MultiFieldReduction());
137 
138  foreach(const ArrayMap::value_type& pos, _arrays)
139  {
140  shared_ptr<ArrayBase> aptr = dynamic_pointer_cast<ArrayBase>(pos.second->newCopy());
141  aptr->normalize(o[*pos.first]);
142  r->addArray(*pos.first,aptr);
143  }
144  return r;
145 }
146 
147 void
149 {
150  foreach(const ArrayMap::value_type& pos, _arrays)
151  {
152  pos.second->setMax(o[*pos.first]);
153  }
154 }
155 
156 void
157 MultiFieldReduction::limit(const double min, const double max)
158 {
159  foreach(const ArrayMap::value_type& pos, _arrays)
160  {
161  pos.second->limit(min,max);
162  }
163 }
164 
165 void
167 {
168  shared_ptr<ArrayBase> sum;
169 
170  foreach(const ArrayMap::value_type& pos, _arrays)
171  {
172  shared_ptr<ArrayBase> thisSum = pos.second->reduceSum();
173  if (sum)
174  *sum += *thisSum;
175  else
176  sum = thisSum;
177  }
178 
179  foreach(const ArrayMap::value_type& pos, _arrays)
180  {
181  pos.second->setSum(*sum);
182  }
183 
184 
185 }
186 
187 bool
189 {
190  return _arrays.find(&i) != _arrays.end();
191 }
192 
193 void
194 MultiFieldReduction::addArray(const Field& i, shared_ptr<ArrayBase> a)
195 {
196  _arrays[&i]=a;
197 }
198 
199 void
200 MultiFieldReduction::print(ostream &os) const
201 {
202  os << "[";
203 
204  foreach(const ArrayMap::value_type& pos, _arrays)
205  {
206  os << pos.first->getName() << " : ";
207  pos.second->print(os);
208  }
209 
210  os << "]";
211 }
212 
213 void
215 {
216 #ifdef FVM_PARALLEL
217  ArrayMap::const_iterator it;
218  for ( it = _arrays.begin(); it != _arrays.end(); it++ ){
219  ArrayBase& myArray = *(it->second);
220  int count = myArray.getDataSize() / sizeof(double);
221  MPI::COMM_WORLD.Allreduce( MPI::IN_PLACE, myArray.getData(), count, MPI::DOUBLE, MPI::SUM);
222  }
223 #endif
224 
225 }
virtual int getDataSize() const =0
void setMax(const MultiFieldReduction &o)
ArrayBase & operator[](const Field &)
void addArray(const Field &aIndex, shared_ptr< ArrayBase > a)
Definition: Field.h:14
shared_ptr< MultiFieldReduction > operator/(const MultiFieldReduction &o)
shared_ptr< MultiFieldReduction > normalize(const MultiFieldReduction &o)
double max(double x, double y)
Definition: Octree.cpp:18
virtual ArrayBase & safeDivide(const ArrayBase &a)=0
#define logCtor()
Definition: RLogInterface.h:26
MultiFieldReduction & operator+=(const MultiFieldReduction &ofield)
shared_ptr< ArrayBase > getArrayPtr(const Field &)
bool hasArray(const Field &aIndex) const
void limit(const double min, const double max)
#define logDtor()
Definition: RLogInterface.h:33
virtual ArrayBase & normalize(const ArrayBase &a)=0
void print(ostream &os) const
shared_ptr< MultiFieldReduction > operator*(const MultiFieldReduction &o)
virtual void * getData() const =0
shared_ptr< MultiFieldReduction > MFRPtr
bool operator<(const double tolerance) const
double min(double x, double y)
Definition: Octree.cpp:23
shared_ptr< MultiFieldReduction > operator-() const