bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
coupling.h
Go to the documentation of this file.
1#ifndef BITSIM_COSIM_COUPLING_H
2#define BITSIM_COSIM_COUPLING_H
3
5
6#include <functional>
7#include <map>
8#include <string>
9#include <vector>
10
11namespace bitsim
12{
13namespace cosim
14{
15
20{
21 std::string from;
23 std::string to;
25};
26
35{
36 public:
40 uint_t add(const std::string &name, uint_t n_inputs, uint_t n_outputs);
41
42 void connect(const std::string &from, uint_t output, const std::string &to, uint_t input);
43
47 void validate() const;
48
49 uint_t size() const noexcept { return static_cast<uint_t>(names_.size()); }
50
51 uint_t index(const std::string &name) const;
52
53 const std::string &name(uint_t i) const { return names_.at(i); }
54
55 uint_t n_inputs(uint_t i) const { return n_inputs_.at(i); }
56
57 uint_t n_outputs(uint_t i) const { return n_outputs_.at(i); }
58
59 const std::vector<Connection> &connections() const noexcept { return connections_; }
60
64 std::vector<std::vector<real_t>>
65 inputs_from(const std::vector<std::vector<real_t>> &outputs) const;
66
70 std::vector<std::vector<real_t>> zero_inputs() const;
71
72 private:
73 std::vector<std::string> names_;
74 std::vector<uint_t> n_inputs_;
75 std::vector<uint_t> n_outputs_;
76 std::map<std::string, uint_t> index_;
77 std::vector<Connection> connections_;
78};
79
97
113
118{
119 std::vector<std::vector<real_t>> inputs;
120 std::vector<std::vector<real_t>> previous;
121 bool has_previous{false};
122};
123
133
138using try_step_type = std::function<std::vector<std::vector<real_t>>(
139 uint_t iteration, const std::vector<std::vector<real_t>> &begin,
140 const std::vector<std::vector<real_t>> &end)>;
141
149MacroStepResult jacobi_step(const CouplingLayout &layout, const CouplingConfig &config,
150 CouplingState &state, const try_step_type &try_step);
151
157std::vector<std::vector<real_t>> resolve_inputs(
158 const CouplingLayout &layout,
159 const std::function<std::vector<std::vector<real_t>>(const std::vector<std::vector<real_t>> &)>
160 &outputs_of);
161
162} // namespace cosim
163} // namespace bitsim
164
165#endif // BITSIM_COSIM_COUPLING_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The subsystems of a co-simulation, their sizes, and which output feeds which input.
Definition coupling.h:35
uint_t index(const std::string &name) const
Definition coupling.cpp:26
uint_t add(const std::string &name, uint_t n_inputs, uint_t n_outputs)
Add a subsystem; its index is the number of subsystems before it.
Definition coupling.cpp:12
uint_t size() const noexcept
Definition coupling.h:49
void connect(const std::string &from, uint_t output, const std::string &to, uint_t input)
Definition coupling.cpp:36
std::vector< std::vector< real_t > > zero_inputs() const
Every subsystem's inputs, all 0.
Definition coupling.cpp:72
uint_t n_outputs(uint_t i) const
Definition coupling.h:57
const std::vector< Connection > & connections() const noexcept
Definition coupling.h:59
void validate() const
Definition coupling.cpp:48
std::vector< std::vector< real_t > > inputs_from(const std::vector< std::vector< real_t > > &outputs) const
The inputs of every subsystem that the outputs of every subsystem give.
Definition coupling.cpp:83
uint_t n_inputs(uint_t i) const
Definition coupling.h:55
const std::string & name(uint_t i) const
Definition coupling.h:53
Definition co_simulation.cpp:9
std::vector< std::vector< real_t > > resolve_inputs(const CouplingLayout &layout, const std::function< std::vector< std::vector< real_t > >(const std::vector< std::vector< real_t > > &)> &outputs_of)
The inputs at the start: outputs_of(inputs) gives the outputs of every subsystem for the inputs,...
Definition coupling.cpp:147
CouplingScheme
The order in which the subsystems of a macro step take their inputs.
Definition coupling.h:84
@ JACOBI
Definition coupling.h:90
@ GAUSS_SEIDEL
Definition coupling.h:95
MacroStepResult jacobi_step(const CouplingLayout &layout, const CouplingConfig &config, CouplingState &state, const try_step_type &try_step)
One macro step of the Jacobi scheme, with as many tries as config allows.
Definition coupling.cpp:93
std::function< std::vector< std::vector< real_t > >( uint_t iteration, const std::vector< std::vector< real_t > > &begin, const std::vector< std::vector< real_t > > &end)> try_step_type
The outputs of every subsystem at the end of try iteration of a macro step, the subsystems having ste...
Definition coupling.h:138
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Output output of subsystem from feeds input input of subsystem to.
Definition coupling.h:20
std::string from
Definition coupling.h:21
std::string to
Definition coupling.h:23
uint_t output
Definition coupling.h:22
uint_t input
Definition coupling.h:24
How a co-simulation couples its subsystems.
Definition coupling.h:102
uint_t max_iterations
JACOBI: the most tries of a macro step; 1 couples loosely.
Definition coupling.h:104
CouplingScheme scheme
Definition coupling.h:103
real_t tolerance
JACOBI: done when no input moves by more than this.
Definition coupling.h:105
bool extrapolate
Definition coupling.h:111
real_t relaxation
Definition coupling.h:106
The inputs of every subsystem at the present time, and at the time before.
Definition coupling.h:118
std::vector< std::vector< real_t > > previous
Definition coupling.h:120
bool has_previous
Definition coupling.h:121
std::vector< std::vector< real_t > > inputs
Definition coupling.h:119
What one macro step did.
Definition coupling.h:128
bool converged
the residual is within the tolerance; always true for one try
Definition coupling.h:131
real_t residual
the largest change of an input in the last try
Definition coupling.h:130
uint_t iterations
the tries of the step
Definition coupling.h:129