78 lines
1.6 KiB
C++
78 lines
1.6 KiB
C++
#include "components.h"
|
|
|
|
/// Link functions
|
|
Link::Link(): charge{}
|
|
{}
|
|
|
|
Link::~Link()
|
|
{}
|
|
|
|
/// Component functions
|
|
Component::Component(std::string const _name, Link& _a, Link& _b):
|
|
name{_name}, a{_a}, b{_b}
|
|
{}
|
|
|
|
double Component::voltage() const
|
|
{
|
|
return a.charge-b.charge;
|
|
}
|
|
|
|
std::string Component::get_name() const {
|
|
return name;
|
|
}
|
|
|
|
/// Resistor functions
|
|
Resistor::Resistor(std::string const _name, Link& _a, Link& _b, double const _resistance):
|
|
Component::Component{_name, _a, _b}, resistance{_resistance}
|
|
{}
|
|
|
|
void Resistor::simulate(double const _dt)
|
|
{
|
|
double charge_motion = (voltage() * _dt) / resistance;
|
|
a.charge -= charge_motion;
|
|
b.charge += charge_motion;
|
|
}
|
|
|
|
double Resistor::current() const
|
|
{
|
|
return Resistor::voltage()/resistance;
|
|
}
|
|
|
|
/// Capacitor function
|
|
Capacitor::Capacitor(std::string const _name, Link& _a, Link& _b, double const _farad):
|
|
Component::Component{_name, _a, _b}, farad{_farad}, charge{0}
|
|
{}
|
|
|
|
void Capacitor::simulate(double const _dt)
|
|
{
|
|
double charge_motion{(voltage() - charge) * farad * _dt};
|
|
a.charge -= charge_motion;
|
|
b.charge += charge_motion;
|
|
charge += charge_motion;
|
|
}
|
|
|
|
double Capacitor::current() const
|
|
{
|
|
return farad * (voltage() - charge);
|
|
}
|
|
|
|
/// Battery function
|
|
Battery::Battery(std::string const _name, Link& _a, Link& _b, double const _potential):
|
|
Component::Component{_name, _a, _b}, potential{_potential}
|
|
{}
|
|
|
|
void Battery::simulate(double const)
|
|
{
|
|
a.charge = potential;
|
|
b.charge = 0;
|
|
}
|
|
|
|
double Battery::voltage() const
|
|
{
|
|
return potential;
|
|
}
|
|
|
|
double Battery::current() const
|
|
{
|
|
return 0;
|
|
}
|