Skip to content

Commit

Permalink
update assert_rel_error to assert_near_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
wright committed Apr 27, 2020
1 parent 8132a60 commit 298043d
Show file tree
Hide file tree
Showing 59 changed files with 618 additions and 676 deletions.
22 changes: 11 additions & 11 deletions benchmark/benchmark_brachistochrone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal
from openmdao.utils.general_utils import set_pyoptsparse_opt
import dymos as dm
from dymos.examples.brachistochrone import BrachistochroneODE
Expand Down Expand Up @@ -93,18 +93,18 @@ def run_asserts(self, p):

thetaf = p.get_val('phase0.timeseries.controls:theta')[-1]

assert_rel_error(self, t_initial, 0.0)
assert_rel_error(self, x0, 0.0)
assert_rel_error(self, y0, 10.0)
assert_rel_error(self, v0, 0.0)
assert_near_equal(t_initial, 0.0)
assert_near_equal(x0, 0.0)
assert_near_equal(y0, 10.0)
assert_near_equal(v0, 0.0)

assert_rel_error(self, tf, 1.8016, tolerance=0.0001)
assert_rel_error(self, xf, 10.0, tolerance=0.001)
assert_rel_error(self, yf, 5.0, tolerance=0.001)
assert_rel_error(self, vf, 9.902, tolerance=0.001)
assert_rel_error(self, g, 9.80665, tolerance=0.001)
assert_near_equal(tf, 1.8016, tolerance=0.0001)
assert_near_equal(xf, 10.0, tolerance=0.001)
assert_near_equal(yf, 5.0, tolerance=0.001)
assert_near_equal(vf, 9.902, tolerance=0.001)
assert_near_equal(g, 9.80665, tolerance=0.001)

assert_rel_error(self, thetaf, 100.12, tolerance=1)
assert_near_equal(thetaf, 100.12, tolerance=1)

def benchmark_radau_30_3_color_simul_compressed_snopt(self):
p = brachistochrone_min_time(transcription='radau-ps',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from numpy import array

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

from dymos.examples.aircraft_steady_flight.aero.aero_coef_comp import AeroCoefComp

Expand Down Expand Up @@ -178,5 +178,5 @@ def test_aero_coefs(self):

prob.run_model()

assert_rel_error(self, prob['aero.CL'], CL_test, tolerance=0.005)
assert_rel_error(self, prob['aero.CD'], CD_test, tolerance=0.005)
assert_near_equal(prob['aero.CL'], CL_test, tolerance=0.005)
assert_near_equal(prob['aero.CD'], CD_test, tolerance=0.005)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

from dymos.examples.aircraft_steady_flight.aero.mbi_aero_coef_comp import setup_surrogates_all, \
MBIAeroCoeffComp
Expand Down Expand Up @@ -204,15 +204,13 @@ def test_aero_coefs(self):
0.021332763445, 0.020777569791, 0.020306416459, 0.019901757560,
0.019550206447, 0.019241644947, 0.018968388593, 0.018724538140])

assert_rel_error(self,
prob['aero.CL'],
CL_data,
tolerance=0.003)
assert_near_equal(prob['aero.CL'],
CL_data,
tolerance=0.003)

assert_rel_error(self,
prob['aero.CD'],
CD_data,
tolerance=0.003)
assert_near_equal(prob['aero.CD'],
CD_data,
tolerance=0.003)


if __name__ == '__main__': # pragma: no cover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_steady_aircraft_for_docs(self):
import matplotlib.pyplot as plt

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

import dymos as dm

Expand Down Expand Up @@ -106,8 +106,8 @@ def test_steady_aircraft_for_docs(self):

dm.run_problem(p)

assert_rel_error(self, p.get_val('traj.phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)
assert_near_equal(p.get_val('traj.phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)

exp_out = traj.simulate()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error, assert_check_partials
from openmdao.utils.assert_utils import assert_near_equal, assert_check_partials

from dymos.examples.aircraft_steady_flight.flight_equlibrium.steady_flight_equilibrium_group \
import SteadyFlightEquilibriumGroup
Expand Down Expand Up @@ -62,8 +62,8 @@ def test_results(self):
CL = self.p['aero.CL']
CM = self.p['aero.CM']

assert_rel_error(self, CL_eq, CL, tolerance=1.0E-12)
assert_rel_error(self, CM, np.zeros_like(CM), tolerance=1.0E-12)
assert_near_equal(CL_eq, CL, tolerance=1.0E-12)
assert_near_equal(CM, np.zeros_like(CM), tolerance=1.0E-12)

def test_partials(self):
cpd = self.p.check_partials(out_stream=None, method='fd', step=1.0E-6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error, assert_check_partials
from openmdao.utils.assert_utils import assert_near_equal, assert_check_partials

from dymos.examples.aircraft_steady_flight.propulsion.propulsion_group import PropulsionGroup

Expand Down Expand Up @@ -36,9 +36,8 @@ def setUpClass(cls):

def test_results(self):

assert_rel_error(self,
self.p['propulsion.tsfc'],
2 * 8.951e-6 * 9.80665 * np.ones(self.n))
assert_near_equal(self.p['propulsion.tsfc'],
2 * 8.951e-6 * 9.80665 * np.ones(self.n))

def test_partials(self):
cpd = self.p.check_partials(method='cs', out_stream=None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

import dymos as dm
from dymos.examples.aircraft_steady_flight.aircraft_ode import AircraftODE
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_cruise_results_gl(self):
tas = p.get_val('phase0.timeseries.TAS', units='km/s')
range = p.get_val('phase0.timeseries.states:range')

assert_rel_error(self, range, tas*time, tolerance=1.0E-4)
assert_near_equal(range, tas*time, tolerance=1.0E-4)

def test_cruise_results_radau(self):
p = om.Problem(model=om.Group())
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_cruise_results_radau(self):
tas = p.get_val('phase0.timeseries.TAS', units='km/s')
range = p.get_val('phase0.timeseries.states:range')

assert_rel_error(self, range, tas*time, tolerance=1.0E-4)
assert_near_equal(range, tas*time, tolerance=1.0E-4)


if __name__ == '__main__': # pragma: no cover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

from dymos.examples.aircraft_steady_flight.aircraft_ode import AircraftODE

Expand Down Expand Up @@ -52,9 +52,8 @@ def setUpClass(cls):
cls.p.run_model()

def test_results(self):
assert_rel_error(self,
self.p['ode.range_rate_comp.dXdt:range'],
self.p['ode.tas_comp.TAS'] * np.cos(self.p['ode.gam_comp.gam']))
assert_near_equal(self.p['ode.range_rate_comp.dXdt:range'],
self.p['ode.tas_comp.TAS'] * np.cos(self.p['ode.gam_comp.gam']))


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import openmdao.api as om
from openmdao.utils.general_utils import set_pyoptsparse_opt
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

import dymos as dm
from dymos.utils.lgl import lgl
Expand Down Expand Up @@ -120,14 +120,14 @@ def tearDownClass(cls):

def test_ex_aircraft_steady_flight_opt(self):
p = ex_aircraft_steady_flight(optimizer='SLSQP', solve_segments=False)
assert_rel_error(self, p.get_val('phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)
assert_near_equal(p.get_val('phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)

def test_ex_aircraft_steady_flight_solve(self):
p = ex_aircraft_steady_flight(optimizer='SLSQP', solve_segments=True,
use_boundary_constraints=True)
assert_rel_error(self, p.get_val('phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)
assert_near_equal(p.get_val('phase0.timeseries.states:range', units='NM')[-1],
726.85, tolerance=1.0E-2)


if __name__ == '__main__': # pragma: no cover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_basic(self):
import matplotlib.pyplot as plt

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

import dymos as dm
from dymos.examples.battery_multibranch.battery_multibranch_ode import BatteryODE
Expand Down Expand Up @@ -100,13 +100,13 @@ def test_basic(self):

# Final value for State of Chrage in each segment should be a good test.
print('State of Charge after 1 hour')
assert_rel_error(self, soc0[-1], 0.63464982, 1e-6)
assert_near_equal(soc0[-1], 0.63464982, 1e-6)
print('State of Charge after 2 hours')
assert_rel_error(self, soc1[-1], 0.23794217, 1e-6)
assert_near_equal(soc1[-1], 0.23794217, 1e-6)
print('State of Charge after 2 hours, battery fails at 1 hour')
assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6)
assert_near_equal(soc1b[-1], 0.0281523, 1e-6)
print('State of Charge after 2 hours, motor fails at 1 hour')
assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
assert_near_equal(soc1m[-1], 0.18625395, 1e-6)

# Plot Results
t0 = prob['traj.phases.phase0.time.time']/3600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import unittest

import openmdao.api as om
from openmdao.utils.assert_utils import assert_rel_error
from openmdao.utils.assert_utils import assert_near_equal

import dymos as dm
from dymos.examples.battery_multibranch.battery_multibranch_ode import BatteryODE
Expand Down Expand Up @@ -109,10 +109,10 @@ def test_optimizer_defects(self):
soc1m = prob['traj.phase1_mfail.states:state_of_charge']

# Final value for State of Chrage in each segment should be a good test.
assert_rel_error(self, soc0[-1], 0.63464982, 1e-6)
assert_rel_error(self, soc1[-1], 0.23794217, 1e-6)
assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6)
assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
assert_near_equal(soc0[-1], 0.63464982, 1e-6)
assert_near_equal(soc1[-1], 0.23794217, 1e-6)
assert_near_equal(soc1b[-1], 0.0281523, 1e-6)
assert_near_equal(soc1m[-1], 0.18625395, 1e-6)

def test_solver_defects(self):
prob = om.Problem()
Expand Down Expand Up @@ -195,10 +195,10 @@ def test_solver_defects(self):
soc1m = prob['traj.phase1_mfail.states:state_of_charge']

# Final value for State of Charge in each segment should be a good test.
assert_rel_error(self, soc0[-1], 0.63464982, 1e-6)
assert_rel_error(self, soc1[-1], 0.23794217, 1e-6)
assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6)
assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
assert_near_equal(soc0[-1], 0.63464982, 1e-6)
assert_near_equal(soc1[-1], 0.23794217, 1e-6)
assert_near_equal(soc1b[-1], 0.0281523, 1e-6)
assert_near_equal(soc1m[-1], 0.18625395, 1e-6)

def test_solver_defects_single_phase_reverse_propagation(self):
prob = om.Problem()
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_solver_defects_single_phase_reverse_propagation(self):
prob.run_model()

soc0 = prob['phase0.states:state_of_charge']
assert_rel_error(self, soc0[-1], 1.0, 1e-6)
assert_near_equal(soc0[-1], 1.0, 1e-6)

def test_solver_defects_reverse_propagation(self):
prob = om.Problem()
Expand Down Expand Up @@ -273,7 +273,7 @@ def test_solver_defects_reverse_propagation(self):
prob.run_model()

soc1 = prob['traj.phase1.states:state_of_charge']
assert_rel_error(self, soc1[-1], 1.0, 1e-6)
assert_near_equal(soc1[-1], 1.0, 1e-6)

def test_optimizer_segments_direct_connections(self):
prob = om.Problem()
Expand Down Expand Up @@ -365,10 +365,10 @@ def test_optimizer_segments_direct_connections(self):
soc1m = prob['traj.phase1_mfail.states:state_of_charge']

# Final value for State of Chrage in each segment should be a good test.
assert_rel_error(self, soc0[-1], 0.63464982, 1e-6)
assert_rel_error(self, soc1[-1], 0.23794217, 1e-6)
assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6)
assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
assert_near_equal(soc0[-1], 0.63464982, 1e-6)
assert_near_equal(soc1[-1], 0.23794217, 1e-6)
assert_near_equal(soc1b[-1], 0.0281523, 1e-6)
assert_near_equal(soc1m[-1], 0.18625395, 1e-6)


class TestBatteryBranchingPhasesRungeKutta(unittest.TestCase):
Expand Down Expand Up @@ -465,10 +465,10 @@ def test_constraint_linkages_rk(self):
soc1m = prob['traj.phase1_mfail.states:state_of_charge']

# Final value for State of Charge in each segment should be a good test.
assert_rel_error(self, soc0[-1], 0.63464982, 1e-4)
assert_rel_error(self, soc1[-1], 0.23794217, 1e-4)
assert_rel_error(self, soc1b[-1], 0.0281523, 1e-4)
assert_rel_error(self, soc1m[-1], 0.18625395, 1e-4)
assert_near_equal(soc0[-1], 0.63464982, 1e-4)
assert_near_equal(soc1[-1], 0.23794217, 1e-4)
assert_near_equal(soc1b[-1], 0.0281523, 1e-4)
assert_near_equal(soc1m[-1], 0.18625395, 1e-4)

def test_single_phase_reverse_propagation_rk(self):
prob = om.Problem()
Expand All @@ -495,7 +495,7 @@ def test_single_phase_reverse_propagation_rk(self):
prob.run_model()

soc0 = prob['phase0.states:state_of_charge']
assert_rel_error(self, soc0[-1], 1.0, 1e-6)
assert_near_equal(soc0[-1], 1.0, 1e-6)

def test_connected_linkages_rk(self):
prob = om.Problem()
Expand Down Expand Up @@ -592,10 +592,10 @@ def test_connected_linkages_rk(self):
soc1m = prob['traj.phase1_mfail.states:state_of_charge']

# Final value for State of Chrage in each segment should be a good test.
assert_rel_error(self, soc0[-1], 0.63464982, 5e-5)
assert_rel_error(self, soc1[-1], 0.23794217, 5e-5)
assert_rel_error(self, soc1b[-1], 0.0281523, 5e-5)
assert_rel_error(self, soc1m[-1], 0.18625395, 5e-5)
assert_near_equal(soc0[-1], 0.63464982, 5e-5)
assert_near_equal(soc1[-1], 0.23794217, 5e-5)
assert_near_equal(soc1b[-1], 0.0281523, 5e-5)
assert_near_equal(soc1m[-1], 0.18625395, 5e-5)


if __name__ == '__main__': # pragma: no cover
Expand Down
Loading

0 comments on commit 298043d

Please sign in to comment.