Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simulation precision parameter to DynaWaltzParameters #353

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class DynaWaltzParameters extends AbstractExtension<DynamicSimulationPara
public static final String SOLVER_OUTPUT_PARAMETERS_FILE = "solvers.par";
private static final boolean DEFAULT_WRITE_FINAL_STATE = true;
public static final boolean USE_MODEL_SIMPLIFIERS = false;
public static final double DEFAULT_PRECISION = 1e-6;

public enum SolverType {
SIM,
Expand All @@ -54,6 +55,7 @@ public enum SolverType {
private boolean writeFinalState = DEFAULT_WRITE_FINAL_STATE;
private boolean useModelSimplifiers = USE_MODEL_SIMPLIFIERS;
private DumpFileParameters dumpFileParameters;
private double precision = DEFAULT_PRECISION;

/**
* Loads parameters from the default platform configuration.
Expand Down Expand Up @@ -119,9 +121,11 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem
if (useDumpFile && (exportFolderNotFound || dumpFile == null || !Files.exists(exportDumpFileFolderPath.resolve(dumpFile)))) {
throw new PowsyblException("File " + dumpFile + " set in 'dumpFile' property cannot be found");
}

DumpFileParameters dumpFileParameters = new DumpFileParameters(exportDumpFile, useDumpFile, exportDumpFileFolderPath, dumpFile);

// Simulation precision
Double precision = config.map(c -> c.getDoubleProperty("precision", DEFAULT_PRECISION)).orElse(DEFAULT_PRECISION);

// Load xml files
List<ParametersSet> modelsParameters = ParametersXml.load(parametersPath);
ParametersSet networkParameters = ParametersXml.load(networkParametersPath, networkParametersId);
Expand All @@ -135,7 +139,8 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem
.setMergeLoads(mergeLoads)
.setWriteFinalState(writeFinalState)
.setUseModelSimplifiers(useModelSimplifiers)
.setDumpFileParameters(dumpFileParameters);
.setDumpFileParameters(dumpFileParameters)
.setPrecision(precision);
}

@Override
Expand Down Expand Up @@ -234,4 +239,13 @@ public DynaWaltzParameters setDefaultDumpFileParameters() {
this.dumpFileParameters = DumpFileParameters.createDefaultDumpFileParameters();
return this;
}

public double getPrecision() {
return precision;
}

public DynaWaltzParameters setPrecision(double precision) {
this.precision = precision;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static void writeSimulation(XMLStreamWriter writer, DynaWaltzContext con
writer.writeEmptyElement(DYN_URI, "simulation");
writer.writeAttribute("startTime", Integer.toString(context.getParameters().getStartTime()));
writer.writeAttribute("stopTime", Integer.toString(context.getParameters().getStopTime()));
writer.writeAttribute("precision", Double.toString(context.getDynaWaltzParameters().getPrecision()));
}

private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context) throws XMLStreamException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ void checkParameters() throws IOException {
String solverParametersId = "solverParametersId";
boolean mergeLoads = true;
boolean useModelSimplifiers = true;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers);
double precision = 1e-8;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers, precision);

DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem);

Expand Down Expand Up @@ -89,6 +90,7 @@ void checkParameters() throws IOException {

assertEquals(mergeLoads, parameters.isMergeLoads());
assertEquals(useModelSimplifiers, parameters.isUseModelSimplifiers());
assertEquals(precision, parameters.getPrecision());
}

@Test
Expand All @@ -111,7 +113,7 @@ void roundTripParametersSerializing() throws IOException {
SolverType solverType = SolverType.IDA;
String solverParametersId = "solverParametersId";
boolean mergeLoads = false;
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false);
initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false, 1e-7);

DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters()
.setStartTime(0)
Expand All @@ -122,7 +124,7 @@ void roundTripParametersSerializing() throws IOException {
JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json");
}

private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelSimplifiers) throws IOException {
private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelSimplifiers, double precision) throws IOException {
String parametersFile = USER_HOME + "parametersFile";
String networkParametersFile = USER_HOME + "networkParametersFile";
String solverParametersFile = USER_HOME + "solverParametersFile";
Expand All @@ -136,6 +138,7 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp
moduleConfig.setStringProperty("solver.type", solverType.toString());
moduleConfig.setStringProperty("solver.parametersId", solverParametersId);
moduleConfig.setStringProperty("useModelSimplifiers", String.valueOf(useModelSimplifiers));
moduleConfig.setStringProperty("precision", Double.toString(precision));

Files.createDirectories(fileSystem.getPath(USER_HOME));
copyFile("/parametersSet/models.par", parametersFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.nio.file.Path;

Expand All @@ -23,7 +22,7 @@
class JobsXmlTest extends DynaWaltzTestUtil {

@Test
void writeJob() throws SAXException, IOException, XMLStreamException {
void writeJob() throws SAXException, IOException {
DynamicSimulationParameters parameters = DynamicSimulationParameters.load();
DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load();
DynaWaltzContext context = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, parameters, dynawoParameters);
Expand All @@ -33,7 +32,7 @@ void writeJob() throws SAXException, IOException, XMLStreamException {
}

@Test
void writeJobWithDumpFile() throws SAXException, IOException, XMLStreamException {
void writeJobWithDumpFile() throws SAXException, IOException {
DynamicSimulationParameters parameters = DynamicSimulationParameters.load();
DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load()
.setDumpFileParameters(DumpFileParameters.createImportExportDumpFileParameters(Path.of("/dumpFiles"), "dump.dmp"));
Expand Down
1 change: 1 addition & 0 deletions dynawaltz/src/test/resources/DynaWaltzParameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dumpFileFolder" : null,
"dumpFile" : null
},
"precision" : 1.0E-7,
"modelsParameters" : [ {
"id" : "test",
"parameters" : {
Expand Down
2 changes: 1 addition & 1 deletion dynawaltz/src/test/resources/jobs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<dyn:precompiledModels useStandardModels="true"/>
<dyn:modelicaModels useStandardModels="false"/>
</dyn:modeler>
<dyn:simulation startTime="1" stopTime="100"/>
<dyn:simulation startTime="1" stopTime="100" precision="1.0E-6"/>
<dyn:outputs directory="outputs">
<dyn:dumpInitValues local="false" global="false"/>
<dyn:timeline exportMode="TXT"/>
Expand Down
2 changes: 1 addition & 1 deletion dynawaltz/src/test/resources/jobsWithDump.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<dyn:precompiledModels useStandardModels="true"/>
<dyn:modelicaModels useStandardModels="false"/>
</dyn:modeler>
<dyn:simulation startTime="1" stopTime="100"/>
<dyn:simulation startTime="1" stopTime="100" precision="1.0E-6"/>
<dyn:outputs directory="outputs">
<dyn:dumpInitValues local="false" global="false"/>
<dyn:timeline exportMode="TXT"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ void testSvc() {
.setNetworkParameters(networkParameters)
.setSolverParameters(solverParameters)
.setSolverType(DynaWaltzParameters.SolverType.IDA)
.setDefaultDumpFileParameters();
.setDefaultDumpFileParameters()
.setPrecision(10e-8);

DynamicSimulationResult result = provider.run(network, dynamicModelsSupplier, EventModelsSupplier.empty(), CurvesSupplier.empty(),
VariantManagerConstants.INITIAL_VARIANT_ID, computationManager, parameters, NO_OP)
Expand Down
Loading