ShmemDistributedVillas.cpp 3.5 KB
Newer Older
1
2
/** Example of shared memory interface
 *
Steffen Vogel's avatar
Steffen Vogel committed
3
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
4
 * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * DPsim
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

22
#include <DPsim.h>
23
24

using namespace DPsim;
25
using namespace CPS::DP;
26
using namespace CPS::DP::Ph1;
27

28
29
int main(int argc, char *argv[])
{
Steffen Vogel's avatar
Steffen Vogel committed
30
31
	SystemComponentList comps, comps2;
	SystemNodeList nodes;
32

33
	Interface::Config conf;
34
35
36
37
38
	conf.samplelen = 4;
	conf.queuelen = 1024;
	conf.polling = true;

	if (argc < 2) {
39
		std::cerr << "Not enough arguments (either 0 or 1 for the test number)" << std::endl;
40
41
42
		std::exit(1);
	}

43
44
45
46
47
48
49
50
51
	String in, out;

	if (String(argv[1]) == "0") {
		in  = "/villas0-in";
		out = "/villas0-out";
	}
	else if (String(argv[1]) == "1") {
		in  = "/villas1-in";
		out = "/villas1-out";
52
	}
53

54
	auto intf = Interface(in, out, &conf);
55
56

	if (String(argv[1]) == "0") {
57
58
59
60
61
62
		// Nodes
		auto n1 = Node::make("n1");
		auto n2 = Node::make("n2");
		auto n3 = Node::make("n3");

		// Components
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		auto evs = VoltageSource::make("v_t");
		auto vs = VoltageSourceNorton::make("v_s");
		auto l1 = Inductor::make("l_1");
		auto r1 = Resistor::make("r_1");

		// Topology
		evs->connect({ Node::GND, n3 });
		vs->connect({ Node::GND, n1 });
		l1->connect({ n1, n2 });
		r1->connect({ n2, n3 });

		// Parameters
		evs->setParameters(Complex(0, 0));
		vs->setParameters(Complex(10000, 0), 1);
		l1->setParameters(0.1);
		r1->setParameters(1);
79

Steffen Vogel's avatar
Steffen Vogel committed
80
		comps = SystemComponentList{evs, vs, l1, r1};
81
		nodes = SystemNodeList{Node::GND, n1, n2, n3};
82

Steffen Vogel's avatar
Steffen Vogel committed
83
		intf.addImport(evs->attribute<Complex>("V_ref"), 0);
Steffen Vogel's avatar
merge    
Steffen Vogel committed
84
		intf.addExport(evs->attribute<Complex>("i_comp"), 0);
85
86
	}
	else if (String(argv[1]) == "1") {
87
88
		// Nodes
		auto n4 = Node::make("n4");
Steffen Vogel's avatar
Steffen Vogel committed
89
		auto n5 = Node::make("n5");
90
91

		// Components
92
93
94
		auto ecs = CurrentSource::make("v_s");
		auto r2A = Resistor::make("r_2");
		auto r2B = Resistor::make("r_2");
Steffen Vogel's avatar
Steffen Vogel committed
95
		auto sw = Ph1::Switch::make("sw");
96
97
98
99

		// Topology
		ecs->connect({ Node::GND, n4 });
		r2A->connect({ Node::GND, n4 });
Steffen Vogel's avatar
Steffen Vogel committed
100
101
		sw->connect({ n4, n5 });
		r2B->connect({ Node::GND, n5 });
102
103
104
105
106

		// Parameters
		ecs->setParameters(Complex(0, 0));
		r2A->setParameters(10);
		r2B->setParameters(8);
Steffen Vogel's avatar
Steffen Vogel committed
107
		sw->setParameters(1e9, 0.1, false);
108

Steffen Vogel's avatar
Steffen Vogel committed
109
110
		comps = SystemComponentList{ecs, sw, r2A, r2B};
		nodes = SystemNodeList{Node::GND, n4, n5};
111

Steffen Vogel's avatar
Steffen Vogel committed
112
		intf.addImport(ecs->attribute<Complex>("I_ref"), 0);
Steffen Vogel's avatar
merge    
Steffen Vogel committed
113
		intf.addExport(ecs->attribute<Complex>("v_comp"), 0);
114
115
116
117
118
119
	}
	else {
		std::cerr << "invalid test number" << std::endl;
		std::exit(1);
	}

120
	String simName = "ShmemDistributed";
121
	Real timeStep = 0.001;
122

Steffen Vogel's avatar
Steffen Vogel committed
123
	auto sys = SystemTopology(50, nodes, comps);
124

Steffen Vogel's avatar
Steffen Vogel committed
125
	auto sim = RealTimeSimulation(simName + argv[1], sys, timeStep, 20);
126
	sim.addInterface(&intf);
127
128

	if (String(argv[1]) == "1") {
Steffen Vogel's avatar
Steffen Vogel committed
129
		auto evt = SwitchEvent::make(10, sys.component<CPS::Base::Ph1::Switch>("sw"), true);
130

Steffen Vogel's avatar
Steffen Vogel committed
131
		sim.addEvent(evt);
132
133
	}

134
	sim.run();
135
136

	return 0;
137
}