InterfacedInductor.cpp 1.59 KB
Newer Older
Markus Mirz's avatar
Markus Mirz committed
1
2
#include "InterfacedInductor.h"

3
4
using namespace DPsim;

5
6
InterfacedInductor::InterfacedInductor(std::string name, int src, int dest, Real inductance) : BaseComponent(name, src, dest) {
	this->mInductance = inductance;
Markus Mirz's avatar
Markus Mirz committed
7
8
}

9
10

/// Initialize internal state
11
12
13
14
15
void InterfacedInductor::init(Real om, Real dt) {
	mCurrentRe = 0;
	mCurrentIm = 0;
	mVoltageRe = 0;
	mVoltageIm = 0;
Markus Mirz's avatar
Markus Mirz committed
16
17
}

18

19
void InterfacedInductor::step(SystemModel& system, Real time) {
Markus Mirz's avatar
Markus Mirz committed
20
	// Calculate current for this step
21
22
	mCurrentStepRe = mCurrentRe + system.getTimeStep() * (1. / mInductance * mVoltageRe + system.getOmega() * mCurrentIm);
	mCurrentStepIm = mCurrentIm + system.getTimeStep() * (1. / mInductance * mVoltageIm - system.getOmega() * mCurrentRe);
Markus Mirz's avatar
Markus Mirz committed
23
24

	// Update current source accordingly
25
	if (mNode1 >= 0) {
26
		system.addCompToRightSideVector(mNode1, -mCurrentStepRe, -mCurrentStepIm);
Markus Mirz's avatar
Markus Mirz committed
27
	}
28
	if (mNode2 >= 0) {
29
		system.addCompToRightSideVector(mNode2, mCurrentStepRe, mCurrentStepIm);
30
	}	
31
32
	mCurrentRe = mCurrentStepRe;
	mCurrentIm = mCurrentStepIm;
33
34
}

35
36
void InterfacedInductor::postStep(SystemModel& system) {
	double vposr, vnegr, vposi, vnegi;
37
38

	// extract solution
39
	if (mNode1 >= 0) {
40
41
		vposr = system.getRealFromLeftSideVector(mNode1);
		vposi = system.getImagFromLeftSideVector(mNode1);
42
43
44
45
46
47
	}
	else {
		vposr = 0;
		vposi = 0;
	}

48
	if (mNode2 >= 0) {
49
50
		vnegr = system.getRealFromLeftSideVector(mNode2);
		vnegi = system.getImagFromLeftSideVector(mNode2);
51
52
53
54
	}
	else {
		vnegr = 0;
		vnegi = 0;
Markus Mirz's avatar
Markus Mirz committed
55
	}
56
57
	mVoltageRe = vposr - vnegr;
	mVoltageIm = vposi - vnegi;
Markus Mirz's avatar
Markus Mirz committed
58
59
}

60
61
62
Complex InterfacedInductor::getCurrent(SystemModel& system) {
	return Complex(mCurrentRe, mCurrentIm);
}