Skip to content

Commit

Permalink
Patch for emcc (javascript runtime) from tshort
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18828 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 29, 2014
1 parent 31bf767 commit f1b3a19
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenC.tpl
Expand Up @@ -4085,7 +4085,7 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) <%makefileParams.cflags%> <%match sopt case SOME(s as SIMULATION_SETTINGS(__)) then '<%s.cflags%> ' /* From the simulate() command */%>
CPPFLAGS=-I"<%makefileParams.omhome%>/include/omc" -I. <%makefileParams.includes ; separator=" "%> -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME
LDFLAGS=<%dirExtra%> <%
if stringEq(Config.simCodeTarget(),"JavaScript") then <<-L'<%makefileParams.omhome%>/lib/omc/emcc' -lblas -llapack -lexpat -lSimulationRuntimeC -lf2c -s TOTAL_MEMORY=536870912 -s MAX_SETJMPS=20000 -s OUTLINING_LIMIT=20000 --pre-js <%makefileParams.omhome%>/lib/omc/emcc/pre.js>>
if stringEq(Config.simCodeTarget(),"JavaScript") then <<-L'<%makefileParams.omhome%>/lib/omc/emcc' -lblas -llapack -lexpat -lSimulationRuntimeC -lf2c --llvm-lto 2 -s TOTAL_MEMORY=268435456 -s MAX_SETJMPS=2000 -s OUTLINING_LIMIT=20000 --pre-js <%makefileParams.omhome%>/lib/omc/emcc/pre.js>>
else <<-L"<%makefileParams.omhome%>/lib/omc" -L"<%makefileParams.omhome%>/lib" -Wl,<% if stringEq(makefileParams.platform, "win32") then "--stack,0x2000000,"%>-rpath,"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,"<%makefileParams.omhome%>/lib" <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%>>>
%>
MAINFILE=<%fileNamePrefix%>.c
Expand Down
185 changes: 107 additions & 78 deletions Compiler/Template/CodegenJS.tpl
Expand Up @@ -17,44 +17,54 @@ case SIMCODE(modelInfo = MODELINFO(functions = functions, varInfo = vi as VARINF
simulationSettingsOpt = SOME(s as SIMULATION_SETTINGS(__)), makefileParams = makefileParams as MAKEFILE_PARAMS(__))
then
<<
# OpenModelica simulation example
## <%Util.escapeModelicaStringToXmlString(dotPath(modelInfo.name))%>

```yaml script=scriptloader
- tinytimer.js
- lib/tinytimer.js
```

```yaml script=dataloader
xml: <%fileNamePrefix%>_init.xml
```

# OpenModelica simulation example
## <%Util.escapeModelicaStringToXmlString(dotPath(modelInfo.name))%>
<style media="screen" type="text/css">
label {font-weight:normal; size: 0.9em}
</style>

<br/>
<br/>

<img src="<%fileNamePrefix%>.svg" class="pull-right" style="width:540px; background-color:#ffffff; border:2px solid gray" />

```yaml jquery=jsonForm class="form-horizontal" name=frm
schema:
stopTime:
type: string
title: Stop time [s]
default: <%s.stopTime%>
intervals:
type: string
title: Output intervals
default: <%realDiv(s.stopTime,s.stepSize)%>
tolerance:
type: string
title: Tolerance
default: <%s.tolerance%>
solver:
type: string
title: Solver
enum:
- dassl
- euler
- rungekutta
form:
- "*"
params:
fieldHtmlClass: input-medium
<div id="status" style="text-align:center"><span id="statustext">
Simulation loading</span>. &nbsp Time: <span id="statustimer"> </span></div>

<br/>

<div class = "row">
<div class = "col-md-4">

<br/>
<br/>
<br/>
<br/>

```yaml jquery=dform
class : form-horizontal
col1class : col-sm-7
col2class : col-sm-5
html:
- name: stopTime
type: number
bs3caption: Stop time, sec
value: 0.1
- name: intervals
type: number
bs3caption: Output intervals
value: 500
- name: tolerance
type: number
bs3caption: Tolerance
value: 0.0001
```

```js
Expand All @@ -68,15 +78,15 @@ defex = $xml.find("DefaultExperiment")
defex.attr("stopTime", stopTime)
defex.attr("stepSize", +stopTime / intervals)
defex.attr("tolerance", tolerance)
defex.attr("solver", solver)

// Set some model parameters - UNCOMMENT & CHANGE AS NEEDED
//$xml.find("ScalarVariable[name = 'RL.R']").find("Real").attr("start", RL)
// Set some model parameters
// Example:
// $xml.find("ScalarVariable[name = 'LAC']").find("Real").attr("start", LAC)

// Write out the initialization file
xmlstring = new XMLSerializer().serializeToString(xml)

$("#statustext").html('<img src="wait.gif" /> Simulation running')
$("#statustext").html('Simulation running')
$("#statustimer").html("");
$('#statustimer').tinyTimer({ from: Date.now() });

Expand All @@ -96,13 +106,7 @@ wworker.addEventListener('error', function(event) {

```

<div id="status" style="text-align:center"><span id="statustext">
Simulation loading</span>. &nbsp Time: <span id="statustimer"> </span></div>


## Results

<div id="yaxisform"> </div>

```js
// read the csv file with the simulation results
Expand All @@ -122,55 +126,72 @@ wworker.addEventListener("message", function(e) {
if (typeof(graphvarX) == "undefined") graphvarX = header[0];

var jsonform = {
schema: {
graphvar: {
type: "string",
title: "Plot variable",
default: graphvar,
enum: header
}
},
form: [
{
key: "graphvar",
onChange: function (evt) {
calculate_forms();
$("#plotdiv").calculate();
}
}
]
};
html: {
type: "select",
bs3caption: "Plot variable",
name: "graphvar",
selectvalue: graphvar,
choices: header
}};
var jsonformX = {
schema: {
graphvarX: {
type: "string",
default: graphvarX,
enum: x.slice(0,1)[0]
}
},
form: [
{
key: "graphvarX",
onChange: function (evt) {
calculate_forms();
$("#plotdiv").calculate();
}
}
]
};
html: {
type: "select",
bs3caption: "",
name: "graphvarX",
selectvalue: graphvarX,
choices: header
}};
updatefun = function (evt) {
calculate_forms();
$("#plotdiv").calculate();
}


$("#yaxisform").html("");
$("#yaxisform").jsonForm(jsonform);
$("#yaxisform").dform(jsonform);
$("#yaxisform").change(updatefun);
$("#xaxisform").html("");
$("#xaxisform").jsonForm(jsonformX);
$("#xaxisform").dform(jsonformX);
$("#xaxisform").change(updatefun);
$("#plotdiv").calculate();

}, false);

```

</div>

<div class = "col-md-1">
</div>


<div class = "col-md-7">

<!-- Nav tabs -->
<ul class="nav nav-tabs" id="mytab">
<li class="active"><a href="#model" data-toggle="tab">Model</a></li>
<li><a href="#results" data-toggle="tab">Results</a></li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
<!-- Model pane -->
<div class="tab-pane active" id="model">

<img src="<%fileNamePrefix%>.svg" style="width:100%; background-color:#ffffff; border:2px solid gray" />

</div>

<!-- Results pane -->
<div class="tab-pane" id="results">

</br>

<div id="yaxisform" style="width:15em; position:relative"> </div>

```js id=plotdiv
if (typeof(header) != "undefined") {
$("#mytab a:last").tab("show"); // Select last tab
yidx = header.indexOf(graphvar);
xidx = header.indexOf(graphvarX);
// pick out the column to plot
Expand All @@ -179,7 +200,15 @@ if (typeof(header) != "undefined") {
}
```

<div id="xaxisform" style="left:200px; width:300px; position:relative"> </div>
<div id="xaxisform" class="center-block" style="text-align:center; width:15em; position:relative"> </div>


</div>
</div>

</div>
</div>


## Comments

Expand Down
6 changes: 2 additions & 4 deletions SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -348,10 +348,6 @@ void initializeOutputFilter(MODEL_DATA *modelData, modelica_string variableFilte
return;
}

#if defined(OMC_EMCC)
/* We do not have libregex in javascript */
return;
#endif
rc = regcomp(&myregex, filter, flags);
if(rc) {
char err_buf[2048] = {0};
Expand Down Expand Up @@ -961,7 +957,9 @@ static void omc_assert_simulation(FILE_INFO info, const char *msg, ...)
fflush(NULL);
va_end(ap);
}
#ifndef OMC_EMCC
longjmp(nonlinearJmpbuf,1);
#endif
break;
case ERROR_INTEGRATOR:
if(ACTIVE_STREAM(LOG_DDASRT))
Expand Down
Expand Up @@ -44,7 +44,7 @@ extern "C"
{
#endif
extern int ipopt_initialization(DATA *data, INIT_DATA *initData, int useScaling);
// extern int ipopt_initialization(DATA *data, INIT_DATA *initData, int useScaling);

#ifdef __cplusplus
}
Expand Down
17 changes: 16 additions & 1 deletion SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c
Expand Up @@ -466,14 +466,17 @@ int solveHybrd(DATA *data, int sysNumber)
int scaling = solverData->useXScaling;
if(scaling)
solverData->useXScaling = 0;

#ifndef OMC_EMCC
/* try */
if(!setjmp(nonlinearJmpbuf))
{
#endif
wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
#ifndef OMC_EMCC
} else { /* catch */
warningStreamPrint(LOG_STDOUT, 0, "Non-Linear Solver try to handle a problem with a called assert.");
}
#endif
if(scaling) {
solverData->useXScaling = 1;
}
Expand Down Expand Up @@ -515,15 +518,18 @@ int solveHybrd(DATA *data, int sysNumber)
giveUp = 1;

/* try */
#ifndef OMC_EMCC
if(!setjmp(nonlinearJmpbuf))
{
#endif
_omc_hybrj_(wrapper_fvec_hybrj, &solverData->n, solverData->x,
solverData->fvec, solverData->fjac, &solverData->ldfjac, &solverData->xtol,
&solverData->maxfev, solverData->diag, &solverData->mode,
&solverData->factor, &solverData->nprint, &solverData->info,
&solverData->nfev, &solverData->njev, solverData->r__,
&solverData->lr, solverData->qtf, solverData->wa1,
solverData->wa2, solverData->wa3, solverData->wa4, data, sysNumber);
#ifndef OMC_EMCC
if(assertCalled) {
infoStreamPrint(LOG_NLS, 0, "After assertions failed, found a solution for which assertions did not fail.");
memcpy(systemData->nlsxOld, solverData->x, solverData->n*(sizeof(double)));
Expand Down Expand Up @@ -552,6 +558,7 @@ int solveHybrd(DATA *data, int sysNumber)
xerror = 1;
assertCalled = 1;
}
#endif

/* set residual function continuous */
if(continuous)
Expand Down Expand Up @@ -585,9 +592,12 @@ int solveHybrd(DATA *data, int sysNumber)
((DATA*)data)->simulationInfo.solveContinuous = 0;

/* try */
#ifndef OMC_EMCC
if(!setjmp(nonlinearJmpbuf))
{
#endif
wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
#ifndef OMC_EMCC
} else { /* catch */
warningStreamPrint(LOG_STDOUT, 0, "Non-Linear Solver try to handle a problem with a called assert.");

Expand All @@ -596,6 +606,7 @@ int solveHybrd(DATA *data, int sysNumber)
xerror = 1;
assertCalled = 1;
}
#endif

if(scaling)
solverData->useXScaling = 1;
Expand Down Expand Up @@ -687,9 +698,12 @@ int solveHybrd(DATA *data, int sysNumber)
memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double)));

/* try */
#ifndef OMC_EMCC
if(!setjmp(nonlinearJmpbuf))
{
#endif
wrapper_fvec_hybrj(&solverData->n, solverData->x, solverData->fvec, solverData->fjac, &solverData->ldfjac, &iflag, data, sysNumber);
#ifndef OMC_EMCC
}
else
{ /* catch */
Expand All @@ -702,6 +716,7 @@ int solveHybrd(DATA *data, int sysNumber)
success = 0;
giveUp = 0;
}
#endif
if(scaling)
solverData->useXScaling = 1;
}
Expand Down

0 comments on commit f1b3a19

Please sign in to comment.