damping_sampling.h Source File

CPP API: damping_sampling.h Source File
damping_sampling.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_EPI_DAMPING_SAMPLING_H
21 #define MIO_EPI_DAMPING_SAMPLING_H
22 
25 
26 namespace mio
27 {
28 
36 template <typename FP>
38 {
39 public:
49  template <class V>
50  DampingSampling(const UncertainValue<FP>& value, DampingLevel level, DampingType type, SimulationTime<FP> time,
51  const std::vector<size_t> matrices, const Eigen::MatrixBase<V>& groups)
52  : m_value(value)
53  , m_level(level)
54  , m_type(type)
55  , m_time(time)
56  , m_matrices(matrices)
57  , m_groups(groups)
58  {
59  }
60 
67  {
68  return m_value;
69  }
71  {
72  return m_value;
73  }
81  {
82  m_value = v;
83  }
84 
89  DampingLevel get_level() const
90  {
91  return m_level;
92  }
93 
98  void set_level(DampingLevel l)
99  {
100  m_level = l;
101  }
102 
107  DampingType get_type() const
108  {
109  return m_type;
110  }
111 
116  void set_type(DampingType t)
117  {
118  m_type = t;
119  }
120 
126  {
127  return m_time;
128  }
129 
135  {
136  m_time = t;
137  }
138 
144  const std::vector<size_t>& get_matrix_indices() const
145  {
146  return m_matrices;
147  }
148 
153  void set_matrix_indices(const std::vector<size_t>& v)
154  {
155  m_matrices = v;
156  }
157 
163  const Eigen::VectorX<FP>& get_group_weights() const
164  {
165  return m_groups;
166  }
172  template <class V>
173  void set_group_weights(const Eigen::MatrixBase<V>& v)
174  {
175  m_groups = v;
176  }
177 
181  void draw_sample()
182  {
184  }
185 
190  bool operator==(const DampingSampling& other) const
191  {
192  return m_value == other.m_value && m_level == other.m_level && m_type == other.m_type &&
193  m_time == other.m_time && m_matrices == other.m_matrices && m_groups == other.m_groups;
194  }
195  bool operator!=(const DampingSampling& other) const
196  {
197  return !(*this == other);
198  }
205  template <class IOContext>
206  void serialize(IOContext& io) const
207  {
208  auto obj = io.create_object("DampingSampling");
209  obj.add_element("Time", get_time());
210  obj.add_element("Type", get_type());
211  obj.add_element("Level", get_level());
212  obj.add_element("Value", get_value());
213  obj.add_list("MatrixIndices", get_matrix_indices().begin(), get_matrix_indices().end());
214  obj.add_element("GroupWeights", get_group_weights());
215  }
216 
221  template <class IOContext>
223  {
224  auto obj = io.expect_object("DampingSampling");
225  auto ti = obj.expect_element("Time", Tag<SimulationTime<FP>>{});
226  auto ty = obj.expect_element("Type", Tag<DampingType>{});
227  auto l = obj.expect_element("Level", Tag<DampingLevel>{});
228  auto v = obj.expect_element("Value", Tag<UncertainValue<FP>>{});
229  auto m = obj.expect_list("MatrixIndices", Tag<size_t>{});
230  auto g = obj.expect_element("GroupWeights", Tag<Eigen::VectorX<FP>>{});
231  return apply(
232  io,
233  [](auto&& ti_, auto&& ty_, auto&& l_, auto&& v_, auto&& m_, auto&& g_) {
234  return DampingSampling(v_, l_, ty_, ti_, m_, g_);
235  },
236  ti, ty, l, v, m, g);
237  }
238 
239 private:
241  DampingLevel m_level;
242  DampingType m_type;
244  std::vector<size_t> m_matrices;
245  Eigen::VectorX<FP> m_groups;
246 };
247 
255 template <class DampingExpression, class DampingSamplings, class F>
256 void apply_dampings(DampingExpression& damping_expression, const DampingSamplings& dampings, F make_matrix)
257 {
258  damping_expression.set_automatic_cache_update(false);
259  for (auto& d : dampings) {
260  for (auto& i : d.get_matrix_indices()) {
261  auto m = make_matrix(d.get_value().value() * d.get_group_weights());
262  damping_expression[i].add_damping(m, d.get_level(), d.get_type(), d.get_time());
263  }
264  }
265  damping_expression.set_automatic_cache_update(true);
266 }
267 
281 template <class V>
283 {
284  auto ones_v = std::decay_t<V>::PlainObject::Constant(groups.size(), 1.0);
285  auto prod = (ones_v - groups) * (ones_v.transpose() - groups.transpose());
286  auto ones_m = decltype(prod)::PlainObject::Constant(groups.size(), groups.size(), 1.0);
287  return ones_m - sqrt(prod.array()).matrix();
288 }
289 
298 template <typename FP, class V>
300 {
301  return Eigen::VectorX<FP>::NullaryExpr(shape.size(), [shape, groups = std::forward<V>(groups)](Eigen::Index i) {
302  auto num_groups = groups.size();
303  auto num_compartments = size_t(shape.size()) / num_groups;
304  return groups[size_t(i) / num_compartments];
305  });
306 }
307 
308 } // namespace mio
309 
310 #endif // MIO_EPI_DAMPING_SAMPLING_H
shape of a column vector.
Definition: matrix_shape.h:243
Eigen::Index size() const
number of rows.
Definition: matrix_shape.h:286
randomly sample dampings for e.g.
Definition: damping_sampling.h:38
SimulationTime< FP > get_time() const
Get the time the damping becomes active.
Definition: damping_sampling.h:125
void set_group_weights(const Eigen::MatrixBase< V > &v)
Set the group weights.
Definition: damping_sampling.h:173
void set_level(DampingLevel l)
Set the damping level.
Definition: damping_sampling.h:98
void set_type(DampingType t)
Set the damping type.
Definition: damping_sampling.h:116
const std::vector< size_t > & get_matrix_indices() const
Get a list of matrix indices that the damping applies to.
Definition: damping_sampling.h:144
DampingLevel get_level() const
Get the damping level.
Definition: damping_sampling.h:89
DampingType get_type() const
Get the damping type.
Definition: damping_sampling.h:107
void draw_sample()
draw a value from the distribution.
Definition: damping_sampling.h:181
bool operator==(const DampingSampling &other) const
equality comparison operators.
Definition: damping_sampling.h:190
DampingLevel m_level
Definition: damping_sampling.h:241
void set_matrix_indices(const std::vector< size_t > &v)
Set a list of matrix indices that the damping applies to.
Definition: damping_sampling.h:153
DampingSampling(const UncertainValue< FP > &value, DampingLevel level, DampingType type, SimulationTime< FP > time, const std::vector< size_t > matrices, const Eigen::MatrixBase< V > &groups)
Creates a DampingSampling.
Definition: damping_sampling.h:50
const UncertainValue< FP > & get_value() const
Get the random value.
Definition: damping_sampling.h:66
void set_value(const UncertainValue< FP > &v)
Set the random value.
Definition: damping_sampling.h:80
bool operator!=(const DampingSampling &other) const
equality comparison operators.
Definition: damping_sampling.h:195
const Eigen::VectorX< FP > & get_group_weights() const
Get the group weights.
Definition: damping_sampling.h:163
static IOResult< DampingSampling > deserialize(IOContext &io)
deserialize an object of this class.
Definition: damping_sampling.h:222
void serialize(IOContext &io) const
serialize this.
Definition: damping_sampling.h:206
UncertainValue< FP > m_value
Definition: damping_sampling.h:240
void set_time(SimulationTime< FP > t)
Set the time the damping becomes active.
Definition: damping_sampling.h:134
SimulationTime< FP > m_time
Definition: damping_sampling.h:243
Eigen::VectorX< FP > m_groups
Definition: damping_sampling.h:245
std::vector< size_t > m_matrices
Definition: damping_sampling.h:244
UncertainValue< FP > & get_value()
Get the random value.
Definition: damping_sampling.h:70
DampingType m_type
Definition: damping_sampling.h:242
double simulation time.
Definition: damping.h:58
FP draw_sample()
Sets the value by sampling from the distribution and returns the new value.
Definition: uncertain_value.h:181
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_sqrt< AD_TAPE_REAL > > sqrt(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:1023
trait_value< T >::RETURN_TYPE & value(T &x)
Definition: ad.hpp:3308
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
requires details::IsElementReference< M > RowMajorIterator< M, false > end(M &m)
create a non-const end iterator for the matrix m.
Definition: eigen_util.h:449
void apply_dampings(DampingExpression &damping_expression, const DampingSamplings &dampings, F make_matrix)
add sampled dampings to a damping expression.
Definition: damping_sampling.h:256
boost::outcome_v2::in_place_type_t< T > Tag
Type that is used for overload resolution.
Definition: io.h:407
auto i
Definition: io.h:809
details::ApplyResultT< F, T... > apply(IOContext &io, F f, const IOResult< T > &... rs)
Evaluate a function with zero or more unpacked IOResults as arguments.
Definition: io.h:481
auto make_contact_damping_matrix(V &&groups)
Make a contact damping matrix from dampings by group.
Definition: damping_sampling.h:282
auto make_mobility_damping_vector(ColumnVectorShape< FP > shape, V &&groups)
Make mobility coefficient damping vector from dampings by group.
Definition: damping_sampling.h:299
requires details::IsElementReference< M > RowMajorIterator< M, false > begin(M &m)
create a non-const iterator to first element of the matrix m.
Definition: eigen_util.h:421
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353