Skip to content

Commit f6d622e

Browse files
committed
Some refactoring.
1 parent 6a3aa9d commit f6d622e

15 files changed

+183
-137
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44

5-
65
## About
76

87
Simosaik is an external simulation for the agent-based discrete-event power system simulation model [SIMONA](https://github.com/ie3-institute/simona).
@@ -20,23 +19,25 @@ You have to use the following branches:
2019

2120
- simosaik: `main`
2221
- simonaAPI: `ms/ReCoDe`
23-
- simona: `ms/simosaik-dev`
22+
- simona: `ms/ReCoDe`
2423

2524

2625
**Step 2:** <br>
2726
Add the mosaik API to the folder `./libs`.
2827

2928

3029
**Step 3:** <br>
31-
After selecting these branches, you have to run the following `shadowJar` on SIMONA and simosaik.
30+
To create executable jars, you have to run the task `shadowJar` on SIMONA and simosaik.
3231

3332

3433
**Step 4:** <br>
35-
Add an execution of the jar containing SIMONA to your MOSAIK scenario.
36-
34+
Add the following statement to your mosaik simulation configuration:
35+
``'SimonaPowerGrid': {
36+
'cmd': 'java -cp path/to/simonaJar edu.ie3.simona.main.RunSimonaStandalone --config=path/to/simona/configuration/file --ext-address=%(addr)s',
37+
}``
3738

3839
**Step 5:** <br>
39-
Add the following to your SIMONA configuration file:
40+
Add the following configuration to your SIMONA configuration file:
4041

4142
- ``simona.input.extSimDir = "path/to/simosaik.jar"``
4243

@@ -50,6 +51,6 @@ Add the following to your SIMONA configuration file:
5051
The following simulations can be selected:
5152

5253
- `PrimaryResult`: Provides SIMONA with primary data and MOSAIK with SIMONA results
53-
- `MosaikOptimizer`: Optimizer for em agents in SIMONA
54+
- `FlexOptionOptimizer`: Optimizer for em agents or models with flexibility options in SIMONA
5455

5556
If no simulation is selected, simosaik will use the `PrimaryResult` simulation.

build.gradle

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
2-
31
plugins {
42
id 'java' // java support
53
id 'scala' // scala support
@@ -29,7 +27,6 @@ java {
2927
}
3028

3129
apply from: scriptsLocation + 'spotless.gradle'
32-
apply from: scriptsLocation + 'tscfg.gradle' // config tasks
3330

3431
configurations {
3532
scalaCompilerPlugin

gradle/scripts/tscfg.gradle

-56
This file was deleted.

src/main/java/edu/ie3/simosaik/MetaUtils.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
/** Methods for simplifying the creation of mosaik meta information. */
2020
public interface MetaUtils {
2121

22+
static Map<String, Object> createMetaWithPowerGrid(String type, ModelParams... additionalModels) {
23+
return createMeta(type, ModelParams.simonaPowerGridEnvironment(), List.of(additionalModels));
24+
}
25+
2226
static Map<String, Object> createMeta(
2327
String type, ModelParams simonaPowerGrid, ModelParams... additionalModels) {
2428
return createMeta(type, simonaPowerGrid, List.of(additionalModels));
2529
}
2630

2731
@SuppressWarnings("unchecked")
28-
static Map<String, Object> createMeta(
32+
private static Map<String, Object> createMeta(
2933
String type, ModelParams simonaPowerGrid, List<ModelParams> additionalModels) {
3034
JSONObject meta = new JSONObject();
3135
meta.put("api_version", Simulator.API_VERSION);
@@ -45,16 +49,26 @@ static Map<String, Object> createMeta(
4549
@SuppressWarnings("unchecked")
4650
static JSONObject createObject(
4751
boolean isPublic, List<String> params, List<String> attrs, List<String> triggers) {
48-
JSONArray paramArray = new JSONArray();
49-
if (params != null) paramArray.addAll(params);
50-
51-
JSONArray attrArray = new JSONArray();
52-
if (attrs != null) attrArray.addAll(attrs);
53-
5452
JSONObject obj = new JSONObject();
5553
obj.put("public", isPublic);
56-
obj.put("params", paramArray);
57-
obj.put("attrs", attrArray);
54+
55+
if (params != null) {
56+
JSONArray paramArray = new JSONArray();
57+
paramArray.addAll(params);
58+
obj.put("params", paramArray);
59+
}
60+
61+
if (attrs != null) {
62+
JSONArray attrArray = new JSONArray();
63+
attrArray.addAll(attrs);
64+
obj.put("attrs", attrArray);
65+
}
66+
67+
if (triggers != null) {
68+
JSONArray triggerArray = new JSONArray();
69+
triggerArray.addAll(triggers);
70+
obj.put("triggers", triggerArray);
71+
}
5872

5973
return obj;
6074
}

src/main/java/edu/ie3/simosaik/MosaikSimulator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public abstract class MosaikSimulator extends Simulator implements SimonaEntitie
2121

2222
public final int stepSize;
2323

24-
protected DataQueueExtSimulationExtSimulator<ExtInputDataContainer> dataQueueMosaikToSimona;
25-
protected DataQueueExtSimulationExtSimulator<ExtResultContainer> dataQueueSimonaToMosaik;
24+
public DataQueueExtSimulationExtSimulator<ExtInputDataContainer> dataQueueMosaikToSimona;
25+
public DataQueueExtSimulationExtSimulator<ExtResultContainer> dataQueueSimonaToMosaik;
2626

2727
public MosaikSimulator(String name, int stepSize) {
2828
super(name);

src/main/java/edu/ie3/simosaik/SimosaikExtLink.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import edu.ie3.simona.api.simulation.ExtSimAdapterData;
1111
import edu.ie3.simosaik.config.ArgsParser;
1212
import edu.ie3.simosaik.primaryResultSimulator.PrimaryResultSimulation;
13-
import edu.ie3.simosaik.simosaikFlexOptionOptimizer.MosaikOptimizerSimulation;
13+
import edu.ie3.simosaik.simosaikFlexOptionOptimizer.FlexOptionOptimizerSimulation;
1414
import java.nio.file.Path;
1515

1616
public class SimosaikExtLink implements ExtLinkInterface {
@@ -27,11 +27,17 @@ public void setup(ExtSimAdapterData data) {
2727

2828
String mosaikIP = arguments.mosaikIP();
2929
Path mappingPath = arguments.mappingPath();
30+
int stepSize = arguments.stepSize();
3031

3132
extSim =
3233
switch (arguments.simulation()) {
33-
case PRIMARY_RESULT -> new PrimaryResultSimulation(mosaikIP, mappingPath);
34-
case MOSAIK_OPTIMIZER -> new MosaikOptimizerSimulation(mosaikIP, mappingPath);
34+
case PRIMARY_RESULT -> new PrimaryResultSimulation(mosaikIP, mappingPath, stepSize);
35+
case FLEX_OPTION_OPTIMIZER ->
36+
new FlexOptionOptimizerSimulation(
37+
mosaikIP,
38+
mappingPath,
39+
stepSize,
40+
arguments.useFlexOptionEntitiesInsteadOfEmAgents());
3541
};
3642

3743
extSim.setAdapterData(data);

src/main/java/edu/ie3/simosaik/SimosaikUtils.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,23 @@ public static Map<String, Object> createSimosaikOutputMap(
126126

127127
public static void addResult(
128128
ExtResultContainer results, String id, String attr, Map<String, Object> outputMap) {
129-
if (equalsAny(attr, MOSAIK_VOLTAGE_DEVIATION_PU, MOSAIK_VOLTAGE_PU)) {
129+
if (equalsAny(attr, MOSAIK_VOLTAGE_DEVIATION_PU)) {
130130
if (results.getTick() == 0L) {
131131
outputMap.put(attr, 0d);
132132
} else {
133133
// grid related results are not sent in time step zero
134134
outputMap.put(attr, results.getVoltageDeviation(id));
135135
}
136136
}
137+
if (equalsAny(attr, MOSAIK_VOLTAGE_PU)) {
138+
if (results.getTick() == 0L) {
139+
outputMap.put(attr, 1d);
140+
} else {
141+
// grid related results are not sent in time step zero
142+
outputMap.put(attr, results.getVoltageDeviation(id));
143+
}
144+
}
145+
137146
if (equalsAny(attr, MOSAIK_ACTIVE_POWER, MOSAIK_ACTIVE_POWER_IN)) {
138147
outputMap.put(attr, results.getActivePower(id));
139148
}

src/main/java/edu/ie3/simosaik/config/ArgsParser.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ public class ArgsParser {
2222
* @param mosaikIP the IP of the socket
2323
* @param mappingPath path for the {@link ExtEntityMapping}
2424
* @param simulation the {@link MosaikSimulator}
25+
* @param stepSize option for the step size
26+
* @param useFlexOptionEntitiesInsteadOfEmAgents false, to communicate with em agents
2527
*/
2628
public record Arguments(
27-
String[] mainArgs, String mosaikIP, Path mappingPath, Simulation simulation) {}
29+
String[] mainArgs,
30+
String mosaikIP,
31+
Path mappingPath,
32+
Simulation simulation,
33+
int stepSize,
34+
boolean useFlexOptionEntitiesInsteadOfEmAgents) {}
2835

2936
/**
3037
* Method for parsing the provided arguments.
@@ -45,7 +52,13 @@ public static Arguments parse(String[] args) {
4552
Path configPath = Path.of(extract(parsedArgs, "--config"));
4653
SimosaikConfig config = SimosaikConfig.load(configPath);
4754

48-
return new Arguments(args, mosaikIP, config.mappingPath(), config.simulation());
55+
return new Arguments(
56+
args,
57+
mosaikIP,
58+
config.mappingPath(),
59+
config.simulation(),
60+
config.stepSize(),
61+
config.useFlexOptionEntitiesInsteadOfEmAgents());
4962
}
5063

5164
/**

src/main/java/edu/ie3/simosaik/config/SimosaikConfig.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,29 @@
77
package edu.ie3.simosaik.config;
88

99
import com.typesafe.config.Config;
10+
import com.typesafe.config.ConfigException;
1011
import com.typesafe.config.ConfigFactory;
12+
import edu.ie3.datamodel.utils.Try;
1113
import java.nio.file.Files;
1214
import java.nio.file.Path;
15+
import java.time.Duration;
1316
import java.util.Optional;
1417

15-
public record SimosaikConfig(Path mappingPath, Simulation simulation) {
18+
public record SimosaikConfig(
19+
Path mappingPath,
20+
Simulation simulation,
21+
int stepSize,
22+
boolean useFlexOptionEntitiesInsteadOfEmAgents) {
1623

1724
public static SimosaikConfig load(Path filePath) {
1825
if (!Files.isReadable(filePath)) {
1926
throw new IllegalArgumentException("Config file at " + filePath + " is not readable.");
2027
}
2128

22-
Config config = ConfigFactory.parseFile(filePath.toFile()).getConfig("simosaik");
29+
Config baseConfig = ConfigFactory.parseFile(filePath.toFile());
30+
int stepSize = getStepSize(baseConfig);
31+
32+
Config config = baseConfig.getConfig("simosaik");
2333

2434
String simulation;
2535
try {
@@ -28,7 +38,21 @@ public static SimosaikConfig load(Path filePath) {
2838
simulation = "PrimaryResult";
2939
}
3040

41+
boolean useFlexOptionEntitiesInsteadOfEmAgents = false;
42+
// config.getBoolean("useFlexOptionEntitiesInsteadOfEmAgents");
43+
3144
return new SimosaikConfig(
32-
Path.of(config.getString("mappingPath")), Simulation.parse(simulation));
45+
Path.of(config.getString("mappingPath")),
46+
Simulation.parse(simulation),
47+
stepSize,
48+
useFlexOptionEntitiesInsteadOfEmAgents);
49+
}
50+
51+
private static int getStepSize(Config baseConfig) {
52+
Try.TrySupplier<Duration, ConfigException> supplier =
53+
() -> baseConfig.getConfig("simona").getConfig("powerflow").getDuration("resolution");
54+
return Try.of(supplier, ConfigException.class)
55+
.map(duration -> (int) duration.toSeconds())
56+
.getOrThrow();
3357
}
3458
}

src/main/java/edu/ie3/simosaik/config/Simulation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
public enum Simulation {
1010
PRIMARY_RESULT,
11-
MOSAIK_OPTIMIZER;
11+
FLEX_OPTION_OPTIMIZER;
1212

1313
public static Simulation parse(String simulation) {
1414
return switch (simulation.toLowerCase()) {
1515
case "primaryresult" -> PRIMARY_RESULT;
16-
case "mosaikoptimizer" -> MOSAIK_OPTIMIZER;
16+
case "flexoptionoptimizer" -> FLEX_OPTION_OPTIMIZER;
1717
default -> throw new IllegalStateException("Unexpected value: " + simulation.toLowerCase());
1818
};
1919
}

src/main/java/edu/ie3/simosaik/primaryResultSimulator/PrimaryResultSimulation.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public class PrimaryResultSimulation extends MosaikSimulation {
1919
private final ExtPrimaryDataConnection extPrimaryDataConnection;
2020
private final ExtResultDataConnection extResultDataConnection;
2121

22-
public PrimaryResultSimulation(String mosaikIP, Path mappingPath) {
23-
super("MosaikPrimaryResultSimulation", mosaikIP, mappingPath, new PrimaryResultSimulator());
22+
public PrimaryResultSimulation(String mosaikIP, Path mappingPath, int stepSize) {
23+
super(
24+
"MosaikPrimaryResultSimulation",
25+
mosaikIP,
26+
mappingPath,
27+
new PrimaryResultSimulator(stepSize));
2428

2529
// set up connection
2630
this.extPrimaryDataConnection = buildPrimaryConnection(mapping, log);
@@ -36,7 +40,10 @@ public Set<ExtDataConnection> getDataConnections() {
3640
protected Optional<Long> activity(long tick, long nextTick) throws InterruptedException {
3741
Optional<Long> maybeNextTick = Optional.of(nextTick);
3842

43+
// sending primary data to SIMONA
3944
sendPrimaryDataToSimona(extPrimaryDataConnection, tick, maybeNextTick, log);
45+
46+
// sending results to mosaik
4047
sendResultToExt(extResultDataConnection, tick, maybeNextTick, log);
4148

4249
return maybeNextTick;

src/main/java/edu/ie3/simosaik/primaryResultSimulator/PrimaryResultSimulator.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ public class PrimaryResultSimulator extends MosaikSimulator {
2121
private Set<String> simonaPrimaryEntities;
2222
private Set<String> simonaResultEntities;
2323

24-
public PrimaryResultSimulator() {
25-
super("PrimaryResultSimulator", 900);
26-
}
27-
2824
public PrimaryResultSimulator(int stepSize) {
2925
super("PrimaryResultSimulator", stepSize);
3026
}
3127

3228
@Override
3329
public Map<String, Object> init(String sid, Float timeResolution, Map<String, Object> simParams) {
34-
return MetaUtils.createMeta(
30+
return MetaUtils.createMetaWithPowerGrid(
3531
"time-based",
36-
ModelParams.simonaPowerGridEnvironment(),
3732
ModelParams.of(PRIMARY_INPUT_ENTITIES),
3833
ModelParams.of(RESULT_OUTPUT_ENTITIES));
3934
}

0 commit comments

Comments
 (0)