Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed time series plots directory to be under the problem reports directory. #784

Merged
merged 19 commits into from
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions benchmark/benchmark_balanced_field.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@
from dymos.examples.balanced_field.balanced_field_ode import BalancedFieldODEComp


def _run_balanced_field_length_problem(tx=dm.GaussLobatto, timeseries=True, sim=True, solvesegs=False):
def _run_balanced_field_length_problem(tx=dm.GaussLobatto, timeseries=True, sim=True, solvesegs=False,
make_plots=False):
p = om.Problem()

_, optimizer = set_pyoptsparse_opt('IPOPT', fallback=True)
@@ -238,7 +239,7 @@ def _run_balanced_field_length_problem(tx=dm.GaussLobatto, timeseries=True, sim=
p.set_val('traj.climb.states:gam', climb.interp('gam', [0, 5.0]), units='deg')
p.set_val('traj.climb.controls:alpha', 5.0, units='deg')

dm.run_problem(p, run_driver=True, simulate=sim)
dm.run_problem(p, run_driver=True, simulate=sim, make_plots=make_plots)

# Test this example in Dymos' continuous integration
assert_near_equal(p.get_val('traj.rto.states:r')[-1], 2188.2, tolerance=0.01)
@@ -267,3 +268,6 @@ def benchmark_radau_timeseries_nosim_nosolveseg(self):
def benchmark_radau_notimeseries_nosim_solveseg(self):
_run_balanced_field_length_problem(tx=dm.Radau, timeseries=False, sim=False,
solvesegs='forward')

if __name__ == '__main__':
_run_balanced_field_length_problem(make_plots=True)
6 changes: 3 additions & 3 deletions benchmark/benchmark_racecar.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
from dymos.examples.racecar.tracks import ovaltrack # track curvature imports


def _run_racecar_problem(transcription, timeseries=False):
def _run_racecar_problem(transcription, timeseries=False, make_plots=False):
# change track here and in curvature.py. Tracks are defined in tracks.py
track = ovaltrack

@@ -161,7 +161,7 @@ def _run_racecar_problem(transcription, timeseries=False):
p.set_val('traj.phase0.controls:thrust', phase.interp('thrust', [0.1, 0.1]), units=None)
# a small amount of thrust can speed up convergence

dm.run_problem(p, run_driver=True, simulate=False, make_plots=False)
dm.run_problem(p, run_driver=True, simulate=False, make_plots=make_plots)
print('Optimization finished')

t = p.get_val('traj.phase0.timeseries.states:t')
@@ -187,4 +187,4 @@ def benchmark_radau_timeseries(self):


if __name__ == '__main__':
_run_racecar_problem(dm.GaussLobatto, timeseries=False)
_run_racecar_problem(dm.GaussLobatto, timeseries=False, make_plots=True)
2 changes: 1 addition & 1 deletion docs/dymos_book/_config.yml
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ execute:
execute_notebooks: force
stderr_output: show
allow_errors: false
timeout: 240
timeout: 360

# Define the name of the latex output file for PDF builds
latex:
Binary file modified docs/dymos_book/examples/brachistochrone/brachistochrone_fbd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -2,18 +2,21 @@
import os
import shutil
import unittest
import pathlib

import numpy as np

import openmdao.api as om
from openmdao.utils.testing_utils import require_pyoptsparse
from openmdao.utils.general_utils import set_pyoptsparse_opt
_, optimizer = set_pyoptsparse_opt('IPOPT', fallback=True)
from openmdao.utils.testing_utils import use_tempdirs


import dymos as dm
from dymos.examples.finite_burn_orbit_raise.finite_burn_eom import FiniteBurnODE
from openmdao.utils.testing_utils import use_tempdirs
from dymos.utils.testing_utils import _get_reports_dir

_, optimizer = set_pyoptsparse_opt('IPOPT', fallback=True)
bokeh_available = importlib.util.find_spec('bokeh') is not None


@@ -200,16 +203,17 @@ def tearDown(self):
def test_bokeh_plots(self):
dm.options['plots'] = 'bokeh'

two_burn_orbit_raise_problem(transcription='gauss-lobatto', transcription_order=3,
compressed=False, optimizer='SLSQP', show_output=False)
p = two_burn_orbit_raise_problem(transcription='gauss-lobatto', transcription_order=3,
compressed=False, optimizer='SLSQP', show_output=False)

self.assertSetEqual({'plots.html'}, set(os.listdir('plots')))
plot_dir = pathlib.Path(_get_reports_dir(p)).joinpath('plots')
self.assertSetEqual({'plots.html'}, set(os.listdir(plot_dir)))

def test_mpl_plots(self):
dm.options['plots'] = 'matplotlib'

two_burn_orbit_raise_problem(transcription='gauss-lobatto', transcription_order=3,
compressed=False, optimizer='SLSQP', show_output=False)
p = two_burn_orbit_raise_problem(transcription='gauss-lobatto', transcription_order=3,
compressed=False, optimizer='SLSQP', show_output=False)

expected_files = {'control_rates_u1_rate.png', 'state_rates_r.png', 'states_deltav.png',
'states_r.png', 'state_rates_accel.png', 'state_rates_deltav.png',
@@ -218,7 +222,9 @@ def test_mpl_plots(self):
'control_rates_u1_rate2.png', 'state_rates_vt.png', 'time_phase.png',
'parameters_c.png', 'state_rates_theta.png', 'state_rates_vr.png', 'dt_dstau.png'}

self.assertSetEqual(expected_files, set(os.listdir('plots')))
html_files = {str(pathlib.Path(f).with_suffix('.html')) for f in expected_files}
plot_dir = pathlib.Path(_get_reports_dir(p)).joinpath('plots')
self.assertSetEqual(expected_files.union(html_files), set(os.listdir(plot_dir)))


if __name__ == '__main__': # pragma: no cover
2 changes: 2 additions & 0 deletions dymos/grid_refinement/test/test_grid_refinement.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

import numpy as np
import openmdao.api as om
from openmdao.utils.testing_utils import use_tempdirs
import dymos as dm


@@ -34,6 +35,7 @@ def compute_partials(self, inputs, partials):
partials['J_dot', 'u'] = inputs['u']


@use_tempdirs
class TestGridRefinement(unittest.TestCase):

def test_refine_hp_non_ode_rate_sources(self):
4 changes: 2 additions & 2 deletions dymos/run_problem.py
Original file line number Diff line number Diff line change
@@ -95,9 +95,9 @@ def run_problem(problem, refine_method='hp', refine_iteration_limit=0, run_drive
if make_plots:
if simulate:
timeseries_plots(solution_record_file, simulation_record_file=simulation_record_file,
plot_dir=plot_dir)
plot_dir=plot_dir, problem=problem)
else:
timeseries_plots(solution_record_file, simulation_record_file=None,
plot_dir=plot_dir)
plot_dir=plot_dir, problem=problem)

return failed
23 changes: 15 additions & 8 deletions dymos/test/test_run_problem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import print_function, division, absolute_import
import os
import unittest
import pathlib

import numpy as np
from numpy.testing import assert_almost_equal
@@ -9,12 +10,14 @@
import openmdao.api as om
from openmdao.utils.assert_utils import assert_near_equal
from openmdao.utils.testing_utils import use_tempdirs, require_pyoptsparse
from openmdao.utils.general_utils import set_pyoptsparse_opt

import dymos as dm
from dymos.examples.hyper_sensitive.hyper_sensitive_ode import HyperSensitiveODE
from dymos.examples.brachistochrone.brachistochrone_ode import BrachistochroneODE
from dymos.examples.brachistochrone.brachistochrone_vector_states_ode import BrachistochroneVectorStatesODE
from openmdao.utils.general_utils import set_pyoptsparse_opt
from dymos.utils.testing_utils import _get_reports_dir

_, optimizer = set_pyoptsparse_opt('IPOPT', fallback=True)

om_version = tuple([int(s) for s in openmdao.__version__.split('-')[0].split('.')])
@@ -627,31 +630,33 @@ def setUp(self):

def test_run_brachistochrone_problem_make_plots(self):
dm.run_problem(self.p, make_plots=True)
plot_dir = pathlib.Path(_get_reports_dir(self.p)).joinpath('plots')

for varname in ['time_phase', 'states:x', 'state_rates:x', 'states:y',
'state_rates:y', 'states:v',
'state_rates:v', 'controls:theta', 'control_rates:theta_rate',
'control_rates:theta_rate2', 'parameters:g']:
self.assertTrue(os.path.exists(f'plots/{varname.replace(":","_")}.png'))
self.assertTrue(plot_dir.joinpath(f'{varname.replace(":","_")}.png').exists())

def test_run_brachistochrone_problem_make_plots_set_plot_dir(self):
plot_dir = "test_plot_dir"
dm.run_problem(self.p, make_plots=True, plot_dir=plot_dir)
dm.run_problem(self.p, make_plots=True, plot_dir="test_plot_dir")

plot_dir = pathlib.Path(_get_reports_dir(self.p))
for varname in ['time_phase', 'states:x', 'state_rates:x', 'states:y',
'state_rates:y', 'states:v',
'state_rates:v', 'controls:theta', 'control_rates:theta_rate',
'control_rates:theta_rate2', 'parameters:g']:
self.assertTrue(os.path.exists(f'test_plot_dir/{varname.replace(":","_")}.png'))
self.assertTrue(plot_dir.joinpath('test_plot_dir', f'{varname.replace(":","_")}.png').exists())

def test_run_brachistochrone_problem_do_not_make_plots(self):
dm.run_problem(self.p, make_plots=False)
plot_dir = pathlib.Path(_get_reports_dir(self.p)).joinpath('plots')

for varname in ['time_phase', 'states:x', 'state_rates:x', 'states:y',
'state_rates:y', 'states:v',
'state_rates:v', 'controls:theta', 'control_rates:theta_rate',
'control_rates:theta_rate2', 'parameters:g']:
self.assertFalse(os.path.exists(f'plots/{varname.replace(":","_")}.png'))
self.assertFalse(plot_dir.joinpath(f'{varname.replace(":","_")}.png').exists())

def test_run_brachistochrone_problem_set_simulation_record_file(self):
simulation_record_file = 'simulation_record_file.db'
@@ -667,21 +672,23 @@ def test_run_brachistochrone_problem_set_solution_record_file(self):

def test_run_brachistochrone_problem_plot_simulation(self):
dm.run_problem(self.p, make_plots=True, simulate=True)
plot_dir = pathlib.Path(_get_reports_dir(self.p)).joinpath('plots')

for varname in ['time_phase', 'states:x', 'state_rates:x', 'states:y',
'state_rates:y', 'states:v',
'state_rates:v', 'controls:theta', 'control_rates:theta_rate',
'control_rates:theta_rate2', 'parameters:g']:
self.assertTrue(os.path.exists(f'plots/{varname.replace(":","_")}.png'))
self.assertTrue(plot_dir.joinpath(f'{varname.replace(":","_")}.png').exists())

def test_run_brachistochrone_problem_plot_no_simulation_record_file_given(self):
dm.run_problem(self.p, make_plots=True, simulate=True)
plot_dir = pathlib.Path(_get_reports_dir(self.p)).joinpath('plots')

for varname in ['time_phase', 'states:x', 'state_rates:x', 'states:y',
'state_rates:y', 'states:v',
'state_rates:v', 'controls:theta', 'control_rates:theta_rate',
'control_rates:theta_rate2', 'parameters:g']:
self.assertTrue(os.path.exists(f'plots/{varname.replace(":","_")}.png'))
self.assertTrue(plot_dir.joinpath(f'{varname.replace(":","_")}.png').exists())


@use_tempdirs
14 changes: 14 additions & 0 deletions dymos/utils/testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import io
import pathlib
from packaging.version import Version

import numpy as np

from scipy.interpolate import interp1d

import openmdao.api as om
import openmdao.utils.assert_utils as _om_assert_utils
from openmdao import __version__ as openmdao_version


def assert_check_partials(data, atol=1.0E-6, rtol=1.0E-6):
@@ -193,3 +196,14 @@ def assert_timeseries_near_equal(t1, x1, t2, x2, tolerance=1.0E-6):
y_interp = np.reshape(interp(t_check), newshape=(num_points,) + shape1)

_om_assert_utils.assert_near_equal(y_interp, x_check, tolerance=tolerance)


def _get_reports_dir(prob):
# need this to work with older OM versions with old reports system API
# reports API changed between 3.18 and 3.19, so handle it here in order to be able to test against older
# versions of openmdao
if Version(openmdao_version) > Version("3.18"):
return prob.get_reports_dir()

from openmdao.utils.reports_system import get_reports_dir
return get_reports_dir(prob)
4 changes: 2 additions & 2 deletions dymos/visualization/linkage/report.py
Original file line number Diff line number Diff line change
@@ -39,10 +39,10 @@ def create_linkage_report(traj, output_file: str = 'linkage_report.html',

import openmdao
openmdao_dir = os.path.dirname(inspect.getfile(openmdao))
vis_dir = os.path.join(openmdao_dir, "visualization/n2_viewer")
vis_dir = os.path.join(openmdao_dir, "visualization", "n2_viewer")

dymos_dir = os.path.dirname(inspect.getfile(dm))
reports_dir = os.path.join(dymos_dir, "visualization/linkage")
reports_dir = os.path.join(dymos_dir, "visualization", "linkage")

HtmlPreprocessor(os.path.join(reports_dir, "report_template.html"), output_file,
search_path=[vis_dir, reports_dir], allow_overwrite=True, var_dict=html_vars,
5 changes: 5 additions & 0 deletions dymos/visualization/linkage/test/test_linkage_report_ui.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Test Dymos Linkage Reports GUI using Playwright."""
import asyncio
from packaging.version import Version
from playwright.async_api import async_playwright
from aiounittest import async_test
import unittest

from openmdao.utils.gui_testing_utils import _GuiTestCase
import sys

import openmdao.api as om
import openmdao
from openmdao.utils.testing_utils import use_tempdirs
from openmdao.utils.assert_utils import assert_near_equal

@@ -18,6 +21,7 @@
# Windows specific event-loop policy & cmd
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())


my_loop = asyncio.get_event_loop()

""" A set of toolbar tests that runs on each model. """
@@ -242,6 +246,7 @@
current_test = 1


@unittest.skipUnless(Version(openmdao.__version__) >= Version("3.19"), "reports API is too old")
@use_tempdirs
class dymos_linkage_gui_test_case(_GuiTestCase):
def setUp(self):
Loading