def write_needed_inputs( self, source_file_path: str = None, source_formatter: IVariableIOFormatter = None, ): """ Writes the input file of the problem with unconnected inputs of the problem. .. warning:: :meth:`setup` must have been run on this Problem instance. Written value of each variable will be taken: 1. from input_data if it contains the variable 2. from defined default values in component definitions WARNING: if inputs have already been read, they won't be needed any more and won't be in written file. :param source_file_path: if provided, variable values will be read from it :param source_formatter: the class that defines format of input file. if not provided, expected format will be the default one. """ variables = VariableList.from_unconnected_inputs( self, with_optional_inputs=True) if source_file_path: ref_vars = VariableIO(source_file_path, source_formatter).read() variables.update(ref_vars) for var in variables: var.is_input = True writer = VariableIO(self.input_file_path) writer.write(variables)
def write_needed_inputs( self, source_file_path: str = None, source_formatter: IVariableIOFormatter = None, ): """ Writes the input file of the problem with unconnected inputs of the configured problem. Written value of each variable will be taken: 1. from input_data if it contains the variable 2. from defined default values in component definitions :param source_file_path: if provided, variable values will be read from it :param source_formatter: the class that defines format of input file. if not provided, expected format will be the default one. """ problem = self.get_problem(read_inputs=False) problem.setup() variables = VariableList.from_unconnected_inputs( problem, with_optional_inputs=True) if source_file_path: ref_vars = VariableIO(source_file_path, source_formatter).read() variables.update(ref_vars) for var in variables: var.is_input = True writer = VariableIO(problem.input_file_path) writer.write(variables)
def write_outputs(self): """ Writes all outputs in the configured output file. """ if self.output_file_path: writer = VariableIO(self.output_file_path) variables = VariableList.from_problem(self) writer.write(variables)
def write_outputs(self): """ Writes all outputs in the configured output file. """ if self.output_file_path: writer = VariableIO(self.output_file_path) if self.additional_variables is None: self.additional_variables = [] variables = VariableList(self.additional_variables) for var in variables: var.is_input = None variables.update( VariableList.from_problem(self, promoted_only=True), add_variables=True ) writer.write(variables)
def test_basic_xml_read_and_write_from_vars(cleanup): """ Tests the creation of an XML file from a VariableList instance """ result_folder = pth.join(RESULTS_FOLDER_PATH, "basic_xml") # Check write hand-made component vars = VariableList() vars["geometry/total_surface"] = {"value": [780.3], "units": "m**2"} vars["geometry/wing/span"] = {"value": 42.0, "units": "m", "description": "span of the wing"} vars["geometry/wing/aspect_ratio"] = {"value": [9.8]} vars["geometry/fuselage/length"] = {"value": 40.0, "units": "m"} vars["constants"] = {"value": [-42.0], "description": "the answer"} vars["constants/k1"] = {"value": [1.0, 2.0, 3.0], "units": "kg"} vars["constants/k2"] = {"value": [10.0, 20.0], "description": "Geronimo!"} vars["constants/k3"] = {"value": np.array([100.0, 200.0, 300.0, 400.0]), "units": "m/s"} vars["constants/k4"] = {"value": [-1.0, -2.0, -3.0]} vars["constants/k5"] = {"value": [100.0, 200.0, 400.0, 500.0, 600.0]} vars["constants/k8"] = {"value": [[1e2, 3.4e5], [5.4e3, 2.1]]} # Try writing with non-existing folder assert not pth.exists(result_folder) filename = pth.join(result_folder, "handmade.xml") xml_write = VariableIO(filename, formatter=VariableXmlStandardFormatter()) xml_write.path_separator = "/" xml_write.write(vars) # check (read another IndepVarComp instance from xml) xml_check = VariableIO(filename, formatter=VariableXmlStandardFormatter()) xml_check.path_separator = ":" new_vars = xml_check.read() _check_basic_vars(new_vars) # Check reading hand-made XML (with some format twists) filename = pth.join(DATA_FOLDER_PATH, "basic.xml") xml_read = VariableIO(filename, formatter=VariableXmlStandardFormatter()) xml_read.path_separator = ":" vars = xml_read.read() _check_basic_vars(vars) # write it (with existing destination folder) new_filename = pth.join(result_folder, "basic.xml") xml_write = VariableIO(new_filename, formatter=VariableXmlStandardFormatter()) xml_write.path_separator = ":" xml_write.write(vars) # check (read another IndepVarComp instance from new xml) xml_check = VariableIO(new_filename, formatter=VariableXmlStandardFormatter()) xml_check.path_separator = ":" new_vars = xml_check.read() _check_basic_vars(new_vars) # try to write with bad separator xml_write.formatter.path_separator = "/" with pytest.raises(FastXPathEvalError): xml_write.write(vars)
def test_basic_xml_partial_read_and_write_from_vars(cleanup): """ Tests the creation of an XML file from an IndepVarComp instance with only and ignore options """ result_folder = pth.join(RESULTS_FOLDER_PATH, "basic_partial_xml") # Read full IndepVarComp filename = pth.join(DATA_FOLDER_PATH, "basic.xml") xml_read = VariableIO(filename, formatter=VariableXmlStandardFormatter()) vars = xml_read.read(ignore=["does_not_exist"]) _check_basic_vars(vars) # Add something to ignore and write it vars["should_be_ignored:pointless"] = {"value": 0.0} vars["should_also_be_ignored"] = {"value": -10.0} badvar_filename = pth.join(result_folder, "with_bad_var.xml") xml_write = VariableIO(badvar_filename, formatter=VariableXmlStandardFormatter()) xml_write.write(vars, ignore=["does_not_exist"]) # Check with non-existent var in ignore list tree = etree.parse(badvar_filename) assert float(tree.xpath("should_be_ignored/pointless")[0].text.strip()) == 0.0 assert float(tree.xpath("should_also_be_ignored")[0].text.strip()) == -10.0 # Check partial reading with 'ignore' xml_read = VariableIO(badvar_filename, formatter=VariableXmlStandardFormatter()) new_vars = xml_read.read(ignore=["should_be_ignored:pointless", "should_also_be_ignored"]) _check_basic_vars(new_vars) # Check partial reading with 'only' ok_vars = [ "geometry:total_surface", "geometry:wing:span", "geometry:wing:aspect_ratio", "geometry:fuselage:length", "constants", "constants:k1", "constants:k2", "constants:k3", "constants:k4", "constants:k5", "constants:k8", ] new_vars2 = xml_read.read(only=ok_vars) _check_basic_vars(new_vars2) # Check partial writing with 'ignore' varok_filename = pth.join(result_folder, "with_bad_var.xml") xml_write = VariableIO(varok_filename, formatter=VariableXmlStandardFormatter()) xml_write.write(vars, ignore=["should_be_ignored:pointless", "should_also_be_ignored"]) xml_read = VariableIO(varok_filename, formatter=VariableXmlStandardFormatter()) new_vars = xml_read.read() _check_basic_vars(new_vars) # Check partial writing with 'only' varok2_filename = pth.join(result_folder, "with_bad_var.xml") xml_write = VariableIO(varok2_filename, formatter=VariableXmlStandardFormatter()) xml_write.write(vars, only=ok_vars) xml_read = VariableIO(varok2_filename, formatter=VariableXmlStandardFormatter()) new_vars = xml_read.read() _check_basic_vars(new_vars)