EMT_Ph3_VoltageSource.cpp 7.72 KB
Newer Older
1
2
/* Copyright 2017-2020 Institute for Automation of Complex Power Systems,
 *                     EONERC, RWTH Aachen University
Markus Mirz's avatar
Markus Mirz committed
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
Markus Mirz's avatar
Markus Mirz committed
7
8
9
10
11
12
13
14
 *********************************************************************************/

#include <cps/EMT/EMT_Ph3_VoltageSource.h>


using namespace CPS;

EMT::Ph3::VoltageSource::VoltageSource(String uid, String name, Logger::Level logLevel)
15
	: SimPowerComp<Real>(uid, name, logLevel) {
Markus Mirz's avatar
Markus Mirz committed
16
17
18
19
20
21
22
23
24
25
26
27
28
	mPhaseType = PhaseType::ABC;
	setVirtualNodeNumber(1);
	setTerminalNumber(2);
	mIntfVoltage = Matrix::Zero(3, 1);
	mIntfCurrent = Matrix::Zero(3, 1);

	addAttribute<Complex>("V_ref", Flags::read | Flags::write);
	addAttribute<Real>("f_src", Flags::read | Flags::write);
}

void EMT::Ph3::VoltageSource::setParameters(Complex voltageRef, Real srcFreq) {
	attribute<Complex>("V_ref")->set(voltageRef);
	attribute<Real>("f_src")->set(srcFreq);
29

30
31
32
33
	mSLog->info("\nVoltage reference phasor [V]: {:s}"
				"\nFrequency [Hz]: {:s}", 
				Logger::phasorToString(voltageRef), 
				Logger::realToString(srcFreq));
34
	mParametersSet = true;
Markus Mirz's avatar
Markus Mirz committed
35
36
}

Jan Dinkelbach's avatar
Jan Dinkelbach committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
void EMT::Ph3::VoltageSource::initializeFromPowerflow(Real frequency) {
	mVoltageRef = attribute<Complex>("V_ref");
	mSrcFreq = attribute<Real>("f_src");

	if (!mParametersSet) {
		mVoltageRef->set(initialSingleVoltage(1) - initialSingleVoltage(0));
		mSrcFreq->set(frequency);
	}

	mSLog->info("\nVoltage reference phasor [V]: {:s}"
					"\nFrequency [Hz]: {:s}", 
					Logger::phasorToString(mVoltageRef->get()), 
					Logger::realToString(mSrcFreq->get()));
	mSLog->flush();
}

Markus Mirz's avatar
Markus Mirz committed
53
SimPowerComp<Real>::Ptr EMT::Ph3::VoltageSource::clone(String name) {
Markus Mirz's avatar
Markus Mirz committed
54
55
56
57
58
59
60
61
62
	auto copy = VoltageSource::make(name, mLogLevel);
	copy->setParameters(attribute<Complex>("V_ref")->get(), attribute<Real>("f_src")->get());
	return copy;
}


void EMT::Ph3::VoltageSource::mnaInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
	MNAInterface::mnaInitialize(omega, timeStep);

Markus Mirz's avatar
Markus Mirz committed
63
	updateMatrixNodeIndices();
Jan Dinkelbach's avatar
Jan Dinkelbach committed
64

Markus Mirz's avatar
Markus Mirz committed
65
66
	mVoltageRef = attribute<Complex>("V_ref");
	mSrcFreq = attribute<Real>("f_src");
Jan Dinkelbach's avatar
Jan Dinkelbach committed
67
68
69
70
71

	mIntfVoltage(0, 0) = RMS3PH_TO_PEAK1PH * Math::abs(mVoltageRef->get()) * cos(Math::phase(mVoltageRef->get()));
	mIntfVoltage(1, 0) = RMS3PH_TO_PEAK1PH * Math::abs(mVoltageRef->get()) * cos(Math::phase(mVoltageRef->get()) - 2. / 3. * M_PI);
	mIntfVoltage(2, 0) = RMS3PH_TO_PEAK1PH * Math::abs(mVoltageRef->get()) * cos(Math::phase(mVoltageRef->get()) + 2. / 3. * M_PI);

Markus Mirz's avatar
Markus Mirz committed
72
73
74
	mMnaTasks.push_back(std::make_shared<MnaPreStep>(*this));
	mMnaTasks.push_back(std::make_shared<MnaPostStep>(*this, leftVector));
	mRightVector = Matrix::Zero(leftVector->get().rows(), 1);
Jan Dinkelbach's avatar
Jan Dinkelbach committed
75
76
77
78
79
80
81
82
83

	mSLog->info(
		"\n--- MNA initialization ---"
		"\nInitial voltage {:s}"
		"\nInitial current {:s}"
		"\n--- MNA initialization finished ---",
		Logger::matrixToString(mIntfVoltage),
		Logger::matrixToString(mIntfCurrent));
	mSLog->flush();
Markus Mirz's avatar
Markus Mirz committed
84
85
86
87
}

void EMT::Ph3::VoltageSource::mnaApplySystemMatrixStamp(Matrix& systemMatrix) {
	if (terminalNotGrounded(0)) {
Markus Mirz's avatar
Markus Mirz committed
88
89
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 0), mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), -1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), matrixNodeIndex(0, 0), -1);
Markus Mirz's avatar
Markus Mirz committed
90

Markus Mirz's avatar
Markus Mirz committed
91
92
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 1), mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), -1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), matrixNodeIndex(0, 1), -1);
Markus Mirz's avatar
Markus Mirz committed
93

Markus Mirz's avatar
Markus Mirz committed
94
95
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0, 2), mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), -1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), matrixNodeIndex(0, 2), -1);
Markus Mirz's avatar
Markus Mirz committed
96
97
	}
	if (terminalNotGrounded(1)) {
Markus Mirz's avatar
Markus Mirz committed
98
99
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 0), mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), 1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), matrixNodeIndex(1, 0), 1);
Markus Mirz's avatar
Markus Mirz committed
100

Markus Mirz's avatar
Markus Mirz committed
101
102
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 1), mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), 1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), matrixNodeIndex(1, 1), 1);
Markus Mirz's avatar
Markus Mirz committed
103

Markus Mirz's avatar
Markus Mirz committed
104
105
		Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1, 2), mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), 1);
		Math::addToMatrixElement(systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), matrixNodeIndex(1, 2), 1);
Markus Mirz's avatar
Markus Mirz committed
106
107
108
109
	}


	// if (terminalNotGrounded(0)) {
Markus Mirz's avatar
Markus Mirz committed
110
111
	// 	mLog.debug() << "Add " << -1 << " to " << matrixNodeIndex(0) << "," << mVirtualNodes[0]->matrixNodeIndex() << std::endl;
	// 	mLog.debug() << "Add " << -1 << " to " << mVirtualNodes[0]->matrixNodeIndex() << "," << matrixNodeIndex(0) << std::endl;
Markus Mirz's avatar
Markus Mirz committed
112
113
	// }
	// if (terminalNotGrounded(1)) {
Markus Mirz's avatar
Markus Mirz committed
114
115
	// 	mLog.debug() << "Add " << 1 << " to " << matrixNodeIndex(1) << "," << mVirtualNodes[0]->matrixNodeIndex() << std::endl;
	// 	mLog.debug() << "Add " << 1 << " to " << mVirtualNodes[0]->matrixNodeIndex() << "," << matrixNodeIndex(1) << std::endl;
Markus Mirz's avatar
Markus Mirz committed
116
117
118
119
	// }
}

void EMT::Ph3::VoltageSource::mnaApplyRightSideVectorStamp(Matrix& rightVector) {
Markus Mirz's avatar
Markus Mirz committed
120
121
122
	Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A), mIntfVoltage(0, 0));
	Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B), mIntfVoltage(1, 0));
	Math::setVectorElement(rightVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C), mIntfVoltage(2, 0));
Markus Mirz's avatar
Markus Mirz committed
123
124
125
}

void EMT::Ph3::VoltageSource::updateVoltage(Real time) {
Jan Dinkelbach's avatar
Jan Dinkelbach committed
126

Markus Mirz's avatar
Markus Mirz committed
127
128
	Complex voltageRef = mVoltageRef->get();
	Real srcFreq = mSrcFreq->get();
Jan Dinkelbach's avatar
Jan Dinkelbach committed
129
	
Markus Mirz's avatar
Markus Mirz committed
130
	if (srcFreq > 0) {
Jan Dinkelbach's avatar
Jan Dinkelbach committed
131
132
133
		mIntfVoltage(0, 0) = RMS3PH_TO_PEAK1PH * Math::abs(voltageRef) * cos(time * 2. * PI * srcFreq + Math::phase(voltageRef));
		mIntfVoltage(1, 0) = RMS3PH_TO_PEAK1PH * Math::abs(voltageRef) * cos(time * 2. * PI * srcFreq + Math::phase(voltageRef) - 2. / 3. * M_PI);
		mIntfVoltage(2, 0) = RMS3PH_TO_PEAK1PH * Math::abs(voltageRef) * cos(time * 2. * PI * srcFreq + Math::phase(voltageRef) + 2. / 3. * M_PI);
Markus Mirz's avatar
Markus Mirz committed
134
135
	}
	else {
Jan Dinkelbach's avatar
Jan Dinkelbach committed
136
137
138
		mIntfVoltage(0, 0) = RMS3PH_TO_PEAK1PH * voltageRef.real();
		mIntfVoltage(1, 0) = RMS3PH_TO_PEAK1PH * voltageRef.real();
		mIntfVoltage(2, 0) = RMS3PH_TO_PEAK1PH * voltageRef.real();
Markus Mirz's avatar
Markus Mirz committed
139
	}
Jan Dinkelbach's avatar
Jan Dinkelbach committed
140
141
142
143
144

	mSLog->debug(
		"\nUpdate Voltage: {:s}",
		Logger::matrixToString(mIntfVoltage)
	);
Markus Mirz's avatar
Markus Mirz committed
145
146
147
148
149
150
}

void EMT::Ph3::VoltageSource::updateVoltage(Matrix vabc) {
	mIntfVoltage=vabc;
}

151
152
153
154
155
156
157
158
159
void EMT::Ph3::VoltageSource::mnaAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) {
	attributeDependencies.push_back(attribute("V_ref"));
	modifiedAttributes.push_back(attribute("right_vector"));
	modifiedAttributes.push_back(attribute("v_intf"));
}

void EMT::Ph3::VoltageSource::mnaPreStep(Real time, Int timeStepCount) {
	updateVoltage(time);
	mnaApplyRightSideVectorStamp(mRightVector);
Markus Mirz's avatar
Markus Mirz committed
160
161
}

162
163
164
165
166
167
168
void EMT::Ph3::VoltageSource::mnaAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) {
	attributeDependencies.push_back(leftVector);
	modifiedAttributes.push_back(attribute("i_intf"));
};

void EMT::Ph3::VoltageSource::mnaPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
	mnaUpdateCurrent(*leftVector);
Markus Mirz's avatar
Markus Mirz committed
169
170
171
}

void EMT::Ph3::VoltageSource::mnaUpdateCurrent(const Matrix& leftVector) {
Markus Mirz's avatar
Markus Mirz committed
172
173
174
	mIntfCurrent(0, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A));
	mIntfCurrent(1, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B));
	mIntfCurrent(2, 0) = Math::realFromVectorElement(leftVector, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C));
Markus Mirz's avatar
Markus Mirz committed
175
}