def test_nodes_and_weights_5(self): x_5, w_5 = lgr(5) assert_almost_equal(x_5, x_i[5], decimal=6) assert_almost_equal(w_5, w_i[5], decimal=6) x_5, w_5 = lgr(5, include_endpoint=True) assert_almost_equal(x_5, x_i[5] + [1], decimal=6) assert_almost_equal(w_5, w_i[5] + [0], decimal=6)
def test_nodes_and_weights_4(self): x_4, w_4 = lgr(4) assert_almost_equal(x_4, x_i[4], decimal=6) assert_almost_equal(w_4, w_i[4], decimal=6) x_4, w_4 = lgr(4, include_endpoint=True) assert_almost_equal(x_4, x_i[4] + [1], decimal=6) assert_almost_equal(w_4, w_i[4] + [0], decimal=6)
def test_nodes_and_weights_3(self): x_3, w_3 = lgr(3) assert_almost_equal(x_3, x_i[3], decimal=6) assert_almost_equal(w_3, w_i[3], decimal=6) x_3, w_3 = lgr(3, include_endpoint=True) assert_almost_equal(x_3, x_i[3] + [1], decimal=6) assert_almost_equal(w_3, w_i[3] + [0], decimal=6)
def test_nodes_and_weights_2(self): x_2, w_2 = lgr(2) assert_almost_equal(x_2, x_i[2], decimal=6) assert_almost_equal(w_2, w_i[2], decimal=6) x_2, w_2 = lgr(2, include_endpoint=True) assert_almost_equal(x_2, x_i[2] + [1], decimal=6) assert_almost_equal(w_2, w_i[2] + [0], decimal=6)
def radau_pseudospectral_subsets_and_nodes(n, seg_idx, compressed=False): """ Returns the subset dictionary corresponding to the Radau Pseudospectral transcription. Parameters ---------- n : int The total number of nodes in the Radau Pseudospectral segment (including right endpoint). seg_idx : int The index of this segment within its phase. compressed : bool True if the subset requested is for a phase with compressed transcription. Returns ------- dict A dictionary with the following keys: 'disc' gives the indices of the state discretization nodes (deprecated) 'state_disc' gives the indices of the state discretization nodes 'state_input' gives the indices of the state input nodes 'control_disc' gives the indices of the control discretization nodes 'control_input' gives the indices of the control input nodes 'segment_ends' gives the indices of the nodes at the start (even) and end (odd) of a segment 'col' gives the indices of the collocation nodes 'all' gives all node indices. Notes ----- Subset 'state_input' is the same as subset 'state_disc' if `compressed == False` or `first_seg == True`. For Radau-Pseudospectral transcription, subset 'control_input' is always the same as subset 'control_disc'. """ subsets = { 'disc': np.arange(n + 1, dtype=int), 'state_disc': np.arange(n + 1, dtype=int), 'state_input': np.arange(n + 1, dtype=int) if not compressed or seg_idx == 0 else np.arange(1, n + 1, dtype=int), 'control_disc': np.arange(n, dtype=int), 'control_input': np.arange(n, dtype=int), 'segment_ends': np.array([0, n], dtype=int), 'col': np.arange(n, dtype=int), 'all': np.arange(n + 1, dtype=int), 'solution': np.arange(n + 1, dtype=int), } return subsets, lgr(n, include_endpoint=True)[0]
def test_state_interp_comp_radau(self): gd = GridData(num_segments=1, transcription_order=3, segment_ends=np.array([0, 10]), transcription='radau-ps') p = om.Problem(model=om.Group()) states = { 'x': { 'units': 'm', 'shape': (1, ) }, 'v': { 'units': 'm/s', 'shape': (1, ) } } X_ivc = om.IndepVarComp() p.model.add_subsystem('X_ivc', X_ivc, promotes=['state_disc:x', 'state_disc:v']) X_ivc.add_output('state_disc:x', val=np.zeros(gd.subset_num_nodes['state_disc']), units='m') X_ivc.add_output('state_disc:v', val=np.zeros(gd.subset_num_nodes['state_disc']), units='m/s') dt_dtau_ivc = om.IndepVarComp() dt_dtau_ivc.add_output('dt_dstau', val=0.0 * np.zeros(gd.subset_num_nodes['col']), units='s') p.model.add_subsystem('dt_dstau_ivc', dt_dtau_ivc, promotes=['dt_dstau']) p.model.add_subsystem('state_interp_comp', subsys=StateInterpComp(transcription='radau-ps', grid_data=gd, state_options=states, time_units='s')) p.model.connect('state_disc:x', 'state_interp_comp.state_disc:x') p.model.connect('state_disc:v', 'state_interp_comp.state_disc:v') p.model.connect('dt_dstau', 'state_interp_comp.dt_dstau') p.setup(force_alloc_complex=True) lgr_nodes, lgr_weights = lgr(3, include_endpoint=True) t_disc = (lgr_nodes + 1.0) * 5.0 t_col = t_disc[:-1] # Test 1: Let x = t**2, f = 2*t p['state_disc:x'] = t_disc**2 # Test 1: Let v = t**3-10*t**2, f = 3*t**2 - 20*t p['state_disc:v'] = t_disc**3 - 10 * t_disc**2 p['dt_dstau'] = 10 / 2.0 p.run_model() if SHOW_PLOTS: # pragma: no cover f, ax = plt.subplots(2, 1) t_disc = np.array([0, 5, 10]) t_col = np.array([2.5, 7.5]) t = np.linspace(0, 10, 100) x1 = t**2 xdot1 = 2 * t x2 = t**3 - 10 * t**2 xdot2 = 3 * t**2 - 20 * t ax[0].plot(t, x1, 'b-', label='$x$') ax[0].plot(t, xdot1, 'b--', label='$\dot{x}$') ax[0].plot(t_disc, p['state_disc:x'], 'bo', label='$X_d:x$') ax[0].plot(t_col, p['state_interp_comp.state_col:x'], 'bv', label='$X_c:x$') ax[0].plot(t_col, p['state_interp_comp.staterate_col:x'], marker='v', color='None', mec='b', label='$Xdot_c:x$') ax[1].plot(t, x2, 'r-', label='$v$') ax[1].plot(t, xdot2, 'r--', label='$\dot{v}$') ax[1].plot(t_disc, p['state_disc:v'], 'ro', label='$X_d:v$') ax[1].plot(t_col, p['state_interp_comp.state_col:v'], 'rv', label='$X_c:v$') ax[1].plot(t_col, p['state_interp_comp.staterate_col:v'], marker='v', color='None', mec='r', label='$Xdot_c:v$') ax[0].legend(loc='upper left', ncol=3) ax[1].legend(loc='upper left', ncol=3) plt.show() # Test 1 assert_almost_equal(p['state_interp_comp.staterate_col:x'][:, 0], 2 * t_col) # Test 2 assert_almost_equal(p['state_interp_comp.staterate_col:v'][:, 0], 3 * t_col**2 - 20 * t_col) cpd = p.check_partials(compact_print=True, method='cs') assert_check_partials(cpd, atol=1.0E-5)
def get_points(n): return lgr(n, include_endpoint=True)