def _pre_run_model_debug_print(self): """ Optionally print some debugging information before the model runs. """ debug_opt = self.options['debug_print'] if not debug_opt or debug_opt == ['totals']: return if not MPI or MPI.COMM_WORLD.rank == 0: header = 'Driver debug print for iter coord: {}'.format( get_formatted_iteration_coordinate()) print(header) print(len(header) * '-') if 'desvars' in debug_opt: desvar_vals = self.get_design_var_values(unscaled=True, ignore_indices=True) if not MPI or MPI.COMM_WORLD.rank == 0: print("Design Vars") if desvar_vals: pprint.pprint(desvar_vals) else: print("None") print() sys.stdout.flush()
def solve(self): """ Run the solver. Returns ------- boolean Failure flag; True if failed to converge, False is successful. float absolute error. float relative error. """ fail, abs_err, rel_err = self._run_iterator() if fail and self.options['debug_print']: coord = get_formatted_iteration_coordinate() out_str = "\n# Inputs and outputs at start of iteration '%s':\n" % coord for vec_type, vec in iteritems(self._err_cache): out_str += '\n' out_str += '# %s %ss\n' % (vec._name, vec._typ) out_str += pprint.pformat(vec._views) out_str += '\n' print(out_str) filename = coord.replace('._solve_nonlinear', '') filename = re.sub('[^0-9a-zA-Z]', '_', filename) + '.dat' with open(filename, 'w') as f: f.write(out_str) print("Inputs and outputs at start of iteration have been " "saved to '%s'." % filename) return fail, abs_err, rel_err
def record_iteration(self, recording_requester, data, metadata, **kwargs): """ Route the record_iteration call to the proper method. Parameters ---------- recording_requester : object System, Solver, Driver in need of recording. metadata : dict, optional Dictionary containing execution metadata. data : dict Dictionary containing desvars, objectives, constraints, responses, and System vars. **kwargs : keyword args Some implementations of record_iteration need additional args. """ if not self._parallel: if MPI and MPI.COMM_WORLD.rank > 0: raise RuntimeError( "Non-parallel recorders should not be recording on ranks > 0" ) self._counter += 1 self._iteration_coordinate = get_formatted_iteration_coordinate() if isinstance(recording_requester, Driver): self.record_iteration_driver(recording_requester, data, metadata) elif isinstance(recording_requester, System): self.record_iteration_system(recording_requester, data, metadata) elif isinstance(recording_requester, Solver): self.record_iteration_solver(recording_requester, data, metadata) else: raise ValueError( "Recorders must be attached to Drivers, Systems, or Solvers.")
def record_iteration(self, recording_requester, data, metadata, **kwargs): """ Route the record_iteration call to the proper method. Parameters ---------- recording_requester : object System, Solver, Driver in need of recording. metadata : dict, optional Dictionary containing execution metadata. data : dict Dictionary containing desvars, objectives, constraints, responses, and System vars. **kwargs : keyword args Some implementations of record_iteration need additional args. """ if not self._parallel: if MPI and MPI.COMM_WORLD.rank > 0: raise RuntimeError("Non-parallel recorders should not be recording on ranks > 0") self._counter += 1 self._iteration_coordinate = get_formatted_iteration_coordinate() if isinstance(recording_requester, Driver): self.record_iteration_driver(recording_requester, data, metadata) elif isinstance(recording_requester, System): self.record_iteration_system(recording_requester, data, metadata) elif isinstance(recording_requester, Solver): self.record_iteration_solver(recording_requester, data, metadata) else: raise ValueError("Recorders must be attached to Drivers, Systems, or Solvers.")
def record_iteration_system(self, object_requesting_recording, metadata): """ Record an iteration using system options. Parameters ---------- object_requesting_recording: <System> The System object that wants to record an iteration. metadata : dict Dictionary containing execution metadata (e.g. iteration coordinate). method : str The method that called record_iteration. One of '_apply_linear', '_solve_linear', '_apply_nonlinear,' '_solve_nonlinear'. Behavior varies based on from which function record_iteration was called. """ super(WebRecorder, self).record_iteration_system(object_requesting_recording, metadata) # Inputs inputs_array = [] if self._inputs: for name, value in iteritems(self._inputs): inputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) # Outputs outputs_array = [] if self._outputs: for name, value in iteritems(self._outputs): outputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) # Residuals residuals_array = [] if self._resids: for name, value in iteritems(self._resids): residuals_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_system_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], inputs_array, outputs_array, residuals_array)
def record_iteration_system(self, recording_requester, data, metadata): """ Record data and metadata from a System. Parameters ---------- recording_requester : object Driver in need of recording. data : dict Dictionary containing inputs, outputs, and residuals. metadata : dict Dictionary containing execution metadata. """ inputs = data['i'] outputs = data['o'] residuals = data['r'] # Inputs inputs_array = [] if inputs: for name, value in iteritems(inputs): inputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) # Outputs outputs_array = [] if outputs: for name, value in iteritems(outputs): outputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) # Residuals residuals_array = [] if residuals: for name, value in iteritems(residuals): residuals_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_system_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], inputs_array, outputs_array, residuals_array)
def record_iteration_solver(self, object_requesting_recording, metadata, **kwargs): """ Record an iteration using solver options. Parameters ---------- object_requesting_recording: <Solver> The Solver object that wants to record an iteration. metadata : dict Dictionary containing execution metadata (e.g. iteration coordinate). absolute : float The absolute error of the Solver requesting recording. It is not cached in the Solver object, so we pass it in here. relative : float The relative error of the Solver requesting recording. It is not cached in the Solver object, so we pass it in here. """ super(WebRecorder, self).record_iteration_solver(object_requesting_recording, metadata, **kwargs) outputs_array = [] if self._outputs: for name, value in iteritems(self._outputs): outputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) residuals_array = [] if self._resids: for name, value in iteritems(self._resids): residuals_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_solver_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], self._abs_error, self._rel_error, outputs_array, residuals_array)
def record_iteration_solver(self, recording_requester, data, metadata): """ Record data and metadata from a Solver. Parameters ---------- recording_requester : object Solver in need of recording. data : dict Dictionary containing outputs, residuals, and errors. metadata : dict Dictionary containing execution metadata. """ abs = data['abs'] rel = data['rel'] outputs = data['o'] residuals = data['r'] outputs_array = [] if outputs: for name, value in iteritems(outputs): outputs_array.append({ 'name': name, 'values': self.convert_to_list(value) }) residuals_array = [] if residuals: for name, value in iteritems(residuals): residuals_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_solver_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], abs, rel, outputs_array, residuals_array)
def _pre_run_model_debug_print(self): """ Optionally print some debugging information before the model runs. """ if not self.options['debug_print']: return if not MPI or MPI.COMM_WORLD.rank == 0: header = 'Driver debug print for iter coord: {}'.format( get_formatted_iteration_coordinate()) print(header) print(len(header) * '-') if 'desvars' in self.options['debug_print']: desvar_vals = self.get_design_var_values() if not MPI or MPI.COMM_WORLD.rank == 0: print("Design Vars") if desvar_vals: for name, value in iteritems(desvar_vals): print("{}: {}".format(name, repr(value))) else: print("None") print()
def record_iteration_driver_passing_vars(self, object_requesting_recording, desvars, responses, objectives, constraints, metadata): """ Record an iteration of a driver with the variables passed in. Parameters ---------- object_requesting_recording: <Driver> The Driver object that wants to record an iteration. desvars : dict Dictionary containing design variables. responses : dict Dictionary containing response variables. objectives : dict Dictionary containing objective variables. constraints : dict Dictionary containing constraint variables. metadata : dict Dictionary containing execution metadata (e.g. iteration coordinate). """ # TODO: this code and the same code in record_iteration should be in a separate method if not self._parallel: if MPI and MPI.COMM_WORLD.rank > 0: raise RuntimeError( "Non-parallel recorders should not be recording on ranks > 0" ) self._counter += 1 self._iteration_coordinate = get_formatted_iteration_coordinate() if self.options['record_desvars']: if self._filtered_driver: self._desvars_values = \ {name: desvars[name] for name in self._filtered_driver['des']} else: self._desvars_values = desvars else: self._desvars_values = None # Cannot handle responses yet # if self.options['record_responses']: # if self._filtered_driver: # self._responses_values = \ # {name: responses[name] for name in self._filtered_driver['res']} # else: # self._responses_values = responses # else: # self._responses_values = None if self.options['record_objectives']: if self._filtered_driver: self._objectives_values = \ {name: objectives[name] for name in self._filtered_driver['obj']} else: self._objectives_values = objectives else: self._objectives_values = None if self.options['record_constraints']: if self._filtered_driver: self._constraints_values = \ {name: constraints[name] for name in self._filtered_driver['con']} else: self._constraints_values = constraints else: self._constraints_values = None
def record_iteration_driver(self, recording_requester, data, metadata): """ Record data and metadata from a Driver. Parameters ---------- recording_requester : object Driver in need of recording. data : dict Dictionary containing desvars, objectives, constraints, responses, and System vars. metadata : dict Dictionary containing execution metadata. """ desvars_array = None responses_array = None objectives_array = None constraints_array = None sysincludes_array = None if data['des']: desvars_array = [] for name, value in iteritems(data['des']): desvars_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if data['res']: responses_array = [] for name, value in iteritems(data['res']): responses_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if data['obj']: objectives_array = [] for name, value in iteritems(data['obj']): objectives_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if data['con']: constraints_array = [] for name, value in iteritems(data['con']): constraints_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if data['sys']: sysincludes_array = [] for name, value in iteritems(data['sys']): sysincludes_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_driver_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], desvars_array, responses_array, objectives_array, constraints_array, sysincludes_array)
def record_iteration_driver(self, object_requesting_recording, metadata): """ Record an iteration using the driver options. Parameters ---------- object_requesting_recording: <Driver> The Driver object that wants to record an iteration. metadata : dict Dictionary containing execution metadata (e.g. iteration coordinate). """ desvars_array = None responses_array = None objectives_array = None constraints_array = None desvars_values = None responses_values = None objectives_values = None constraints_values = None if self.options['record_desvars']: if self._filtered_driver: desvars_values = \ object_requesting_recording.get_design_var_values(self._filtered_driver['des']) else: desvars_values = object_requesting_recording.get_design_var_values() if desvars_values: desvars_array = [] for name, value in iteritems(desvars_values): desvars_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if self.options['record_responses']: if self._filtered_driver: responses_values = \ object_requesting_recording.get_response_values(self._filtered_driver['res']) else: responses_values = object_requesting_recording.get_response_values() if responses_values: responses_array = [] for name, value in iteritems(responses_values): responses_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if self.options['record_objectives']: if self._filtered_driver: objectives_values = \ object_requesting_recording.get_objective_values(self._filtered_driver['obj']) else: objectives_values = object_requesting_recording.get_objective_values() if objectives_values: objectives_array = [] for name, value in iteritems(objectives_values): objectives_array.append({ 'name': name, 'values': self.convert_to_list(value) }) if self.options['record_constraints']: if self._filtered_driver: constraints_values = \ object_requesting_recording.get_constraint_values(self._filtered_driver['con']) else: constraints_values = object_requesting_recording.get_constraint_values() if constraints_values: constraints_array = [] for name, value in iteritems(constraints_values): constraints_array.append({ 'name': name, 'values': self.convert_to_list(value) }) iteration_coordinate = get_formatted_iteration_coordinate() self._record_driver_iteration(self._counter, iteration_coordinate, metadata['success'], metadata['msg'], desvars_array, responses_array, objectives_array, constraints_array)