Team:Groningen/Modelling/Model.js

function formatNumberToHTML(v,p) { if (p===undefined) p = 5; return v.toPrecision(p) .replace(/e\+([0-9]+)$/i,'&middot;10$1') .replace(/e\-([0-9]+)$/i,'&middot;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; }

/* function simulate(x0,maxt,f) { if (!maxt.length) maxt = [maxt]; maxt.sort(function(a,b){return a - b;}); var t = 0; var h = maxt[maxt.length-1], nt, mti = 0; var data = {time: [0], timeKey: []}, x = {}, k1, nx, nk1, e, cont; for(var v in x0) { x[v] = x0[v]; data[v] = [x0[v]]; } k1 = f(t,x); for(var mti=0; mti(Math.abs(x[v])+Math.abs(nx[v]))*1e-3) { h /= 2; nt = t + h;         cont = true; break; }     }      if (!cont) { var nnt = Math.min(nt + 1.1*h,maxt[mti]); h = nnt-nt; t = nt; nt = nnt; x = nx; k1 = nk1; for(var v in x) { data[v].push(x[v]); }       data.time.push(t); }   } while(cont || t<maxt[mti]); data.timeKey.push(data.time.length-1); } return data; }*/