More work!

This commit is contained in:
Shoofle 2013-09-05 16:01:58 -04:00
parent 402eef28a0
commit 08f5aa8860
2 changed files with 129 additions and 80 deletions

View File

@ -10,9 +10,9 @@
<script type="text/javascript">
function heyo(sq) {
if (sq.temperature.value < sq.ignition_threshhold.value) {
return -0.5*sq.temperature.value;
return -sq.temperature_decay.value*sq.temperature.value;
}
return sq.temperature.value*sq.fuel_flow_rate.value - 0.5*sq.temperature.value;
return sq.temperature.value*sq.fuel_flow_rate.value - sq.temperature_decay.value*sq.temperature.value;
}
var e1=engine($('#fuel-management-1'), heyo);
</script>
@ -39,9 +39,9 @@
<script type="text/javascript">
function heyo2(sq) {
if (sq.temperature.value < sq.ignition_threshhold.value) {
return -0.5;
return -sq.temperature_decay.value;
}
return sq.temperature.value*sq.fuel_flow_rate.value - 0.5;
return sq.temperature.value*sq.fuel_flow_rate.value - sq.temperature_decay.value;
}
var e2=engine($('#fuel-management-2'), heyo2);
</script>

View File

@ -1,114 +1,163 @@
var a=true;
function engine(element, reaction_rate_function) {
var container = $(element);
var e = {}; // the engine object!
e.container = $(element);
var q = {};
e.quantities = q;
q.temperature = {
"update": function () {
var old_value = this.value;
this.value += reaction_rate_function(q)*timestep/1000
if (this.value < 0) { this.value = 0; }
if (old_value != this.value) { return true; }
},
"out": function () {
container.find('.temp-out').html(this.value.toFixed(2));
if (this.value > damage_threshhold) { container.find('.damage').show();}
else { container.find('.damage').hide(); }
if (this.value < q.ignition_threshhold.value) { container.find('.noignition').show(); }
else { container.find('.noignition').hide(); }
e.container.find('.temp-out').html(this.value.toFixed(2));
if (this.value > q.damage_threshhold.value) { e.container.find('.damage').show();}
else { e.container.find('.damage').hide(); }
if (this.value < q.ignition_threshhold.value) { e.container.find('.noignition').show(); }
else { e.container.find('.noignition').hide(); }
for (var i=0; i<this.graph_data.data.length; i++) {
this.graph_data.data[i][0] = this.value;
}
this.graph_data.data[1][1] = get_current_potential(this.value);
},
"value": 0,
"graph_data": {
data: [[0,10],[0,0],[0,-10]],
lines: {show: true, lineWidth: 1},
points: {show: true, radius: 3},
color: 'red',
},
};
function get_current_potential(t) {
var data_series = all_graph_data[0].data;
var number_of_points = data_series.length;
var width = e.graph_config.xaxis.max - e.graph_config.xaxis.min;
var index = Math.floor((t / width) * number_of_points);
var below = data_series[index], above = data_series[index+1];
if (!below) { return 0; }
if (!above) { return null; }
//console.log("got current potential");
return below[1] + ((t - below[0])/(above[0] - below[0]))*(above[1] - below[1]);
}
q.fuel_flow_rate = {
"update": function () { this.value = parseFloat(container.find('[name=fuel]').val()); },
"out": function () { update_graph(); },
"update": function () {
var old_value = this.value;
this.value = parseFloat(e.container.find('[name=fuel]').val());
if (old_value != this.value) {
return true;
}
},
"out": function () { generate_graph_points(); q.temperature.out(); },
"value": 0,
};
q.ignition_threshhold = {
"update": function () {},
"out": function() {},
"out": function() {
this.graph_data.data = [[this.value, 10], [this.value, -10], [-this.value, -10], [-this.value, 10], [this.value, 10]];
},
"value": 2,
"graph_data": {
data: [],
lines: {show: true, lineWidth: 0, fill: 0.2},
points: {show: false},
color: 'blue',
},
};
q.damage_threshhold = {
"update": function () {},
"out": function() {
this.graph_data.data = [[this.value, -10], [this.value, 10], [20*this.value, 10], [20*this.value, -10], [this.value, -10]];
},
"value": 10,
"graph_data": {
data: [],
lines: {show: true, lineWidth: 0, fill: 0.2},
points: {show: false},
color: 'yellow',
},
};
q.ignition_boost = {
"update": function () {},
"out": function() {},
"value": 5,
}
var damage_threshhold = 10;
var temperature_decay = 0.5;
var timestep=10;
var graph, graph_plot, graph_config = {
xaxis: { min:0, max:15, tickSize: 3 },
yaxis: { min:-4, max:4, show: false },
series: {
lines: {
lineWidth: 4,
},
points: {
radius: 0.1,
}
},
colors: ['red'],
};
function update_graph() {
var step_size = 0.5;
var ghost_count = 1, ghost_separation = 0.03;
q.temperature_decay = {
"update": function () {},
"out": function () {},
"value": 0.5,
};
var ghost_count = 2, ghost_separation = 0.05;
var ghost_data_points = Array(1 + ghost_count*2);
var all_graph_data = [{"data": [], lines: {show: true}, points: {show: false}}];
for (var s=1; s<=ghost_count; s++) {
all_graph_data.push({"data": [], lines: {show: true, lineWidth: 0.5}, points: {show: true}});
all_graph_data.push({"data": [], lines: {show: true, lineWidth: 0.5}, points: {show: true}});
}
all_graph_data.push(q.temperature.graph_data);
all_graph_data.push(q.ignition_threshhold.graph_data);
all_graph_data.push(q.damage_threshhold.graph_data);
e.graph_config = {
xaxis: { min:0, max:15, tickSize: 3 },
yaxis: { min:-8, max:8, show: false },
series: { lines: { lineWidth: 4, }, points: { radius: 0.1, } },
colors: ['gray'],
};
function generate_graph_points() {
var step_size = 0.4;
var flow_value = q.fuel_flow_rate.value;
var the_current_potential=0, delta;
var d = [{"data": [], lines: {show: true}, points: {show: false}}];
var points = [$.extend({}, q, {"fuel_flow_rate": {"value": flow_value}, "temperature": {"value": 0}})];
ghost_data_points[0] = $.extend({}, q, {"fuel_flow_rate": {"value": flow_value}, "temperature": {"value": 0}});
var potentials = [0];
for (var s=1; s <= ghost_count; s++) {
d.push( {"data": [], lines: {show: false}, points: {show: true}});
var p1 = $.extend({}, q);
$.extend(p1, {"fuel_flow_rate": {"value": flow_value + ghost_separation * s}, "temperature": {"value": 0}});
points.push(p1);
potentials.push(0)
d.push( {"data": [], lines: {show: false}, points: {show: true}});
var p2 = $.extend({}, q);
$.extend(p2, {"fuel_flow_rate": {"value": flow_value - ghost_separation * s}, "temperature": {"value": 0}});
points.push(p2);
potentials.push(0)
}
for (var i=0; i<15; i+=step_size) {
for (var s=0; s<potentials.length; s++) {
d[s].data.push([i, potentials[s]]);
points[s].temperature.value = i;
delta = -1 * reaction_rate_function(points[s]);
if (s == 0 && (i <= q.temperature.value) && (q.temperature.value < i+step_size)) {
the_current_potential = potentials[0] + delta * (q.temperature.value - i);
}
potentials[s] += delta * step_size;
}
}
d.push({
data: [[q.temperature.value, -10], [q.temperature.value, the_current_potential], [q.temperature.value, 10]],
lines: {show: true, lineWidth: 1},
points: {show: true, radius: 3},
color: 'blue',
ghost_data_points[s] = $.extend({}, q);
$.extend(ghost_data_points[s], {
"fuel_flow_rate": {"value": flow_value + ghost_separation * s},
"temperature": {"value": 0}
});
if (a) { console.log(points); a=false; }
graph_plot.setData(d);
graph_plot.draw();
potentials.push(0);
ghost_data_points[s+ghost_count] = $.extend({}, q);
$.extend(ghost_data_points[s+ghost_count], {
"fuel_flow_rate": {"value": flow_value - ghost_separation * s},
"temperature": {"value": 0}
});
potentials.push(0);
}
for (var s=0; s<potentials.length; s++) { all_graph_data[s].data = []; }
for (var i=e.graph_config.xaxis.min; i<e.graph_config.xaxis.max; i+=step_size) {
for (var s=0; s<potentials.length; s++) {
all_graph_data[s].data.push([i, potentials[s]]);
ghost_data_points[s].temperature.value = i;
potentials[s] += -1 * step_size * reaction_rate_function(ghost_data_points[s]);
}
}
//console.log("regenerated graph");
}
function update_graph() {
e.graph_plot.setData(all_graph_data);
e.graph_plot.draw();
if (a) { console.log(all_graph_data); a=false; }
}
var a, b;
container.ready(function () {
container.find('[name=ignition]').on('click', function() { q.temperature.value += q.ignition_boost.value; });
a = setInterval(jQuery.each, timestep, q, function(name, x) { x.update(); });
b = setInterval(jQuery.each, timestep, q, function(name, x) { x.out(); });
graph = container.find('.potential_plot');
graph_plot = $.plot(graph, [[]], graph_config);
var timestep=10;
e.container.ready(function () {
e.graph = e.container.find('.potential_plot');
e.graph_plot = $.plot(e.graph, [[]], e.graph_config);
jQuery.each(q, function(name, x) { x.update(); });
jQuery.each(q, function(name, x) { x.out(); });
jQuery.each(q, function(name, x) { x.update(); });
jQuery.each(q, function(name, x) { x.out(); });
e.container.find('[name=ignition]').on('click', function() { q.temperature.value += q.ignition_boost.value; });
a = setInterval(jQuery.each, timestep, q, function(name, x) { if (x.update()) { x.out(); } });
b = setInterval(update_graph, timestep);
});
return {
'container': container,
'quantities': q,
'graph_container': graph,
'graph_plot': graph_plot,
'poke': function () { this.graph_container = graph; this.graph_plot = graph_plot; },
};
return e;
}