Skip to content

Commit

Permalink
Merge pull request #333 from moorepants/initial-conditions
Browse files Browse the repository at this point in the history
Added text boxes to edit the initial conditions in the GUI.
  • Loading branch information
moorepants committed Mar 9, 2016
2 parents d19a6da + 37d3e38 commit 53089b9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,15 @@ Release Notes
0.4.0
-----

- The initial conditions can now be adjusted in the notebook GUI. [PR `#333`_]
- The width of the viz canvas is now properly bounded in the notebook. [PR `#332`_]
- Planes now render both sides in the visualization GUI. [PR `#330`_]
- Adds in more type checks for System.times. [PR `#322`_]
- Added an OctaveMatrixGenerator for basic Octave/Matlab printing. [PR `#323`_]
- Simplified the right hand side evaluation code in the ODEFunctionGenerator.
Note that this change comes with some performance hits. [PR `#301`_]

.. _#333: https://github.com/pydy/pydy/pull/333
.. _#332: https://github.com/pydy/pydy/pull/332
.. _#330: https://github.com/pydy/pydy/pull/330
.. _#322: https://github.com/pydy/pydy/pull/322
Expand Down
45 changes: 38 additions & 7 deletions pydy/viz/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pkg_resources import parse_version
import numpy as np
from sympy import latex
from sympy.physics.mechanics import ReferenceFrame, Point
from sympy.physics.mechanics import ReferenceFrame, Point, dynamicsymbols

# local
from .camera import PerspectiveCamera
Expand Down Expand Up @@ -653,9 +653,12 @@ def _rerun_button_callback(self, btn):
original_scene_file = self._scene_json_file
original_sim_file = self._simulation_json_file
original_constants = self.system.constants.copy()
original_initial_conditions = self.system.initial_conditions.copy()
try:
self.system.constants = {s: w.value for s, w in
self._constants_text_widgets.items()}
self.system.initial_conditions = {s: w.value for s, w in
self._initial_conditions_text_widgets.items()}
pydy_dir = os.path.join(os.getcwd(), self.pydy_directory)
self._generate_json(directory=pydy_dir)
except:
Expand All @@ -669,7 +672,9 @@ def _rerun_button_callback(self, btn):
self._scene_json_file = original_scene_file
self._simulation_json_file = original_sim_file
self.system.constants = original_constants
self.system.initial_conditions = original_initial_conditions
self._fill_constants_widgets()
self._fill_initial_conditions_widgets()

js_tmp = 'jQuery("#json-input").val("{}/{}");'
js = js_tmp.format(self.pydy_directory, self._scene_json_file)
Expand All @@ -693,6 +698,21 @@ def _fill_constants_widgets(self):

self._constants_text_widgets[sym] = text_widget

def _fill_initial_conditions_widgets(self):

for sym in self._system.coordinates + self._system.speeds:

val = self._system.initial_conditions[sym]

sym_0 = sym.xreplace({dynamicsymbols._t: self._system.times[0]})

desc = latex(sym_0, mode='inline')

text_widget = widgets.FloatText(value=val,
description=desc)

self._initial_conditions_text_widgets[sym] = text_widget

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

Expand Down Expand Up @@ -727,18 +747,29 @@ def display_ipython(self):
# button if the scene was generated with a System.
if self._system is not None:

# Construct a container that holds all of the constants input
# text widgets.
self._initial_conditions_container = widgets.Box()
self._initial_conditions_container.padding = "10px"
self._initial_conditions_text_widgets = OrderedDict()
self._fill_initial_conditions_widgets()
title = widgets.HTML('<h4 style="margin-left: 5ex;">Initial Conditions</h4>')
self._initial_conditions_container.children = ((title, ) +
tuple(v for v in self._initial_conditions_text_widgets.values()))

self._constants_container = widgets.Box()
self._constants_container.padding = "20px"
self._constants_text_widgets = OrderedDict()
self._fill_constants_widgets()
# Add all of the constants widgets to the container.
self._constants_container.children = \
tuple(v for v in self._constants_text_widgets.values())
title = widgets.HTML('<h4 style="margin-left: 5ex;">Constants</h4>')
self._constants_container.children = ((title, ) +
tuple(v for v in self._constants_text_widgets.values()))

self._initialize_rerun_button()

display(self._constants_container)
parameter_input_container = \
widgets.HBox(children=(self._initial_conditions_container,
self._constants_container))

display(parameter_input_container)
display(self._rerun_button)

ipython_static_url = os.path.relpath(self.pydy_directory, os.getcwd())
Expand Down

0 comments on commit 53089b9

Please sign in to comment.