KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
indirect_scalar.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors:
11 //
12 //
13 
14 #if !defined(KRATOS_INDIRECT_SCALAR_H_INCLUDED)
15 #define KRATOS_INDIRECT_SCALAR_H_INCLUDED
16 
17 // System includes
18 #include <functional>
19 
20 // Project includes
22 #include "includes/node.h"
23 
24 namespace Kratos
25 {
26 
29 
43 template <class T, typename S>
45 {
46 public:
47  IndirectScalar() : set([](T) {}), get([]() -> T { return T{}; })
48  {
49  }
50 
51  IndirectScalar(std::function<void(T)> set, std::function<T()> get)
52  : set(set), get(get)
53  {
54  }
55 
57  {
58  set(value);
59  return *this;
60  }
61 
63  {
64  set(get() + value);
65  return *this;
66  }
67 
69  {
70  set(get() - value);
71  return *this;
72  }
73 
75  {
76  set(get() * value);
77  return *this;
78  }
79 
81  {
82  set(get() / value);
83  return *this;
84  }
85 
86  operator T() const
87  {
88  return get();
89  }
90 
91  std::ostream& print(std::ostream& os) const
92  {
93  return os << get();
94  }
95 
96 private:
97  std::function<void(T)> set;
98  std::function<T()> get;
99 
100  friend class Serializer;
101 
102  void save(Serializer& rSerializer) const
103  {
104  }
105 
106  void load(Serializer& rSerializer)
107  {
108  }
109 };
110 
111 template <class T>
112 std::ostream& operator<<(std::ostream& os, const IndirectScalar<T>& s)
113 {
114  return s.print(os);
115 }
116 
118 
119 template <class TVariableType>
121  const TVariableType& rVariable)
122 {
123  auto fset = [&rNode, &rVariable](typename TVariableType::Type s) {
124  rNode.FastGetSolutionStepValue(rVariable) = s;
125  };
126 
127  auto fget = [&rNode, &rVariable]() -> typename TVariableType::Type {
128  return rNode.FastGetSolutionStepValue(rVariable);
129  };
130 
132 }
133 
134 template <class TVariableType>
136  const TVariableType& rVariable,
137  std::size_t Step)
138 {
139  if (Step == 0)
140  {
141  return MakeIndirectScalar(rNode, rVariable);
142  }
143  // Here we don't capture Step to avoid allocating memory.
144  else if (Step == 1)
145  {
146  auto fset = [&rNode, &rVariable](typename TVariableType::Type s) {
147  rNode.FastGetSolutionStepValue(rVariable, 1) = s;
148  };
149  auto fget = [&rNode, &rVariable]() -> typename TVariableType::Type {
150  return rNode.FastGetSolutionStepValue(rVariable, 1);
151  };
153  }
154  else if (Step == 2)
155  {
156  auto fset = [&rNode, &rVariable](typename TVariableType::Type s) {
157  rNode.FastGetSolutionStepValue(rVariable, 2) = s;
158  };
159  auto fget = [&rNode, &rVariable]() -> typename TVariableType::Type {
160  return rNode.FastGetSolutionStepValue(rVariable, 2);
161  };
163  }
164  else
165  {
166  KRATOS_ERROR << "Invalid Step = " << Step << std::endl;
168  }
169 }
170 
171 } // namespace Kratos.
172 
173 #endif // KRATOS_INDIRECT_SCALAR_H_INCLUDED defined
Wrapper for a function which behaves like an arithmetic type.
Definition: indirect_scalar.h:45
IndirectScalar< T, S > & operator=(const T value)
Definition: indirect_scalar.h:56
IndirectScalar(std::function< void(T)> set, std::function< T()> get)
Definition: indirect_scalar.h:51
IndirectScalar()
Definition: indirect_scalar.h:47
std::ostream & print(std::ostream &os) const
Definition: indirect_scalar.h:91
IndirectScalar< T, S > & operator/=(const T value)
Definition: indirect_scalar.h:80
IndirectScalar< T, S > & operator-=(const T value)
Definition: indirect_scalar.h:68
IndirectScalar< T, S > & operator*=(const T value)
Definition: indirect_scalar.h:74
IndirectScalar< T, S > & operator+=(const T value)
Definition: indirect_scalar.h:62
This class defines the node.
Definition: node.h:65
TVariableType::Type & FastGetSolutionStepValue(const TVariableType &rThisVariable)
Definition: node.h:435
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
IndirectScalar< typename TVariableType::Type > MakeIndirectScalar(Node &rNode, const TVariableType &rVariable)
Definition: indirect_scalar.h:120
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
def load(f)
Definition: ode_solve.py:307