Commit e07c9736 authored by Malte Heithoff's avatar Malte Heithoff

semantics

parent d4717d80
Pipeline #384373 failed with stage
in 1 minute and 6 seconds
......@@ -5,7 +5,9 @@ enable_language(CXX)
set(SERIALLOOPINCLUDE target/generated-sources-cpp/serialLoop)
set(SIMPLELOOPINCLUDE target/generated-sources-cpp/simpleLoop)
set(OSCILLATIONINCLUDE target/generated-sources-cpp/oscillation)
set(PARALLELLOOPINCLUDE target/generated-sources-cpp/parallelLoop)
add_subdirectory(${SERIALLOOPINCLUDE})
add_subdirectory(${SIMPLELOOPINCLUDE})
add_subdirectory(${OSCILLATIONINCLUDE})
add_subdirectory(${PARALLELLOOPINCLUDE})
add_subdirectory(looptest)
\ No newline at end of file
if exist build del /F /Q /S build
@ECHO OFF
if exist build del /F /Q /S build > nul
mkdir build
cd build
@ECHO ON
cmake ..
cmake --build . --config release
cd ..
\ No newline at end of file
......@@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.5)
project(LoopTest CXX)
set(CMAKE_CXX_STANDARD 11)
message("JO ${CMAKE_CURRENT_SOURCE_DIR} ${OSCILLATIONINCLUDE}")
add_executable(Oscillation mainOscillation.cpp)
target_include_directories(Oscillation PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(Oscillation de_monticore_lang_monticar_semantics_loops_oscillation ${LIBS})
......@@ -18,3 +16,8 @@ add_executable(SimpleLoop mainSimpleLoop.cpp)
target_include_directories(SimpleLoop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(SimpleLoop de_monticore_lang_monticar_semantics_loops_simpleLoop ${LIBS})
export(TARGETS SimpleLoop FILE SimpleLoop.cmake)
add_executable(ParallelLoop mainParallelLoop.cpp)
target_include_directories(ParallelLoop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(ParallelLoop de_monticore_lang_monticar_semantics_loops_parallelLoop ${LIBS})
export(TARGETS ParallelLoop FILE ParallelLoop.cmake)
#include "de_monticore_lang_monticar_semantics_loops_oscillation.h"
#include "ExecutionStepper.h"
int main(int argc, char** argv)
{
de_monticore_lang_monticar_semantics_loops_oscillation instance;
......@@ -6,7 +7,7 @@ int main(int argc, char** argv)
int i;
for (i=1; i<atof(argv[1]); i++) {
instance.execute();
std::cout << instance.out1 << "\n";
std::cout << "t = " << getCurrentTime() << ":\t " << instance.output << "\n";
}
return 0;
}
\ No newline at end of file
#include "de_monticore_lang_monticar_semantics_loops_parallelLoop.h"
#include "ExecutionStepper.h"
int main(int argc, char** argv)
{
de_monticore_lang_monticar_semantics_loops_parallelLoop instance;
instance.init();
int i;
for (i=1; i<argc; i++) {
instance.in1 = atof(argv[i]);
instance.execute();
std::cout << "t = " << getCurrentTime() << ":\t " << instance.out1 << "\n";
}
return 0;
}
\ No newline at end of file
#include "de_monticore_lang_monticar_semantics_loops_serialLoop.h"
#include "ExecutionStepper.h"
int main(int argc, char** argv)
{
de_monticore_lang_monticar_semantics_loops_serialLoop instance;
......@@ -7,7 +8,7 @@ int main(int argc, char** argv)
for (i=1; i<argc; i++) {
instance.in1 = atof(argv[i]);
instance.execute();
std::cout << instance.out1 << "\n";
std::cout << "t = " << getCurrentTime() << ":\t " << instance.out1 << "\n";
}
return 0;
}
\ No newline at end of file
#include "de_monticore_lang_monticar_semantics_loops_simpleLoop.h"
#include "ExecutionStepper.h"
int main(int argc, char** argv)
{
de_monticore_lang_monticar_semantics_loops_simpleLoop instance;
......@@ -7,7 +8,7 @@ int main(int argc, char** argv)
for (i=1; i<argc; i++) {
instance.in1 = atof(argv[i]);
instance.execute();
std::cout << instance.out1 << "\n";
std::cout << "t = " << getCurrentTime() << ":\t " << instance.out1 << "\n";
}
return 0;
}
\ No newline at end of file
......@@ -17,7 +17,7 @@
<properties>
<!-- .. EMA-Libraries ................................................. -->
<EmbeddedMontiArc-semantics.version>0.4.4-SNAPSHOT</EmbeddedMontiArc-semantics.version>
<EmbeddedMontiArc-executionSemantics.version>0.4.4-SNAPSHOT</EmbeddedMontiArc-executionSemantics.version>
<!-- .. Libraries .................................................. -->
<guava.version>25.1-jre</guava.version>
......@@ -55,8 +55,8 @@
<!-- MontiCore Dependencies -->
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>semantics</artifactId>
<version>${EmbeddedMontiArc-semantics.version}</version>
<artifactId>executionSemantics</artifactId>
<version>${EmbeddedMontiArc-executionSemantics.version}</version>
</dependency>
......@@ -69,7 +69,7 @@
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
<version>2.3.29</version>
</dependency>
<dependency>
......
......@@ -12,10 +12,7 @@ import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Class to configure cmake file generation.
......@@ -34,6 +31,8 @@ public class CMakeConfig {
conf.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
conf.setLogTemplateExceptions(false);
conf.setClassForTemplateLoading(AllTemplates.class, "/template/cmake/");
conf.setNumberFormat("#.################");
conf.setLocale(Locale.ENGLISH);
try {
CMAKE_LISTS_CPP = conf.getTemplate("CMakeListsCppTemplate.ftl");
CMAKE_FIND_PACKAGE = conf.getTemplate("CMakeFindPackageTemplate.ftl");
......
......@@ -4,21 +4,24 @@ package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.FileContent;
public class ExecutionStepperHelper {
public static String fileName = "ExecutionStepper";
private static boolean usedTime = false;
private static double DT = 0.1;
public static String FILENAME = "ExecutionStepper";
private static boolean TIME_USED = false;
public static void setUsed() {
usedTime = true;
TIME_USED = true;
}
public static boolean isUsed() {
return usedTime;
return TIME_USED;
}
public static FileContent getTimeHelperFileContent(double dt) {
FileContent fileContent = new FileContent();
fileContent.setFileName(fileName + ".h");
fileContent.setFileName(FILENAME + ".h");
String fileContentString = getTimeHelperSourceCode(dt);
fileContent.setFileContent(fileContentString);
......@@ -26,7 +29,7 @@ public class ExecutionStepperHelper {
}
public static FileContent getTimeHelperFileContent(){
return getTimeHelperFileContent(1);
return getTimeHelperFileContent(DT);
}
public static String getTimeHelperSourceCode(double dt) {
......@@ -46,6 +49,14 @@ public class ExecutionStepperHelper {
}
public static String getTimeVariableName() {
return de.monticore.lang.monticar.semantics.Options.timeName;
return de.monticore.lang.monticar.semantics.Constants.timeName;
}
public static double getDT() {
return DT;
}
public static void setDT(double dt) {
ExecutionStepperHelper.DT = DT;
}
}
......@@ -18,14 +18,17 @@ import de.monticore.lang.monticar.generator.cpp.instruction.ConnectInstructionCP
import de.monticore.lang.monticar.generator.cpp.loopSolver.CPPEquationSystemHelper;
import de.monticore.lang.monticar.generator.cpp.loopSolver.daecpp.DAECPPEquationSystemGenerator;
import de.monticore.lang.monticar.generator.cpp.loopSolver.daecpp.DAECPPOptions;
import de.monticore.lang.monticar.generator.cpp.loopSolver.odeint.OdeintEquationSystemGenerator;
import de.monticore.lang.monticar.generator.cpp.loopSolver.odeint.OdeintOptions;
import de.monticore.lang.monticar.generator.cpp.mathopt.MathOptSolverConfig;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import de.monticore.lang.monticar.generator.cpp.viewmodel.AutopilotAdapterDataModel;
import de.monticore.lang.monticar.generator.cpp.viewmodel.ServerWrapperViewModel;
import de.monticore.lang.monticar.generator.testing.StreamTestGenerator;
import de.monticore.lang.monticar.semantics.helper.NameHelper;
import de.monticore.lang.monticar.semantics.loops.detection.EMAEquationSystem;
import de.monticore.lang.monticar.semantics.loops.detection.LoopSymbolInstance;
import de.monticore.lang.monticar.semantics.loops.detection.ConnectionHelper;
import de.monticore.lang.monticar.semantics.loops.symbols.EMAEquationSystem;
import de.monticore.lang.monticar.semantics.loops.symbols.LoopComponentSymbolInstance;
import de.monticore.lang.monticar.semantics.resolve.SymbolTableHelper;
import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
......@@ -63,7 +66,6 @@ public class GeneratorCPP implements EMAMGenerator {
protected boolean checkModelDir = false;
protected boolean streamTestGenerationMode = false;
public boolean isGenerateCV = false;
protected double dt = 1;
// CMake
private boolean generateCMake = false;
......@@ -195,14 +197,14 @@ public class GeneratorCPP implements EMAMGenerator {
return false;
}
});
bluePrintCPP.addAdditionalIncludeString(ExecutionStepperHelper.fileName);
bluePrintCPP.addAdditionalIncludeString(ExecutionStepperHelper.FILENAME);
}
}
// connect information to eqs
if (componentSymbol instanceof LoopSymbolInstance) {
if (componentSymbol instanceof LoopComponentSymbolInstance) {
ExecutionStepperHelper.setUsed();
EMAEquationSystem equationSystem = ((LoopSymbolInstance) componentSymbol).getEquationSystem();
EMAEquationSystem equationSystem = ((LoopComponentSymbolInstance) componentSymbol).getEquationSystem();
String eqsName = equationSystem.getName();
Variable eqs = new Variable("eqs", "");
eqs.setVariableType(new VariableType("", eqsName, eqsName));
......@@ -212,7 +214,7 @@ public class GeneratorCPP implements EMAMGenerator {
for (EMAPortInstanceSymbol inport : equationSystem.getInports()) {
Optional<EMAPortInstanceSymbol> originalSourcePort = equationSystem.getAtomicSourceOf(inport);
Optional<EMAPortInstanceSymbol> currentPort = componentSymbol.getIncomingPortInstances().stream()
.filter(i -> SymbolTableHelper.getAtomicSourceOf(i).equals(originalSourcePort))
.filter(i -> ConnectionHelper.sourceOf(i).equals(originalSourcePort))
.findFirst();
if (currentPort.isPresent()) {
String sourceName = currentPort.get().getName();
......@@ -270,12 +272,12 @@ public class GeneratorCPP implements EMAMGenerator {
//setGenerateMainClass(true);
}
if (componentInstanceSymbol instanceof LoopSymbolInstance) {
((LoopSymbolInstance) componentInstanceSymbol).getEquationSystem()
if (componentInstanceSymbol instanceof LoopComponentSymbolInstance) {
((LoopComponentSymbolInstance) componentInstanceSymbol).getEquationSystem()
.setName(String.join("_",
NameHelper.replaceWithUnderScore(NameHelper.calculateFullQualifiedNameOf(rootModel)),
((LoopSymbolInstance) componentInstanceSymbol).getEquationSystem().getName()));
for (CMakeFindModule dependency : DAECPPOptions.getDependencies()) {
((LoopComponentSymbolInstance) componentInstanceSymbol).getEquationSystem().getName()));
for (CMakeFindModule dependency : OdeintOptions.getDependencies()) {
cMakeConfig.addModuleDependency(dependency);
}
}
......@@ -340,10 +342,10 @@ public class GeneratorCPP implements EMAMGenerator {
}
if (componentInstanceSymbol instanceof LoopSymbolInstance) {
if (!equationSystemsAlreadyBuild.contains(((LoopSymbolInstance) componentInstanceSymbol).getEquationSystem())) {
fileContents.addAll(DAECPPEquationSystemGenerator.generateEquationSystem(
((LoopSymbolInstance) componentInstanceSymbol).getEquationSystem()));
if (componentInstanceSymbol instanceof LoopComponentSymbolInstance) {
if (!equationSystemsAlreadyBuild.contains(((LoopComponentSymbolInstance) componentInstanceSymbol).getEquationSystem())) {
fileContents.addAll(OdeintEquationSystemGenerator.generateEquationSystem(
((LoopComponentSymbolInstance) componentInstanceSymbol).getEquationSystem()));
}
}
......@@ -384,7 +386,7 @@ public class GeneratorCPP implements EMAMGenerator {
}
// Add advanceTime
if (ExecutionStepperHelper.isUsed()) {
fileContents.add(ExecutionStepperHelper.getTimeHelperFileContent(dt));
fileContents.add(ExecutionStepperHelper.getTimeHelperFileContent());
}
List<File> files = saveFilesToDisk(fileContents);
//cmake
......@@ -691,15 +693,15 @@ public class GeneratorCPP implements EMAMGenerator {
}
public double getDeltaT() {
return dt;
return ExecutionStepperHelper.getDT();
}
public void setDeltaT(double dt) {
this.dt = dt;
ExecutionStepperHelper.setDT(dt);
}
public void setDeltaT(String dt) {
double v = Double.parseDouble(dt);
this.dt = v;
setDeltaT(v);
}
}
......@@ -98,7 +98,7 @@ public class MathFunctionFixer extends BaseMathFunctionFixerHandler {
.equals(((MathNameExpressionSymbol) mathExpressionSymbol).getNameToResolveValue())) {
ExecutionStepperHelper.setUsed();
((MathNameExpressionSymbol) mathExpressionSymbol).setNameToResolveValue("getCurrentTime()");
bluePrintCPP.addAdditionalIncludeString(ExecutionStepperHelper.fileName);
bluePrintCPP.addAdditionalIncludeString(ExecutionStepperHelper.FILENAME);
}
notHandled = false;
} else if (((MathValueExpressionSymbol) mathExpressionSymbol).isBooleanExpression()) {
......
......@@ -11,10 +11,11 @@ import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.expression.MathNameExpressionSymbol;
import de.monticore.lang.math._symboltable.expression.MathParenthesisExpressionSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.cpp.ExecutionStepperHelper;
import de.monticore.lang.monticar.generator.cpp.viewmodel.ViewModelBase;
import de.monticore.lang.monticar.semantics.helper.NameHelper;
import de.monticore.lang.monticar.semantics.loops.analyze.SpecificationConverter;
import de.monticore.lang.monticar.semantics.loops.detection.EMAEquationSystem;
import de.monticore.lang.monticar.semantics.loops.symbols.EMAEquationSystem;
import de.monticore.lang.monticar.semantics.resolve.SymbolTableHelper;
import de.monticore.lang.monticar.semantics.util.math.NameReplacer;
import de.se_rwth.commons.logging.Log;
......@@ -32,6 +33,15 @@ public class EquationSystemViewModel extends ViewModelBase {
private List<String> function = new ArrayList<>();
private List<String> inports = new ArrayList<>();
private boolean isAlgebraic = true;
private double atol;
private double rtol;
private double jtol;
private double dt_init;
private double dt_max;
private double loggingLevel;
private List<EMAComponentInstanceSymbol> algebraicComponents = new ArrayList<>();
public String getName() {
......@@ -58,12 +68,50 @@ public class EquationSystemViewModel extends ViewModelBase {
return inports;
}
public boolean getIsAlgebraic() {
return isAlgebraic;
}
public double getAtol() {
return atol;
}
public double getRtol() {
return rtol;
}
public double getJtol() {
return jtol;
}
public double getDt_init() {
return dt_init;
}
public double getDt_max() {
return dt_max;
}
public double getLoggingLevel() {
return loggingLevel;
}
public List<EMAComponentInstanceSymbol> getAlgebraicComponents() {
return algebraicComponents;
}
private void setOptions() {
this.atol = NumericSolverOptions.ATOL;
this.rtol = NumericSolverOptions.RTOL;
this.jtol = NumericSolverOptions.JTOL;
this.loggingLevel = NumericSolverOptions.LEVEL_LOGGING;
this.dt_init = NumericSolverOptions.DT_SOLVER;
this.dt_max = NumericSolverOptions.DT_SOLVER;
}
public EquationSystemViewModel (EMAEquationSystem eqs) {
setOptions();
this.name = eqs.getName();
Map<String, String> inportMapping = new HashMap<>();
......@@ -151,6 +199,10 @@ public class EquationSystemViewModel extends ViewModelBase {
CPPEquationSystemHelper.getNameOfPortOfComponent(portVariable.getPort().get())));
}
}
if (massMatrixDiag.stream().filter(s -> "1".equals(s))
.findFirst().isPresent())
this.isAlgebraic = false;
}
private String getFunction(MathExpressionSymbol symbol,
......@@ -184,7 +236,7 @@ public class EquationSystemViewModel extends ViewModelBase {
@Override
public void visit(MathMatrixNameExpressionSymbol node) {
if (node.getNameToAccess().equals(de.monticore.lang.monticar.semantics.Options.derivativeOperatorName)) {
if (node.getNameToAccess().equals(de.monticore.lang.monticar.semantics.Constants.derivativeOperatorName)) {
if(! (getParents().peek() instanceof EMAMEquationSymbol))
Log.error("Not supported diff operator: should stand alone to the lift side of an equation");
if (! ((EMAMEquationSymbol) getParents().peek()).getLeftExpression().equals(node))
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp.loopSolver;
public class NumericSolverOptions {
// Absolute tolerance for the solver
public static double ATOL = 1.0E-15;
// Relative tolerance for the solver
public static double RTOL = 1.0E-15;
// Tolerance for the numerical Jacobian
public static double JTOL = 1.0E-6;
// Level for logging, if it is supported
public static int LEVEL_LOGGING = 0;
// Delta t used in the solver, lower delta t leads to longer execution times
// but better accuracy
public static double DT_SOLVER = 1.0E-2;
}
......@@ -5,7 +5,7 @@ import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.cpp.loopSolver.EquationSystemViewModel;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import de.monticore.lang.monticar.generator.cpp.template.TemplateHelper;
import de.monticore.lang.monticar.semantics.loops.detection.EMAEquationSystem;
import de.monticore.lang.monticar.semantics.loops.symbols.EMAEquationSystem;
import de.se_rwth.commons.logging.Log;
import freemarker.template.Configuration;
import freemarker.template.Template;
......@@ -16,6 +16,7 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
public class DAECPPEquationSystemGenerator {
......@@ -25,11 +26,13 @@ public class DAECPPEquationSystemGenerator {
private static final Template RHS_TEMPLATE;
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
Configuration conf = new Configuration(Configuration.VERSION_2_3_29);
conf.setDefaultEncoding("UTF-8");
conf.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
conf.setLogTemplateExceptions(false);
conf.setClassForTemplateLoading(AllTemplates.class, "/template/loopSolver/daecpp/");
conf.setNumberFormat("#.################");
conf.setLocale(Locale.ENGLISH);
try {
EQUATIONSYSTEM_TEMPLATE = conf.getTemplate("EquationSystem.ftl");
MASSMATRIX_TEMPLATE = conf.getTemplate("MassMatrix.ftl");
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp.loopSolver.odeint;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.cpp.loopSolver.EquationSystemViewModel;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import de.monticore.lang.monticar.generator.cpp.template.TemplateHelper;
import de.monticore.lang.monticar.semantics.loops.symbols.EMAEquationSystem;
import de.se_rwth.commons.logging.Log;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
public class OdeintEquationSystemGenerator {
private static final Template EQUATIONSYSTEM_TEMPLATE;
private static final Template RHS_TEMPLATE;
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_29);
conf.setDefaultEncoding("UTF-8");
conf.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
conf.setLogTemplateExceptions(false);
conf.setClassForTemplateLoading(AllTemplates.class, "/template/loopSolver/odeint/");
conf.setNumberFormat("#.################");
conf.setLocale(Locale.ENGLISH);
try {
EQUATIONSYSTEM_TEMPLATE = conf.getTemplate("EquationSystem.ftl");
RHS_TEMPLATE = conf.getTemplate("RHS.ftl");
} catch (IOException e) {
String msg = "could not load cmake templates";
Log.error(msg, e);
throw new RuntimeException(msg, e);
}
}
public static Collection<FileContent> generateEquationSystem(EMAEquationSystem eqs) {
Collection<FileContent> result = new HashSet<>();
EquationSystemViewModel viewModel = new EquationSystemViewModel(eqs);
// map data
Map<String, Object> dataForTemplate = TemplateHelper.getDataForTemplate(viewModel);
// try generate file content
try {
StringWriter sw = new StringWriter();
EQUATIONSYSTEM_TEMPLATE.process(dataForTemplate, sw);
result.add(new FileContent(sw.toString(), String.format("/%s.h", eqs.getName())));
sw = new StringWriter();
RHS_TEMPLATE.process(dataForTemplate, sw);
result.add(new FileContent(sw.toString(), String.format("/%s_RHS.h", eqs.getName())));
} catch (TemplateException | IOException e) {
Log.error("EquationSystem template generation failed. ", e);
}
return result;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp.loopSolver.odeint;
import de.monticore.lang.monticar.generator.cmake.CMakeFindModule;
import org.apache.commons.lang3.SystemUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;