Skip to content

Commit

Permalink
Bokeh traj results report tests now run with reports enabled.
Browse files Browse the repository at this point in the history
Changed handling of indexing with *idxs to be compliant with older versions of python.
  • Loading branch information
robfalck committed Dec 5, 2024
1 parent e8e67b4 commit 562615d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import importlib
import os
import pathlib
import unittest
from numpy.testing import assert_almost_equal

import sys

try:
sys.modules.pop('bokeh')
except:
pass

from openmdao.utils.general_utils import set_pyoptsparse_opt, printoptions
from openmdao.utils.testing_utils import use_tempdirs, set_env_vars_context
from openmdao.utils.tests.test_hooks import hooks_active

import dymos as dm
import dymos.examples.brachistochrone.test.ex_brachistochrone_vector_states as ex_brachistochrone_vs
Expand All @@ -18,6 +27,14 @@
@use_tempdirs
class TestBrachistochroneVectorStatesExample(unittest.TestCase):

def setUp(self):
self.testflo_running = os.environ.pop('TESTFLO_RUNNING', None)

def tearDown(self):
# restore what was there before running the test
if self.testflo_running is not None:
os.environ['TESTFLO_RUNNING'] = self.testflo_running

def assert_results(self, p):
t_initial = p.get_val('traj0.phase0.timeseries.time')[0]
t_final = p.get_val('traj0.phase0.timeseries.time')[-1]
Expand Down Expand Up @@ -103,8 +120,8 @@ def test_ex_brachistochrone_vs_birkhoff(self):
self.assert_partials(p)

@unittest.skipIf(not bokeh_available, 'bokeh unavailable')
@hooks_active
def test_bokeh_plots(self):

with set_env_vars_context(OPENMDAO_REPORTS='1'):
with dm.options.temporary(plots='bokeh'):
p = ex_brachistochrone_vs.brachistochrone_min_time(transcription='radau-ps',
Expand All @@ -113,6 +130,7 @@ def test_bokeh_plots(self):
run_driver=True,
simulate=True,
make_plots=True)

self.assert_results(p)
self.assert_partials(p)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from openmdao.utils.general_utils import set_pyoptsparse_opt
from openmdao.utils.testing_utils import use_tempdirs, set_env_vars_context


import dymos as dm
from dymos.examples.finite_burn_orbit_raise.finite_burn_orbit_raise_problem import two_burn_orbit_raise_problem
from dymos.utils.testing_utils import _get_reports_dir
Expand Down
80 changes: 52 additions & 28 deletions dymos/examples/min_time_climb/test/test_ex_min_time_climb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import unittest
import numpy as np
from numpy.polynomial import Polynomial as P
Expand All @@ -15,7 +16,7 @@
import dymos as dm
from dymos.examples.min_time_climb.min_time_climb_ode import MinTimeClimbODE
from dymos.utils.misc import om_version
from openmdao.utils.testing_utils import use_tempdirs, require_pyoptsparse
from openmdao.utils.testing_utils import use_tempdirs, require_pyoptsparse, set_env_vars_context


def min_time_climb(optimizer='SLSQP', num_seg=3, transcription='gauss-lobatto',
Expand Down Expand Up @@ -293,37 +294,19 @@ def test_results_birkhoff(self):

self._test_timeseries_units(p)

@require_pyoptsparse(optimizer='IPOPT')
def test_results_gauss_lobatto_renamed_time(self):
NUM_SEG = 12
ORDER = 3
p = min_time_climb(optimizer='IPOPT', num_seg=NUM_SEG, transcription_order=ORDER,
transcription='gauss-lobatto', add_rate=True, time_name='t')

self._test_results(p, time_name='t')

self._test_wilcard_outputs(p)

self._test_timeseries_units(p)

self._test_mach_rate(p, time_name='t')

@require_pyoptsparse(optimizer='IPOPT')
def test_results_radau_renamed_time(self):
NUM_SEG = 15
ORDER = 3
p = min_time_climb(optimizer='IPOPT', num_seg=NUM_SEG, transcription_order=ORDER,
transcription='radau-ps', add_rate=True, time_name='t', force_alloc_complex=True,
make_plots=True)

self._test_results(p, time_name='t')

self._test_wilcard_outputs(p)
@use_tempdirs
class TestMinTimeClimbWithReports(TestMinTimeClimb):

self._test_timeseries_units(p)
def setUp(self):
self.testflo_running = os.environ.pop('TESTFLO_RUNNING', None)

self._test_mach_rate(p, plot=False, time_name='t')
def tearDown(self):
# restore what was there before running the test
if self.testflo_running is not None:
os.environ['TESTFLO_RUNNING'] = self.testflo_running

def _test_traj_results_report(self, p):
html_file = _get_reports_dir(p) / 'traj_results_report.html'
self.assertTrue(html_file.exists(), msg=f'{html_file} does not exist!')

Expand Down Expand Up @@ -353,6 +336,47 @@ def test_results_radau_renamed_time(self):
for label in expected_labels:
self.assertIn(label, html_data)

@require_pyoptsparse(optimizer='IPOPT')
def test_results_gauss_lobatto_renamed_time(self):
with set_env_vars_context(OPENMDAO_REPORTS='1'):
with dm.options.temporary(plots='bokeh'):
NUM_SEG = 12
ORDER = 3
p = min_time_climb(optimizer='IPOPT', num_seg=NUM_SEG, transcription_order=ORDER,
force_alloc_complex=True,
transcription='gauss-lobatto', add_rate=True, time_name='t',
make_plots=True)

self._test_results(p, time_name='t')

self._test_wilcard_outputs(p)

self._test_timeseries_units(p)

self._test_mach_rate(p, time_name='t')

self._test_traj_results_report(p)

@require_pyoptsparse(optimizer='IPOPT')
def test_results_radau_renamed_time(self):
with set_env_vars_context(OPENMDAO_REPORTS='1'):
with dm.options.temporary(plots='bokeh'):
NUM_SEG = 15
ORDER = 3
p = min_time_climb(optimizer='IPOPT', num_seg=NUM_SEG, transcription_order=ORDER,
transcription='radau-ps', add_rate=True, time_name='t',
force_alloc_complex=True, make_plots=True)

self._test_results(p, time_name='t')

self._test_wilcard_outputs(p)

self._test_timeseries_units(p)

self._test_mach_rate(p, plot=False, time_name='t')

self._test_traj_results_report(p)


if __name__ == '__main__': # pragma: no cover
unittest.main()
1 change: 0 additions & 1 deletion dymos/run_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def run_problem(problem, refine_method='hp', refine_iteration_limit=0, run_drive
sims[subsys.pathname] = sim_prob

if make_plots:

if om_version()[0] > (3, 34, 2):
outputs_dir = problem.get_outputs_dir()
if os.sep in str(solution_record_file):
Expand Down
5 changes: 2 additions & 3 deletions dymos/visualization/timeseries/bokeh_timeseries_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ def make_timeseries_report(prob, solution_record_file=None, simulation_record_fi
# but we want the y_axis label to show the indices of the columns
# being plotted as 'varname[i,j,k]'.
sources[f'{var_name}[{str_idxs}]'] = s = f'{var_name}_{str_idxs.replace(",", "_")}'
sol_data_column = sol_data[var_name][:, *idxs]
sol_data_column = sol_data[var_name][(slice(None), *idxs)]
sol_data[s] = sol_data_column
sim_data_column = sim_data[var_name][:, *idxs]
sim_data_column = sim_data[var_name][(slice(None), *idxs)]
sim_data[s] = sim_data_column
else:
sources = {var_name: var_name}
Expand Down Expand Up @@ -598,6 +598,5 @@ def make_timeseries_report(prob, solution_record_file=None, simulation_record_fi
phase_select=phase_select)))

# Save

save(report_layout, filename=report_path, title=f'trajectory results for {traj_path}',
resources=bokeh_resources.INLINE)

0 comments on commit 562615d

Please sign in to comment.