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