antioch-0.4.0
sigma_bin_converter.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_SIGMA_BIN_MANAGER_H
28 #define ANTIOCH_SIGMA_BIN_MANAGER_H
29 
30 //Antioch
33 
34 //C++
35 #include <vector>
36 #include <iostream>
37 #include <cmath>
38 
39 namespace Antioch{
40 
41 template <typename VectorCoeffType = std::vector<double> >
43 {
44  public:
47 
48  template <typename VectorStateType>
49  void y_on_custom_grid(const VectorCoeffType &x_old, const VectorCoeffType &y_old,
50  const VectorStateType &x_new, VectorStateType &y_new) const;
51 
52  private:
53 
54  template <typename StateType, typename VUIntType>
55  StateType custom_bin_value(const StateType & custom_head, const StateType & custom_tail,
56  const VUIntType & index_heads, unsigned int custom_head_index,
57  const VectorCoeffType & list_ref_head_tails,
58  const VectorCoeffType & list_ref_values) const;
59 
60 };
61 
62 template <typename VectorCoeffType>
63 inline
65 {
66  return;
67 }
68 
69 template <typename VectorCoeffType>
70 inline
72 {
73  return;
74 }
75 
76 template <typename VectorCoeffType>
77 template <typename VectorStateType>
78 inline
79 void SigmaBinConverter<VectorCoeffType>::y_on_custom_grid(const VectorCoeffType &x_old, const VectorCoeffType &y_old,
80  const VectorStateType &x_custom, VectorStateType &y_custom) const
81 {
82 // data consistency
83  antioch_assert_not_equal_to(x_custom.size(),0);
84  antioch_assert_equal_to(y_custom.size(),x_custom.size());
85  antioch_assert_not_equal_to(x_old.size(),0);
86  antioch_assert_equal_to(x_old.size(),y_old.size());
87 
88 
89 // first meta-prog needed stuff
91  unsigned int>::type UIntType;
92  typedef typename Antioch::rebind<VectorStateType,UIntType>::type VUIntType;
93 
94 // find all the indexes of old that are just after all
95 // the custom values
96 // todo: better way to build the indexes container?
97  UIntType example;
98  Antioch::zero_clone(example,x_custom[0]);
99 // two levels needed to obtain correct fixed-sized vexcl vector
100 // within eigen vector (VIntType = Eigen<vexcl<unsigned int> >):
101 // Eigen => can't initialize in constructor VUIntType
102 // vexcl => fixed size accessible only within constructor
103  VUIntType ihead(x_custom.size());
104  Antioch::init_constant(ihead,example);
105 
106  UIntType unfound = Antioch::constant_clone(example,x_old.size()-1);
107 
108  for(unsigned int ic = 0; ic < x_custom.size(); ic++)
109  {
110  UIntType ihigh = Antioch::constant_clone(example,x_old.size()-1);
111  for (unsigned int i = 0; i != x_old.size() - 1; ++i)
112  {
113  UIntType icus = Antioch::constant_clone(example,ic);
114 
115  ihigh = Antioch::if_else (Antioch::eval_index(x_custom,icus) < x_old[i] && ihigh == unfound,
116  Antioch::constant_clone(example,i),
117  ihigh);
118 
119  if(Antioch::conjunction(ihigh != unfound))break; // once we found everyone, don't waste time
120  }
121  ihead[ic] = ihigh;
122  }
123 
124  // bin
125  for(unsigned int ic = 0; ic < x_custom.size() - 1; ic++) // right stairs, last one = 0
126  {
127  y_custom[ic] = this->custom_bin_value(x_custom[ic], x_custom[ic + 1],ihead, ic, x_old, y_old);
128  }
129 
130  return;
131 
132 }
133 
134  template <typename VectorCoeffType>
135  template <typename StateType, typename VUIntType>
136  inline
137  StateType SigmaBinConverter<VectorCoeffType>::custom_bin_value(const StateType & custom_head, const StateType & custom_tail,
138  const VUIntType & index_heads, unsigned int custom_head_index,
139  const VectorCoeffType & list_ref_head_tails,
140  const VectorCoeffType & list_ref_values) const
141  {
142 
143  using std::min;
144  using Antioch::min;
145  using std::max;
146  using Antioch::max;
147 
148  antioch_assert_equal_to(list_ref_head_tails.size(),list_ref_values.size());
149 
150  StateType surf = Antioch::zero_clone(custom_head);
151 
152  typedef typename Antioch::value_type<VUIntType>::type UIntType;
153 
154  UIntType start_head = index_heads[custom_head_index];
155 
156  UIntType value_head = Antioch::if_else(start_head > (typename Antioch::value_type<typename Antioch::value_type<VUIntType>::type>::type)0,
157  (UIntType)(start_head - Antioch::constant_clone(start_head,1)),
158  (UIntType)(index_heads[index_heads.size() - 1])); // right stairs, value never used
159 
160  UIntType ref_end_tail = min((UIntType)(start_head + Antioch::constant_clone(start_head,1)) ,
161  (UIntType)(Antioch::constant_clone(start_head,list_ref_head_tails.size() - 1)));
162 
163  // if StateType is vectorized, it takes care of it here
164  StateType ref_head = Antioch::custom_clone(custom_head,list_ref_head_tails,start_head);
165  StateType ref_tail = Antioch::custom_clone(custom_head,list_ref_head_tails,ref_end_tail);
166  StateType ref_value = Antioch::custom_clone(custom_head,list_ref_values,value_head);
167 
168  // head from custom head to ref head
169  // super not efficient, everything is calculated every time...
170  surf += Antioch::if_else(Antioch::constant_clone(custom_head,list_ref_head_tails[0]) > custom_head ||
171  Antioch::constant_clone(custom_head,list_ref_head_tails[list_ref_head_tails.size() - 1]) < custom_head, // custom is outside ref
172  Antioch::zero_clone(surf),
173  (StateType)
174  Antioch::if_else(ref_head < custom_tail, // custom is within ref bin
175  (StateType)(ref_value * (ref_head - custom_head)),
176  (StateType)(ref_value * (custom_tail - custom_head)))
177  );
178 
179  //body from ref head to ref last tail
180  while(Antioch::disjunction(ref_tail < custom_tail && // ref is below tail (<=> start_head < index_heads[custom_head_index + 1])
181  start_head < Antioch::constant_clone(start_head,list_ref_head_tails.size() - 1))) // ref is still defined
182  {
183  ref_end_tail = min((UIntType)(start_head + Antioch::constant_clone(start_head,1)) ,
184  (UIntType)(Antioch::constant_clone(start_head,list_ref_head_tails.size() - 1)));
185 
186  ref_head = Antioch::custom_clone(custom_head,list_ref_head_tails,start_head);
187  ref_tail = Antioch::custom_clone(custom_head,list_ref_head_tails,ref_end_tail);
188  ref_value = Antioch::custom_clone(custom_head,list_ref_values,start_head);
189 
190  surf += Antioch::if_else(ref_tail < custom_tail && start_head < Antioch::constant_clone(start_head,list_ref_head_tails.size()),
191  (StateType)((ref_tail - ref_head) * ref_value),
192  Antioch::zero_clone(surf));
193 
194  start_head += Antioch::if_else(ref_tail < custom_tail && start_head < Antioch::constant_clone(start_head,list_ref_head_tails.size()),
195  Antioch::constant_clone(start_head,1),
196  Antioch::zero_clone(start_head));
197  }
198 
199  ref_end_tail = min((UIntType)(start_head + Antioch::constant_clone(start_head,1)) ,
200  (UIntType)(Antioch::constant_clone(start_head,list_ref_head_tails.size() - 1)));
201 
202  ref_head = Antioch::custom_clone(custom_head,list_ref_head_tails,start_head);
203  ref_tail = Antioch::custom_clone(custom_head,list_ref_head_tails,ref_end_tail);
204  ref_value = Antioch::custom_clone(custom_head,list_ref_values,start_head);
205 
206  // tail from ref_head to custom_tail
207  // super not efficient, everything is calculated every time...
208  surf += Antioch::if_else(
209  Antioch::constant_clone(custom_tail,list_ref_head_tails[list_ref_head_tails.size() - 1]) < custom_tail || // custom is outside ref
210  Antioch::constant_clone(custom_tail,list_ref_head_tails.front()) > custom_tail || // custom is outside ref
211  ref_head > custom_tail, // custom is fully inside ref bin (already taken into account in head)
212  Antioch::zero_clone(surf),
213  (StateType)(ref_value * (custom_tail - ref_head)));
214 
215  return surf / (custom_tail - custom_head);
216  }
217 }
218 
219 #endif
Antioch::enable_if_c< is_eigen< T >::value, typename value_type< T >::type >::type max(const T &in)
Definition: eigen_utils.h:88
#define antioch_assert_equal_to(expr1, expr2)
StateType custom_bin_value(const StateType &custom_head, const StateType &custom_tail, const VUIntType &index_heads, unsigned int custom_head_index, const VectorCoeffType &list_ref_head_tails, const VectorCoeffType &list_ref_values) const
#define antioch_assert_not_equal_to(expr1, expr2)
bool conjunction(const bool &vec)
T custom_clone(const T &, const VectorScalar &values, unsigned int indexes)
Antioch::enable_if_c< is_eigen< T >::value, typename value_type< T >::type >::type min(const T &in)
Definition: eigen_utils.h:98
enable_if_c< is_eigen< typename value_type< VectorT >::type >::value, typename value_type< VectorT >::type >::type eval_index(const VectorT &vec, const _Matrix< _UIntT, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &index)
Definition: eigen_utils.h:268
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > constant_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex, const Scalar &value)
Definition: eigen_utils.h:181
max(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
enable_if_c< is_eigen< T1 >::value &&is_eigen< T2 >::value, typename state_type< T1 >::type >::type if_else(const Condition &condition, const T1 &if_true, const T2 &if_false)
Definition: eigen_utils.h:250
void y_on_custom_grid(const VectorCoeffType &x_old, const VectorCoeffType &y_old, const VectorStateType &x_new, VectorStateType &y_new) const
void init_constant(Vector &output, const Scalar &example)
The parameters are reduced parameters.
typename _Scalar int _Rows int _Cols int _Options int _MaxRows int _MaxCols inline min(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > zero_clone(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &ex)
Definition: eigen_utils.h:145
bool disjunction(const bool &vec)

Generated on Thu Jul 7 2016 11:09:45 for antioch-0.4.0 by  doxygen 1.8.8