Groningen2009/Model.js

Revision as of 07:47, 19 October 2009 by Jaspervdg (Talk | contribs) (New page: function formatNumberToHTML(v,p) { if (p===undefined) p = 5; return v.toPrecision(p) .replace(/e\+([0-9]+)$/i,'·10<sup>$1</sup>') .replace(/e\-([0-9]+)$/i,'&...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

function formatNumberToHTML(v,p) {

 if (p===undefined) p = 5;
 return v.toPrecision(p)
         .replace(/e\+([0-9]+)$/i,'·10$1')
         .replace(/e\-([0-9]+)$/i,'·10-$1');

}

function setOutput(name,v,p) {

 var node = document.getElementById(name);
 if (!node) return;
 node.innerHTML = formatNumberToHTML(v);
 node.value = v;

}

function getInput(name) {

 var node = document.getElementById(name);
 if (!node) return undefined;
 return Number(node.value);

}

function simulate(x0,maxt,f) {

 if (!maxt.length) maxt = [maxt];
 maxt.sort(function(a,b){return a - b;});
 var t = 0;
 var oh = Math.min(1,maxt[0]/2), h, nt, mti = 0;
 var data = {time: [0], timeKey: []}, x = {}, k1 = {};
 for(var v in x0) {
   if (v.charAt(0)=='_') {
     x[v] = x0[v];
     data[v] = [];
   } else {
     x[v] = x0[v];
     data[v] = [x0[v]];
   }
 }
 do {
   if (t+oh>=maxt[mti]) {
     h = maxt[mti]-t;
     nt = maxt[mti];
   } else {
     h = oh;
     nt = t + h;
   }
   k1 = f(t,x);
   for(var v in x) {
     if (v.charAt(0)=='_') {
       x[v] = k1[v];
     } else {
       x[v] += h*k1[v];
     }
     data[v].push(x[v]);
   }
   t = nt;
   data.time.push(t);
   if (t>=maxt[mti]) {
     data.timeKey.push(data.time.length-1);
     mti++;
   }
 } while(t<maxt[maxt.length-1]);
 return data;

}