From 317f00a2332271d63b7174259e823c6a05b83327 Mon Sep 17 00:00:00 2001 From: Randall Britten Date: Thu, 27 Jun 2013 18:19:09 +1200 Subject: [PATCH 1/2] Initial rough attempt at JavaScript code generation template. Copied from MATLAB.xml, then altered. Still very rough and untested. See tracker item 3607. --- CeLEDS/languages/JavaScript.xml | 192 ++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 CeLEDS/languages/JavaScript.xml diff --git a/CeLEDS/languages/JavaScript.xml b/CeLEDS/languages/JavaScript.xml new file mode 100644 index 0000000..9131395 --- /dev/null +++ b/CeLEDS/languages/JavaScript.xml @@ -0,0 +1,192 @@ + + + + +JavaScript + + + ( + ) + Math.abs(#expr1) + #exprs[&] + Math.acos(#expr1) + acosh(#expr1) + acot(#expr1) + acoth(#expr1) + acsc(#expr1) + acsch(#expr1) + asec(#expr1) + asech(#expr1) + Math.asin(#expr1) + asinh(#expr1) + Math.atan(#expr1) + atanh(#expr1) + Math.ceil(#expr1) + Math.cos(#expr1) + cosh(#expr1) + cot(#expr1) + coth(#expr1) + csc(#expr1) + csch(#expr1) + #lookupDiffVariable + #expr1/#expr2 + #exprs[==] + Math.exp(#expr1) + factorial(#expr1) + (#expr1%#expr2) == 0 + Math.floor(#expr1) + gcd_multi([#exprs[, ]]) + #exprs[>=] + #exprs[>] + ~#expr1 || #expr2 + defint(func#unique1, BOUND, CONSTANTS, RATES, VARIABLES, #bvarIndex)#supplement double func#unique1(double* BOUND, double* CONSTANTS, double* RATES, double* VARIABLES) { return #expr1; } + lcm_multi([#exprs[, ]]) + #exprs[<=] + Math.log(#expr1) + arbitrary_log(#expr1, #logbase) + #exprs[<] + Math.max(#exprs[, ]) + Math.min([#exprs[, ]) + #expr1 - #expr2 + #expr1 != #expr2 + !#expr1 + #exprs[||] + #exprs[+] + Math.pow(#expr1, #expr2) + Math.floor(#expr1 / #expr2) + #expr1 % #expr2 + Math.pow(#expr1, 1.0 ./ #degree) + 1/Math.cos(#expr1) + sech(#expr1) + Math.sin(#expr1) + sinh(#expr1) + Math.tan(#expr1) + tanh(#expr1) + #exprs[*] + - #expr1 + #expr1 * #expr2 + #expr3 + #expr1 * #expr2 + #expr1+#expr2 + xor(#expr1 , #expr2) + piecewise({#expr1, #expr2 + , #expr1, #expr2 + }, #expr1) + }, NaN) + Math.PI + 0.577215664901533 + Inf + + + + CONSTANTS[%] + STATES[%] + ALGEBRAIC[%] + RATES[%] + VOI + 0 + <LHS> = <RHS>; + + <LHS> = <RHS>; + + + + + + + // There are a total of + entries in each of the algebraic variable array. + + // There are a total of + entries in each of the rate and state variable arrays. + + // There are a total of + entries in the constant variable array. +// + + ,,initialValues,computeRates,,1e9); + + y = numeric.transpose(sol.y); + t = sol.x; + + workshop.plot([numeric.transpose([t,y[0]])]) + +]]> + function initConsts() { + var STATES = new Array(); + var CONSTANTS = new Array(); + + + return {states:STATES, constants:CONSTANTS}; + } + + + + function computeRates(VOI, STATES) { + var RATES = new Array(); + + + return RATES; +} + + + // Calculate algebraic variables +function computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI) { + var ALGEBRAIC = new Array(); + + + + return ALGEBRAIC; +} + + + + + +/* + + +*/ + + + + + + + + + + + + + From 685047f83eaa7378fc56ccc655ad4f97a7ae89be Mon Sep 17 00:00:00 2001 From: Randall Britten Date: Fri, 28 Jun 2013 12:34:29 +1200 Subject: [PATCH 2/2] Copied Piecewise template from C.xml. Generated code executes, see for example: http://www.numericjs.com/workshop.php?link=b0ce9fbd7054e710cd4a0d82d129ecbe2cf4a03d66ff37eb7fd6aac80f71ea41 Generated from the HH 1952 model (at http://models.cellml.org/exposure/5d116 ) But, the ODE solver built-in to numericjs is probably not "industrial strength, certainly by comparison to the likes of CVODE. Haven't got it producing reasonable numerical results yet. Still some leftover MATLAB snippets lying around. Also, should vectorise computeAlgebraic so that algebraic variables can also be plotted. --- CeLEDS/languages/JavaScript.xml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/CeLEDS/languages/JavaScript.xml b/CeLEDS/languages/JavaScript.xml index 9131395..4489393 100644 --- a/CeLEDS/languages/JavaScript.xml +++ b/CeLEDS/languages/JavaScript.xml @@ -71,10 +71,10 @@ #expr1 * #expr2 #expr1+#expr2 xor(#expr1 , #expr2) - piecewise({#expr1, #expr2 - , #expr1, #expr2 - }, #expr1) - }, NaN) + (#expr1 ? #expr2 : + #expr1 ? #expr2 : + #expr1) + 0.0/0.0) Math.PI 0.577215664901533 Inf @@ -136,6 +136,7 @@ function computeRates(VOI, STATES) { var RATES = new Array(); + ALGEBRAIC=computeAlgebraic(CONSTANTS, STATES, VOI); return RATES; @@ -143,7 +144,7 @@ // Calculate algebraic variables -function computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI) { +function computeAlgebraic(CONSTANTS, STATES, VOI) { var ALGEBRAIC = new Array();