MoorDyn
State.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, Jose Luis Cercos-Pita <jlc@core-marine.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
35 #pragma once
36 
37 #include "Misc.hpp"
38 #include "IO.hpp"
39 #include "Instance.hpp"
40 #include <Eigen/Dense>
41 #include <map>
42 #include <string>
43 #include <utility>
44 #include <sstream>
45 
46 namespace moordyn {
47 
48 namespace state {
49 
53 class DECLDIR State final : public moordyn::io::IO
54 {
55  public:
60  : moordyn::io::IO(log)
61  {
62  }
63 
67  State(const State& rhs)
68  : moordyn::io::IO(rhs._log)
69  {
70  copy(rhs);
71  }
72 
75  ~State() { clear(); };
76 
81  void addInstance(moordyn::Instance* obj);
82 
88  unsigned int removeInstance(moordyn::Instance* obj);
89 
92  inline StateVarView get()
93  {
94  return _var(Eigen::seq(0, Eigen::placeholders::last));
95  }
96 
103  {
104  const auto i = indexer(obj);
105  return _var(i).topLeftCorner(obj->stateN(), obj->stateDims());
106  }
107 
115  inline const Eigen::Index indexer(moordyn::Instance* obj)
116  {
117  const size_t id = obj->id();
118  if ((id >= _indexes.size()) || (_indexes[id] < 0)) {
119  throw moordyn::invalid_value_error("Missing instance");
120  }
121  return Eigen::Index(_indexes[id]);
122  }
123 
124  inline const std::vector<Eigen::Index> indexer(
125  std::vector<moordyn::Instance*> obj)
126  {
127  std::vector<Eigen::Index> slcs;
128  slcs.reserve(obj.size());
129  for (auto o : obj) {
130  slcs.push_back(indexer(o));
131  }
132  return slcs;
133  }
134 
148  std::vector<uint64_t> Serialize(void);
149 
156  uint64_t* Deserialize(const uint64_t* data);
157 
161  inline State& operator=(const State& rhs)
162  {
163  copy(rhs);
164  return *this;
165  }
166 
167  private:
170  void clear();
171 
178  void copy(const State& visitor);
179 
185  inline std::vector<int> make_indexes()
186  {
187  std::vector<int> indexes;
188  for (size_t i = 0; i < _objs.size(); i++) {
189  size_t key = _objs[i]->id();
190  if (indexes.size() <= key) {
191  indexes.resize(key + 1, -1);
192  }
193  indexes[key] = i;
194  }
195  return indexes;
196  }
197 
199  StateVar _var;
200 
202  std::vector<moordyn::Instance*> _objs;
203 
205  std::vector<int> _indexes;
206 };
207 
208 } // ::state
209 
210 } // ::moordyn
211 
212 inline moordyn::StateVar
213 operator*(moordyn::StateVarView v, moordyn::real f)
214 {
215  moordyn::StateVar out;
216  out.resize(v.rows());
217  for (unsigned int i = 0; i < v.rows(); i++) {
218  out(i).resize(v(i).rows(), v(i).cols());
219  out(i) = v(i) * f;
220  }
221  return out;
222 }
223 
224 inline moordyn::StateVar
225 operator*(moordyn::real f, moordyn::StateVarView v)
226 {
227  return v * f;
228 }
229 
230 inline moordyn::StateVar
231 operator*(moordyn::StateVar v, moordyn::real f)
232 {
233  moordyn::StateVar out;
234  out.resize(v.rows());
235  for (unsigned int i = 0; i < v.rows(); i++) {
236  out(i).resize(v(i).rows(), v(i).cols());
237  out(i) = v(i) * f;
238  }
239  return out;
240 }
241 
242 inline moordyn::StateVar
243 operator*(moordyn::real f, moordyn::StateVar v)
244 {
245  return v.topLeftCorner(v.rows(), v.cols()) * f;
246 }
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A generic instance.
Definition: Instance.hpp:55
virtual const size_t stateN() const
Get the number of state variables required by this instance.
Definition: Instance.hpp:101
const size_t id() const
Get the unique identifier of this instance.
Definition: Instance.hpp:76
virtual const size_t stateDims() const
Get the dimension of the state variable.
Definition: Instance.hpp:108
A Logging utility.
Definition: Log.hpp:149
A base class for all the entities that must save/load data to/from disk.
Definition: IO.hpp:60
The collection of state variables of the whole system.
Definition: State.hpp:54
const Eigen::Index indexer(moordyn::Instance *obj)
Get the index within a state var for a particular instance.
Definition: State.hpp:115
InstanceStateVarView get(moordyn::Instance *obj)
Get the state variables associated to an instance.
Definition: State.hpp:102
State(const State &rhs)
Copy constructor.
Definition: State.hpp:67
~State()
Destructor.
Definition: State.hpp:75
State & operator=(const State &rhs)
Assignment operator.
Definition: State.hpp:161
StateVarView get()
Get the state variables.
Definition: State.hpp:92
State(moordyn::Log *log)
Constructor.
Definition: State.hpp:59
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
Eigen::Block< InstanceStateVar, Eigen::Dynamic > InstanceStateVarView
View of the State variables for a particular instance.
Definition: Misc.hpp:167
Eigen::VectorBlock< StateVar, Eigen::Dynamic > StateVarView
View of the State variable.
Definition: Misc.hpp:171
Eigen::Matrix< InstanceStateVar, Eigen::Dynamic, 1 > StateVar
State variable.
Definition: Misc.hpp:169
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118