mbsimelectronics  4.0.0
MBSim Electronics Module
simulation_classes.h
1 #ifndef _SIMULATION_CLASSES_H
2 #define _SIMULATION_CLASSES_H
3 
4 #include "mbsim/dynamic_system_solver.h"
5 #include "mbsim/objects/object.h"
6 #include "mbsim/links/link.h"
7 #include "mbsim/functions/function.h"
8 #include "mbsim/mbsim_event.h"
9 #include "modelling_classes.h"
10 #include <string>
11 //#include <mbsim/utils/function.h>
12 
13 //namespace MBSimControl {
14 // class Signal;
15 //}
16 
17 namespace MBSimElectronics {
18 
19  class Branch;
20  class Mesh : public MBSim::Object {
21  protected:
22  Object* precessor;
23  std::vector<Branch*> branch;
24  public:
25  Mesh(const std::string &name) : MBSim::Object(name), precessor(nullptr) {}
26  void calcqSize() override { qSize = 1;}
27  void calcuSize(int j) override { uSize[j] = (j==0) ? 1 : 0;}
28  void init(InitStage stage, const MBSim::InitConfigSet &config) override;
29  void setPrecessor(Object* obj) {precessor = obj;}
30  void addBranch(Branch* branch_) {branch.push_back(branch_);}
31  int getNumberOfBranches() {return branch.size();}
32  Branch* getBranch(int i) {return branch[i];}
33  std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() override { return std::shared_ptr<OpenMBV::Group>(); }
34  };
35 
36  class Branch : public MBSim::Object {
37  protected:
38  fmatvec::Mat J[2];
39  fmatvec::Vec Q, I;
40  std::vector<Mesh*> mesh;
41  std::vector<double> vz;
42  Terminal *startTerminal, *endTerminal;
43  std::vector<Branch*> connectedBranch;
44  int flag;
45  Object* precessor;
46  bool updQ, updI;
47  public:
48  Branch(const std::string &name) : Object(name), Q(1), I(1), flag(0), precessor(nullptr), updQ(true), updI(true) { }
49  void calcuSize(int j) override { uSize[j] = (j==0) ? 0 : 1;}
50  void updateCharge();
51  void updateCurrent();
52  const fmatvec::Mat& getJacobian(int j, bool check=true) const { return J[j];}
53  fmatvec::Mat& getJacobian(int j, bool check=true) { return J[j];}
54  const fmatvec::Vec& getCurrent(bool check=true) const { assert((not check) or (not updI)); return I; }
55  fmatvec::Vec& getCurrent(bool check=true) { assert((not check) or (not updI)); return I; }
56  const fmatvec::Vec& evalCurrent() { if(updI) updateCurrent(); return I; }
57  const fmatvec::Vec& getCharge(bool check=true) const { assert((not check) or (not updQ)); return Q; }
58  fmatvec::Vec& getCharge(bool check=true) { assert((not check) or (not updQ)); return Q; }
59  const fmatvec::Vec& evalCharge() { if(updQ) updateCharge(); return Q; }
60  void setvz(double vz_, Mesh* mesh);
61  void connect(Mesh *mesh_) {mesh.push_back(mesh_);mesh_->addBranch(this);vz.push_back(0);}
62  void clearMeshList() {mesh.clear();}
63  int getNumberOfConnectedMeshes() const {return mesh.size();}
64  Mesh* getMesh(int i) {return mesh[i];}
65  void init(InitStage stage, const MBSim::InitConfigSet &config) override;
66  void setStartTerminal(Terminal* p) {startTerminal = p; p->addConnectedBranch(this);}
67  void setEndTerminal(Terminal* p) {endTerminal = p; p->addConnectedBranch(this);}
68  Terminal* getStartTerminal() {return startTerminal;}
69  Terminal* getEndTerminal() {return endTerminal;}
70  void addConnectedBranch(Branch* branch);
71  void buildTreeBranches(Branch* callingBranch, std::vector<Branch*> &treeBranch, unsigned int nmax);
72  void buildMeshes(Terminal* callingTerminal, Branch* callingBranch, Mesh* currentMesh, bool &foundMesh);
73 
74  void setFlag(int f) { flag = f; }
75  int getFlag() const { return flag; }
76  void setPrecessor(Object* obj) {precessor = obj;}
77  void resetUpToDate() override { Object::resetUpToDate(); updQ = true; updI = true; }
78  std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() override { return std::shared_ptr<OpenMBV::Group>(); }
79  };
80 
82  protected:
83  fmatvec::Vec gdn, gdd;
84  public:
85  ElectronicLink(const std::string &name) : Link(name) {}
86  Element* getParent() override {return parent;}
87  const Element* getParent() const override {return parent;}
88  void setParent(Element* parent_) override {parent = parent_;}
89 
90  void calcSize() override { ng = 1; ngd = 1; nla = 1; }
91 
92  void calcgSize(int j) override;
93  void calcgdSize(int j) override;
94  void calclaSize(int j) override;
95  void updateg() override;
96  void updategd() override;
97  void updateW(int j=0) override;
98  void updateh(int j=0) override;
99  void plot() override;
100  std::string getName() const override {return Link::getName();}
101  void setName(std::string name) override {Link::setName(name);}
102  bool isActive() const override {return true;}
103  bool gActiveChanged() override {return true;}
104  bool isSingleValued() const override { return true; }
105  void init(InitStage stage, const MBSim::InitConfigSet &config) override;
106  void updatehRef(fmatvec::Vec &hParent, int j=0) override;
107  void updaterRef(fmatvec::Vec &rParent, int j=0) override;
108 
109  /* INHERITED INTERFACE OF LINKINTERFACE */
110  virtual void updater() { throwError("(ElectronicLink::updater): Not implemented!"); }
111  /*****************************************************/
112 
113  /* INHERITED INTERFACE OF LINK */
114  void updateWRef(fmatvec::Mat& ref, int j) override;
115  void updateVRef(fmatvec::Mat& ref, int j) override;
116  virtual void updatedhdqRef(fmatvec::Mat& ref, int i=0) {}
117  virtual void updatedhduRef(fmatvec::SqrMat& ref, int i=0) {}
118  virtual void updatedhdtRef(fmatvec::Vec& ref, int i=0) {}
119  void resetUpToDate() override { Link::resetUpToDate(); updQ = true; updI = true; }
120  /*****************************************************/
121  };
122 
123  class Resistor : public ElectronicLink {
124  protected:
125  double R;
126  public:
127  Resistor(const std::string &name);
128  void setResistance(double R_) { R = R_;}
129  void updateGeneralizedForces() override;
130  };
131 
132  class Capacitor : public ElectronicLink {
133  protected:
134  double C;
135  public:
136  Capacitor(const std::string &name);
137  void setCapacity(double C_) { C = C_;}
138  void updateGeneralizedForces() override;
139  };
140 
141  class VoltageSource : public ElectronicLink {
142  protected:
143  MBSim::Function<fmatvec::VecV(double)> *voltageSignal;
144  public:
145  VoltageSource(const std::string &name);
146  ~VoltageSource() override { delete voltageSignal; }
147  void setVoltageSignal(MBSim::Function<fmatvec::VecV(double)> *func) {voltageSignal = func;}
148  void updateGeneralizedForces() override;
149  };
150 
151  class Diode : public ElectronicLink {
152  protected:
153  bool sv;
154  public:
155  Diode(const std::string &name);
156  void setSetValued(bool flag) {sv = flag;}
157  void updateGeneralizedForces() override;
158  bool isSetValued() const override {return sv;}
159  bool isSingleValued() const override {return not sv;}
160  void checkImpactsForTermination() override;
161  void solveImpactsGaussSeidel() override;
162  };
163 
164  class Switch : public ElectronicLink {
165  protected:
166  MBSim::Function<fmatvec::VecV(double)> *voltageSignal;
167  bool sv;
168  public:
169  Switch(const std::string &name);
170  ~Switch() override { delete voltageSignal; }
171  void setSetValued(bool flag) {sv = flag;}
172  bool isSetValued() const override {return sv;}
173  bool isSingleValued() const override {return not sv;}
174  void updateGeneralizedForces() override;
175  void checkImpactsForTermination() override;
176  void solveImpactsGaussSeidel() override;
177  void setVoltageSignal(MBSim::Function<fmatvec::VecV(double)> *func) {voltageSignal = func;}
178  };
179 
181  protected:
182  public:
183  ElectronicObject(const std::string &name) : Object(name) {}
184  void calcSize() override { nq = 0; nu = 1; }
185  void init(InitStage stage, const MBSim::InitConfigSet &config) override;
186  Element* getParent() override {return parent;}
187  const Element* getParent() const override {return parent;}
188  void setParent(Element* parent_) override {parent = parent_;}
189  void plot() override;
190  std::string getName() const override {return Object::getName();}
191  void setName(std::string name) override {Object::setName(name);}
192  void resetUpToDate() override { Object::resetUpToDate(); updQ = true; updI = true; }
193  std::shared_ptr<OpenMBV::Group> getOpenMBVGrp() override { return std::shared_ptr<OpenMBV::Group>(); }
194  };
195 
196  class Inductor : public ElectronicObject {
197  protected:
198  double L;
199  public:
200  Inductor(const std::string &name);
201  void updateM() override;
202  void setInductance(double L_) { L = L_;}
203  };
204 
205 }
206 
207 #endif
Definition: simulation_classes.h:132
Definition: simulation_classes.h:196
Definition: simulation_classes.h:151
Object(const std::string &name)
Definition: simulation_classes.h:123
Definition: simulation_classes.h:180
Definition: simulation_classes.h:20
Definition: simulation_classes.h:36
std::string name
Definition: modelling_classes.h:44
Definition: simulation_classes.h:164
Definition: modelling_classes.h:21
Definition: simulation_classes.h:141
Element(const std::string &name)