def _serialize_single_option(option): """ Return a json-safe equivalent of the option. The default_noraise function performs the datatype serialization, while this function takes care of attributes specific to options dicts. Parameters ---------- option : object Option to be serialized. Returns ------- object JSON-safe serialized object. """ val = option['value'] if not option['recordable']: serialized_option = 'Not Recordable' elif val is _UNDEFINED: serialized_option = str(val) else: serialized_option = default_noraise(val) return serialized_option
def _get_var_dict(system, typ, name): if name in system._var_discrete[typ]: meta = system._var_discrete[typ][name] is_discrete = True else: if name in system._var_abs2meta['output']: meta = system._var_abs2meta['output'][name] else: meta = system._var_abs2meta['input'][name] name = system._var_abs2prom[typ][name] is_discrete = False var_dict = OrderedDict() var_dict['name'] = name var_dict['type'] = typ if typ == 'output': isimplicit = isinstance(system, ImplicitComponent) var_dict['type'] = 'output' var_dict['implicit'] = isimplicit var_dict['dtype'] = type(meta['value']).__name__ if 'units' in meta: if meta['units'] is None: var_dict['units'] = 'None' else: var_dict['units'] = meta['units'] if 'shape' in meta: var_dict['shape'] = str(meta['shape']) if 'distributed' in meta: var_dict['distributed'] = meta['distributed'] var_dict['is_discrete'] = is_discrete if is_discrete: if isinstance( meta['value'], (int, str, list, dict, complex, np.ndarray)) or MPI is None: var_dict['value'] = default_noraise(system.get_val(name)) else: var_dict['value'] = type(meta['value']).__name__ else: if meta['value'].size < _MAX_ARRAY_SIZE_FOR_REPR_VAL: if MPI: var_dict['value'] = meta['value'] else: var_dict['value'] = _convert_ndarray_to_support_nans_in_json( system.get_val(name)) else: var_dict['value'] = None return var_dict
def _get_var_dict(system, typ, name, is_parallel): if name in system._var_discrete[typ]: meta = system._var_discrete[typ][name] is_discrete = True else: if name in system._var_abs2meta['output']: meta = system._var_abs2meta['output'][name] else: meta = system._var_abs2meta['input'][name] name = system._var_abs2prom[typ][name] is_discrete = False var_dict = {} var_dict['name'] = name var_dict['type'] = typ if typ == 'output': isimplicit = isinstance(system, ImplicitComponent) var_dict['type'] = 'output' var_dict['implicit'] = isimplicit var_dict['dtype'] = type(meta['value']).__name__ if 'units' in meta: if meta['units'] is None: var_dict['units'] = 'None' else: var_dict['units'] = meta['units'] if 'shape' in meta: var_dict['shape'] = str(meta['shape']) if 'distributed' in meta: var_dict['distributed'] = is_distributed = meta['distributed'] else: is_distributed = False if 'surrogate_name' in meta: var_dict['surrogate_name'] = meta['surrogate_name'] var_dict['is_discrete'] = is_discrete if is_discrete: if isinstance(meta['value'], (int, str, list, dict, complex, np.ndarray)) or MPI is None: var_dict['value'] = default_noraise(system.get_val(name)) else: var_dict['value'] = type(meta['value']).__name__ else: if meta['value'].size < _MAX_ARRAY_SIZE_FOR_REPR_VAL: if not MPI: # get the current value var_dict['value'] = _convert_ndarray_to_support_nans_in_json(system.get_val(name)) elif is_parallel or is_distributed: # we can't access non-local values, so just get the initial value var_dict['value'] = meta['value'] var_dict['initial_value'] = True else: # get the current value but don't try to get it from the source, # which could be remote under MPI val = system.get_val(name, from_src=False) var_dict['value'] = _convert_ndarray_to_support_nans_in_json(val) else: var_dict['value'] = None return var_dict
def _get_tree_dict(system, component_execution_orders, component_execution_index, is_parallel=False): """Get a dictionary representation of the system hierarchy.""" tree_dict = OrderedDict() tree_dict['name'] = system.name tree_dict['type'] = 'subsystem' tree_dict['class'] = system.__class__.__name__ tree_dict['expressions'] = None if not isinstance(system, Group): tree_dict['subsystem_type'] = 'component' tree_dict['is_parallel'] = is_parallel if isinstance(system, ImplicitComponent): tree_dict['component_type'] = 'implicit' elif isinstance(system, ExecComp): tree_dict['component_type'] = 'exec' tree_dict['expressions'] = system._exprs elif isinstance(system, (MetaModelStructuredComp, MetaModelUnStructuredComp)): tree_dict['component_type'] = 'metamodel' elif isinstance(system, IndepVarComp): tree_dict['component_type'] = 'indep' elif isinstance(system, ExplicitComponent): tree_dict['component_type'] = 'explicit' else: tree_dict['component_type'] = None component_execution_orders[ system.pathname] = component_execution_index[0] component_execution_index[0] += 1 children = [] for typ in ['input', 'output']: for abs_name in system._var_abs_names[typ]: children.append(_get_var_dict(system, typ, abs_name)) for prom_name in system._var_discrete[typ]: children.append(_get_var_dict(system, typ, prom_name)) else: if isinstance(system, ParallelGroup): is_parallel = True tree_dict['component_type'] = None tree_dict['subsystem_type'] = 'group' tree_dict['is_parallel'] = is_parallel children = [] for s in system._subsystems_myproc: if (s.name != '_auto_ivc'): children.append( _get_tree_dict(s, component_execution_orders, component_execution_index, is_parallel)) if system.comm.size > 1: if system._subsystems_myproc: sub_comm = system._subsystems_myproc[0].comm if sub_comm.rank != 0: children = [] children_lists = system.comm.allgather(children) children = [] for children_list in children_lists: children.extend(children_list) if isinstance(system, ImplicitComponent): if overrides_method('solve_linear', system, ImplicitComponent): tree_dict['linear_solver'] = "solve_linear" tree_dict['linear_solver_options'] = None elif system.linear_solver: tree_dict['linear_solver'] = system.linear_solver.SOLVER options = { k: system.linear_solver.options[k] for k in system.linear_solver.options } tree_dict['linear_solver_options'] = options else: tree_dict['linear_solver'] = "" tree_dict['linear_solver_options'] = None if overrides_method('solve_nonlinear', system, ImplicitComponent): tree_dict['nonlinear_solver'] = "solve_nonlinear" tree_dict['nonlinear_solver_options'] = None elif system.nonlinear_solver: tree_dict['nonlinear_solver'] = system.nonlinear_solver.SOLVER options = { k: system.nonlinear_solver.options[k] for k in system.nonlinear_solver.options } tree_dict['nonlinear_solver_options'] = options else: tree_dict['nonlinear_solver'] = "" tree_dict['nonlinear_solver_options'] = None else: if system.linear_solver: tree_dict['linear_solver'] = system.linear_solver.SOLVER options = { k: system.linear_solver.options[k] for k in system.linear_solver.options } tree_dict['linear_solver_options'] = options else: tree_dict['linear_solver'] = "" tree_dict['linear_solver_options'] = None if system.nonlinear_solver: tree_dict['nonlinear_solver'] = system.nonlinear_solver.SOLVER options = { k: system.nonlinear_solver.options[k] for k in system.nonlinear_solver.options } tree_dict['nonlinear_solver_options'] = options if system.nonlinear_solver.SOLVER == NewtonSolver.SOLVER: tree_dict[ 'solve_subsystems'] = system._nonlinear_solver.options[ 'solve_subsystems'] else: tree_dict['nonlinear_solver'] = "" tree_dict['nonlinear_solver_options'] = None tree_dict['children'] = children options = {} for k in system.options: # need to handle solvers separate because they are classes or instances if k in ['linear_solver', 'nonlinear_solver']: options[k] = system.options[k].SOLVER else: val = system.options._dict[k]['value'] if not system.options._dict[k]['recordable']: options[k] = default_noraise(val) elif val is _UNDEFINED: options[k] = str(val) else: options[k] = val tree_dict['options'] = options if not tree_dict['name']: tree_dict['name'] = 'root' tree_dict['type'] = 'root' return tree_dict
def _get_var_dict(system, typ, name, is_parallel, is_implicit): if name in system._var_abs2meta[typ]: meta = system._var_abs2meta[typ][name] prom = system._var_abs2prom[typ][name] val = meta['val'] is_dist = MPI is not None and meta['distributed'] var_dict = { 'name': prom, 'type': typ, 'dtype': type(val).__name__, 'is_discrete': False, 'distributed': is_dist, 'shape': str(meta['shape']), } if typ == 'output': var_dict['implicit'] = is_implicit vec = system._outputs else: # input if MPI: # for inputs if we're under MPI, we only retrieve the value currently stored # in the input vector and not from the connected source because that source # could be remote. vec = system._inputs else: vec = None # if 'vec' is not None at this point, we can retrieve the value using vec._abs_get_val, # which is a faster call than system.get_val. if meta['units'] is None: var_dict['units'] = 'None' else: var_dict['units'] = meta['units'] if val.size < _MAX_ARRAY_SIZE_FOR_REPR_VAL: if not MPI: # get the current value if vec: var_dict['val'] = _convert_ndarray_to_support_nans_in_json( vec._abs_get_val(name, flat=False)) else: var_dict['val'] = _convert_ndarray_to_support_nans_in_json( system.get_val(prom)) elif is_parallel or is_dist: # we can't access non-local values, so just get the initial value var_dict['val'] = val var_dict['initial_value'] = True else: # get the current value but don't try to get it from the source, # which could be remote under MPI if vec: var_dict['val'] = _convert_ndarray_to_support_nans_in_json( vec._abs_get_val(name, flat=False)) else: var_dict['val'] = _convert_ndarray_to_support_nans_in_json( system.get_val(prom, from_src=False)) else: var_dict['val'] = None else: # discrete meta = system._var_discrete[typ][name] val = meta['val'] var_dict = { 'name': name, 'type': typ, 'dtype': type(val).__name__, 'is_discrete': True, } if MPI is None or isinstance( val, (int, str, list, dict, complex, np.ndarray)): var_dict['val'] = default_noraise(system.get_val(name)) else: var_dict['val'] = type(val).__name__ if 'surrogate_name' in meta: var_dict['surrogate_name'] = meta['surrogate_name'] return var_dict