Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components.cc 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "components.h"
  2. /// Link functions
  3. Link::Link(): charge{}
  4. {}
  5. Link::~Link()
  6. {}
  7. /// Component functions
  8. Component::Component(std::string const _name, Link& _a, Link& _b):
  9. name{_name}, a{_a}, b{_b}
  10. {}
  11. double Component::voltage() const
  12. {
  13. return a.charge-b.charge;
  14. }
  15. std::string Component::get_name() const {
  16. return name;
  17. }
  18. /// Resistor functions
  19. Resistor::Resistor(std::string const _name, Link& _a, Link& _b, double const _resistance):
  20. Component::Component{_name, _a, _b}, resistance{_resistance}
  21. {}
  22. void Resistor::simulate(double const _dt)
  23. {
  24. double charge_motion = (voltage() * _dt) / resistance;
  25. a.charge -= charge_motion;
  26. b.charge += charge_motion;
  27. }
  28. double Resistor::current() const
  29. {
  30. return Resistor::voltage()/resistance;
  31. }
  32. /// Capacitor function
  33. Capacitor::Capacitor(std::string const _name, Link& _a, Link& _b, double const _farad):
  34. Component::Component{_name, _a, _b}, farad{_farad}, charge{0}
  35. {}
  36. void Capacitor::simulate(double const _dt)
  37. {
  38. double charge_motion{(voltage() - charge) * farad * _dt};
  39. a.charge -= charge_motion;
  40. b.charge += charge_motion;
  41. charge += charge_motion;
  42. }
  43. double Capacitor::current() const
  44. {
  45. return farad * (voltage() - charge);
  46. }
  47. /// Battery function
  48. Battery::Battery(std::string const _name, Link& _a, Link& _b, double const _potential):
  49. Component::Component{_name, _a, _b}, potential{_potential}
  50. {}
  51. void Battery::simulate(double const)
  52. {
  53. a.charge = potential;
  54. b.charge = 0;
  55. }
  56. double Battery::voltage() const
  57. {
  58. return potential;
  59. }
  60. double Battery::current() const
  61. {
  62. return 0;
  63. }