Skip to content

Commit

Permalink
Create text widgets for System time parameters
Browse files Browse the repository at this point in the history
If a System object is used, create text widgets for start time, end
time, and time step. This only allows creation of an evenly spaced time
array and changes to these widgets will create a new time array for
simulation.

This commit resolves pydy#206.
  • Loading branch information
oliverlee committed Oct 22, 2015
1 parent 829d9a4 commit 6ff4f19
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
23 changes: 18 additions & 5 deletions pydy/viz/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,20 @@ def _generate_json(self, directory=None, prefix=None):

if times is None:
tf = (num_time_steps - 1)/self.frames_per_second
self._scene_info["timeDelta"] = 1.0 / self.frames_per_second
self._scene_info["timeStep"] = 1.0 / self.frames_per_second
self._scene_info["startTime"] = 0.0
self._scene_info["endTime"] = tf
else:
# Assume that times is evenly spaced and monotonic.
# TODO: Interpolate if times are not evenly spaced.
total_time = times[-1] - times[0]
self._scene_info["timeDelta"] = total_time / (num_time_steps - 1)
self._scene_info["timeStep"] = total_time / (num_time_steps - 1)
self._scene_info["startTime"] = times[0]
self._scene_info["endTime"] = times[-1]

self._scene_info["fps"] = self.frames_per_second
self._scene_info["speedup"] = (self.frames_per_second *
self._scene_info["timeDelta"])
self._scene_info["timeStep"])
self._scene_info["timeSteps"] = num_time_steps
self._scene_info["constant_map"] = constant_map_for_json

Expand Down Expand Up @@ -652,8 +652,12 @@ def _rerun_button_callback(self, btn):
original_sim_file = self._simulation_json_file
original_constants = self.system.constants.copy()
try:
self.system.constants = {s: w.value for s, w in
self._constants_text_widgets.items()}
d = {s: w.value for s, w in self._constants_text_widgets.items()}
t0 = d.pop('start time')
tf = d.pop('end time')
dt = d.pop('time step')
self.system.constants = d
self.system.times = np.arange(t0, tf, dt)
pydy_dir = os.path.join(os.getcwd(), self.pydy_directory)
self._generate_json(directory=pydy_dir)
except:
Expand Down Expand Up @@ -691,6 +695,15 @@ def _fill_constants_widgets(self):

self._constants_text_widgets[sym] = text_widget

# add in time parameters
for k in ['start time', 'time step', 'end time']:
# TODO: don't set time values both here and in _scene_info
k1, k2 = k.split()
key = '{}{}'.format(k1, k2.capitalize())
w = widgets.FloatText(value=self._scene_info[key],
description=k)
self._constants_text_widgets[k] = w

def _initialize_rerun_button(self):
"""Construct a button for controlling rerunning the simulations."""

Expand Down
2 changes: 1 addition & 1 deletion pydy/viz/static/js/dyviz/dv.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ DynamicsVisualizer = Class.create({
var div = jQuery("#simulation-params").fadeOut();
div.html(" "); // clear html first

for(var i in constants){
for (var i in constants) {
div.append('<span class="input-group-addon">' + i + '</span>');
div.append(jQuery('<input />',{ type:'text', id: i, class: 'form-control', value: constants[i]}));
}
Expand Down
4 changes: 2 additions & 2 deletions pydy/viz/static/js/dyviz/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ DynamicsVisualizer.Scene = Object.extend(DynamicsVisualizer, {

var t0 = self.model.startTime;
var tf = self.model.endTime;
var dt = self.model.timeDelta;
var dt = self.model.timeStep;

if(!self.animationPaused){
self.currentTime = t0;
Expand Down Expand Up @@ -412,7 +412,7 @@ DynamicsVisualizer.Scene = Object.extend(DynamicsVisualizer, {
var tf = self.model.endTime;
var percent = (100*(currentTime - t0)/(tf - t0)).toFixed(3);

var time_index = Math.round((currentTime - t0)/self.model.timeDelta);
var time_index = Math.round((currentTime - t0)/self.model.timeStep);
var _children = self._scene.children;
for(var i=0;i<_children.length;i++){
if(!(_children[i] instanceof (THREE.OrthoGraphicCamera || THREE.PerspectiveCamera))){
Expand Down

0 comments on commit 6ff4f19

Please sign in to comment.