def test_prefix_plus_math(self): # From an issue: m**2 converts fine, but cm**2 does not. x1 = convert_units(1.0, 'm**2', 'cm**2') self.assertEqual(x1, 10000.0) # Let's make sure we can dclare some complicated units x = PhysicalQuantity('7200nm**3/kPa*dL') #from issue 825, make sure you can handle single characters before a / x = PhysicalQuantity('1 g/kW')
def _parse_target(target_definition: dict): """ For each parameter in target definition, if the associated value is a dict (with "value" and "unit" as keys), transforms it into a value in base units, as defined in BASE_UNITS. :param target_definition: """ for target_key, target_value in target_definition.items(): if isinstance(target_value, dict) and "value" in target_value: target_definition[target_key] = om.convert_units( target_value["value"], target_value.get("unit"), BASE_UNITS.get(target_key), )
def _parse_values_and_units(cls, definition): """ Browse recursively provided dictionary and if a dictionary that has "value" and "unit" as only keys is found, it is transformed into a value in base units, as defined in BASE_UNITS. Does nothing if definition is not a dict. :param definition: """ if isinstance(definition, dict): for key, value in definition.items(): if isinstance(value, dict) and "value" in value.keys(): definition[key] = om.convert_units( value["value"], value.get("unit"), BASE_UNITS.get(key), ) else: cls._parse_values_and_units(value) elif isinstance(definition, list): for value in definition: cls._parse_values_and_units(value)
def eval_ode_on_grid(phase, transcription): """ Evaluate the ODE from the given phase at all nodes of the given transcription. Parameters ---------- phase : Phase A Phase object which has been executed and whose ODE is to be run at all nodes of the given transcription. transcription : Radau or GaussLobatto transcription instance The transcription object at which to execute the ODE of the given phase at all nodes. Returns ------- dict of (str: ndarray) A dictionary of the state values from the phase interpolated to the new transcription. dict of (str: ndarray) A dictionary of the control values from the phase interpolated to the new transcription. dict of (str: ndarray) A dictionary of the polynomial control values from the phase interpolated to the new transcription. dict of (str: ndarray) A dictionary of the state rates computed in the phase's ODE at the new transcription points. """ x = {} u = {} u_rate = {} u_rate2 = {} p = {} p_rate = {} p_rate2 = {} param = {} f = {} # Build the interpolation matrix which interpolates from all nodes on the old grid to # all nodes on the new grid. grid_data = transcription.grid_data L, _ = interpolation_lagrange_matrix( old_grid=phase.options['transcription'].grid_data, new_grid=grid_data) # Create a new problem for the grid_refinement # For this test, use the same grid as the original problem. p_refine = om.Problem(model=om.Group()) grid_refinement_system = GridRefinementODESystem( grid_data=grid_data, time=phase.time_options, states=phase.state_options, controls=phase.control_options, polynomial_controls=phase.polynomial_control_options, parameters=phase.parameter_options, ode_class=phase.options['ode_class'], ode_init_kwargs=phase.options['ode_init_kwargs']) p_refine.model.add_subsystem('grid_refinement_system', grid_refinement_system, promotes=['*']) p_refine.setup() # Set the values in the refinement problem using the outputs from the first ode = p_refine.model.grid_refinement_system.ode t_prev = phase.get_val('timeseries.time', units=phase.time_options['units']) t_phase_prev = phase.get_val('timeseries.time_phase', units=phase.time_options['units']) t_initial = np.repeat(t_prev[0, 0], repeats=transcription.grid_data.num_nodes, axis=0) t_duration = np.repeat(t_prev[-1, 0], repeats=transcription.grid_data.num_nodes, axis=0) t = np.dot(L, t_prev) t_phase = np.dot(L, t_phase_prev) targets = get_targets(ode, 'time', phase.time_options['targets']) time_phase_targets = get_targets(ode, 'time_phase', phase.time_options['time_phase_targets']) t_initial_targets = get_targets(ode, 't_initial', phase.time_options['t_initial_targets']) t_duration_targets = get_targets(ode, 't_duration', phase.time_options['t_duration_targets']) if targets: p_refine.set_val(f'time', t) if time_phase_targets: p_refine.set_val(f'time_phase', t_phase) if t_initial_targets: p_refine.set_val(f't_initial', t_initial) if t_duration_targets: p_refine.set_val(f't_duration', t_duration) for name, options in phase.state_options.items(): x_prev = phase.get_val(f'timeseries.states:{name}', units=options['units']) x[name] = np.dot(L, x_prev) targets = get_targets(ode, name, options['targets']) if targets: p_refine.set_val(f'states:{name}', x[name]) for name, options in phase.control_options.items(): targets = get_targets(ode, name, options['targets']) rate_targets = get_targets(ode, f'{name}_rate', options['rate_targets']) rate2_targets = get_targets(ode, f'{name}_rate12', options['rate2_targets']) u_prev = phase.get_val(f'timeseries.controls:{name}', units=options['units']) u[name] = np.dot(L, u_prev) if targets: p_refine.set_val(f'controls:{name}', u[name]) u_rate_prev = phase.get_val(f'timeseries.control_rates:{name}_rate') u_rate[name] = np.dot(L, u_rate_prev) if rate_targets: p_refine.set_val(f'control_rates:{name}_rate', u_rate[name]) u_rate2_prev = phase.get_val(f'timeseries.control_rates:{name}_rate2') u_rate2[name] = np.dot(L, u_rate2_prev) if rate2_targets: p_refine.set_val(f'control_rates:{name}_rate2', u_rate2[name]) for name, options in phase.polynomial_control_options.items(): targets = get_targets(ode, name, options['targets']) rate_targets = get_targets(ode, f'{name}_rate', options['rate_targets']) rate2_targets = get_targets(ode, f'{name}_rate2', options['rate2_targets']) p_prev = phase.get_val(f'timeseries.polynomial_controls:{name}', units=options['units']) p[name] = np.dot(L, p_prev) if targets: p_refine.set_val(f'polynomial_controls:{name}', p[name]) p_rate_prev = phase.get_val( f'timeseries.polynomial_control_rates:{name}_rate') p_rate[name] = np.dot(L, p_rate_prev) if rate_targets: p_refine.set_val(f'polynomial_control_rates:{name}_rate', p_rate[name]) p_rate2_prev = phase.get_val( f'timeseries.polynomial_control_rates:{name}_rate2') p_rate2[name] = np.dot(L, p_rate2_prev) if rate2_targets: p_refine.set_val(f'polynomial_control_rates:{name}_rate2', p_rate2[name]) # Configure the parameters for name, options in phase.parameter_options.items(): targets = get_targets(ode, name, options['targets']) # The value of the parameter at one node param[name] = phase.get_val(f'timeseries.parameters:{name}', units=options['units'])[0, ...] if targets: p_refine.set_val(f'parameters:{name}', param[name], units=options['units']) # Execute the model p_refine.run_model() # Assign the state rates on the new grid to f for name, options in phase.state_options.items(): rate_units = get_rate_units(options['units'], phase.time_options['units']) rate_source = options['rate_source'] rate_source_class = phase.classify_var(rate_source) if rate_source_class in {'time'}: src_units = phase.time_options['units'] f[name] = om.convert_units(t, src_units, rate_units) elif rate_source_class in {'time_phase'}: src_units = phase.time_options['units'] f[name] = om.convert_units(t_phase, src_units, rate_units) elif rate_source_class in {'state'}: src_units = phase.state_options[rate_source]['units'] f[name] = om.convert_units(x[rate_source], src_units, rate_units) elif rate_source_class in {'input_control', 'indep_control'}: src_units = phase.control_options[rate_source]['units'] f[name] = om.convert_units(u[rate_source], src_units, rate_units) elif rate_source_class in {'control_rate'}: u_name = rate_source[:-5] u_units = phase.control_options[u_name]['units'] src_units = get_rate_units(u_units, phase.time_options['units'], deriv=1) f[name] = om.convert_units(u_rate[rate_source], src_units, rate_units) elif rate_source_class in {'control_rate2'}: u_name = rate_source[:-6] u_units = phase.control_options[u_name]['units'] src_units = get_rate_units(u_units, phase.time_options['units'], deriv=2) f[name] = om.convert_units(u_rate2[rate_source], src_units, rate_units) elif rate_source_class in { 'input_polynomial_control', 'indep_polynomial_control' }: src_units = phase.polynomial_control_options[rate_source]['units'] f[name] = om.convert_units(p[rate_source], src_units, rate_units) elif rate_source_class in {'polynomial_control_rate'}: pc_name = rate_source[:-5] pc_units = phase.polynomial_control_options[pc_name]['units'] src_units = get_rate_units(pc_units, phase.time_options['units'], deriv=1) f[name] = om.convert_units(p_rate[rate_source], src_units, rate_units) elif rate_source_class in {'polynomial_control_rate2'}: pc_name = rate_source[:-6] pc_units = phase.polynomial_control_options[pc_name]['units'] src_units = get_rate_units(pc_units, phase.time_options['units'], deriv=2) f[name] = om.convert_units(p_rate2[rate_source], src_units, rate_units) elif rate_source_class in {'parameter'}: src_units = phase.parameter_options[rate_source]['units'] shape = phase.parameter_options[rate_source]['shape'] f[name] = om.convert_units(param[rate_source], src_units, rate_units) f[name] = np.broadcast_to(f[name], (grid_data.num_nodes, ) + shape) elif rate_source_class in {'ode'}: f[name] = p_refine.get_val(f'ode.{rate_source}', units=rate_units) if len(f[name].shape) == 1: f[name] = np.reshape(f[name], newshape=(f[name].shape[0], 1)) return x, u, p, f
def print_memory_state(tag): memory = om.convert_units( tracemalloc.get_traced_memory()[0] - tracemalloc.get_tracemalloc_memory(), "byte", "Mibyte") print(f"{tag:50}:", f"{memory:.3f}MiB") return memory