def list_inputs(component: Union[om.ExplicitComponent, om.Group]) -> list: """ Reads input variables from a component/problem and return as a list """ register_wrappers() variables = VariableList.from_system(component) input_names = [var.name for var in variables if var.is_input] return input_names
def list_variables(component: Union[om.ExplicitComponent, om.Group]) -> list: """ Reads all variables from a component/problem and return as a list """ register_wrappers() if isinstance(component, om.Group): new_component = AutoUnitsDefaultGroup() new_component.add_subsystem("system", component, promotes=['*']) component = new_component variables = VariableList.from_system(component) return variables
def test_register_checks_as_decorator(cleanup): log_file_path = pth.join(RESULTS_FOLDER_PATH, "log4.txt") set_logger_file(log_file_path) @ValidityDomainChecker({"input1": (1.0, 5.0), "output2": (300.0, 500.0)}, "main.dec") class Comp1(om.ExplicitComponent): def setup(self): self.add_input("input1", 0.5, units="km") self.add_output("output1", 150.0, units="km/h", upper=130.0) self.add_output("output2", 200.0, units="K", lower=0.0) self.add_output("output3", 1000.0, units="kg", lower=0.0, upper=5000.0) comp = Comp1() # Just checking there is no side effect. VariableList.from_system() uses # setup(), even if it is made to have no effect, and ValidityDomainChecker # modifies setup(), so is is worth checking. variables = VariableList.from_system(comp) assert len(variables) == 4 # Now for the real test # We test that upper and lower bounds are retrieved from OpenMDAO component, # overwritten when required and that units are correctly taken into account. comp.setup() variables = VariableList( [ Variable("input1", value=3.0, units="m"), Variable("output1", value=40.0, units="m/s"), Variable("output2", value=310.0, units="degC"), Variable("output3", value=6.0, units="t"), ] ) records = ValidityDomainChecker.check_variables(variables) assert [ ( rec.variable_name, rec.status, rec.limit_value, rec.value, rec.source_file, rec.logger_name, ) for rec in records ] == [ ("input1", ValidityStatus.TOO_LOW, 1.0, 3.0, __file__, "main.dec"), ("output1", ValidityStatus.TOO_HIGH, 130.0, 40.0, __file__, "main.dec"), ("output2", ValidityStatus.TOO_HIGH, 500.0, 310.0, __file__, "main.dec"), ("output3", ValidityStatus.TOO_HIGH, 5000.0, 6.0, __file__, "main.dec"), ] ValidityDomainChecker.log_records(records) with open(log_file_path) as log_file: assert len(log_file.readlines()) == 4
def setup(self): """Will replace the original setup method.""" # Ok kid, this is where it gets maybe too complicated... # We need to get variables of the current OpenMDAO instance. # This is done with VariableList.from_system() which we know does # setup() on a copy of given instance (not much choice to be sure # from_system() will work also for Group instances) # But by doing this, it will call this setup() and be stuck in an # infinite recursion. # So we create a copy of current instance where we put back the # original setup so we can safely use VariableList.from_system() original_self = deepcopy(self) setattr(original_self, "setup", getattr(original_self, setup_new_name)) variables = VariableList.from_system(original_self) # Now update limit definition limit_definitions = ValidityDomainChecker._limit_definitions[checker_id] for var in variables: if var.name in limit_definitions: # Get units for already defined limits limit_def = limit_definitions[var.name] if limit_def.units is None and var.units is not None: limit_def.units = var.units elif "lower" in var.metadata or "upper" in var.metadata: # Get bounds if defined in add_output. lower = var.metadata.get("lower") # lower can be None if it is not found OR if it defined and set to None if lower is None: lower = -np.inf upper = var.metadata.get("upper") # upper can be None if it is not found OR if it defined and set to None if upper is None: upper = np.inf units = var.metadata.get("units") if lower > -np.inf or upper < np.inf: limit_definitions[var.name] = _LimitDefinition(lower, upper, units) # Now run original setup that has been moved to setup_new_name getattr(self, setup_new_name)()