def add_from_same(self, obj, inputs_from="obj", update_inputs=True): if not hasattr(self, "inputs"): raise ModelError("self does not contain attribute: 'inputs'") if inputs_from == "obj": if hasattr(obj, "inputs"): inputs_list = obj.inputs else: raise ModelError("obj does not contain attribute: 'inputs'") else: inputs_list = self.inputs for item in inputs_list: if hasattr(obj, item): setattr(self, item, getattr(obj, item)) if update_inputs and item not in self.inputs: self.inputs.append(item)
def get_pad_pos_in_width_dir(self, i): """ Determines the centre position of the ith pad in the width direction. Parameters ---------- i: int Ith number of pad in width direction (0-indexed) """ if i >= self.n_pads_w: raise ModelError("pad index out-of-bounds") if self._pad_pos_in_width_dir is None: raise ModelError('pad positions not set') return self._pad_pos_in_width_dir[i]
def set_column_prop(self, prop, values, repeat="up", sections=None): """ Specify the properties of the columns Parameters ---------- prop: str Name of property that values should be assigned to values: value or array_like Value or list of values to be assigned repeat: str If 'up' then duplicate up the structure, if 'all' the duplicate for all columns """ si = 0 if sections is not None: si = 1 values = np.array(values) if repeat == "up": assert len(values.shape) == 1 + si values = [values for ss in range(self.n_storeys)] elif repeat == 'all': assert len(values.shape) == 0 + si values = [[values for i in range(self.n_cols)] for ss in range(self.n_storeys)] else: assert len(values.shape) == 2 + si if len(values[0]) != self.n_cols: raise ModelError("column props does not match n_cols (%i)." % self.n_cols) for ss in range(self.n_storeys): for i in range(self.n_cols): self._columns[ss][i].set_section_prop(prop, values[ss][i], sections=sections)
def set_storey_masses_by_pressure(self, stresses): if hasattr(stresses, "length"): if len(stresses) != self.n_storeys: raise ModelError("Length of defined storey pressures: {0}, " "must equal number of stories: {1}".format( len(stresses), self.n_storeys)) self.storey_masses = stresses * self.floor_area / self._g else: self.storey_masses = stresses * np.ones( self.n_storeys) * self.floor_area / self._g
def mass_density(self, value): if value is None or value == "": return density = self._calc_mass_density() if density is not None and not np.isclose( density, value, rtol=self._tolerance): raise ModelError("Density inconsistent with set mass") self._mass_density = float(value) self._add_to_stack('mass_density', float(value)) mass = self._calc_mass() if mass is not None and not ct.isclose(mass, self.mass): self.mass = mass
def mass(self, value): if value is None or value == "": return mass = self._calc_mass() if mass is not None and not ct.isclose( mass, value, rel_tol=self._tolerance): raise ModelError("Mass inconsistent with set density") self._mass = float(value) self._add_to_stack('mass', self._mass) density = self._calc_mass_density() if density is not None and not ct.isclose( density, self.density, rel_tol=self._tolerance): self.mass_density = density
def to_dict(self, **kwargs): outputs = OrderedDict() skip_list = [] for item in self.inputs: if item not in skip_list: value = self.__getattribute__(item) if "_id" in item and value is None: raise ModelError( "Cannot export system with %s set to None" % item) if item not in skip_list: value = self.__getattribute__(item) outputs[item] = sf.collect_serial_value(value) return outputs
def oop_axis(self, oop_axis): if oop_axis is None: self._ip_axis = None self._oop_axis = None return elif oop_axis == 'width': self._ip_axis = 'length' elif oop_axis == 'length': self._ip_axis = 'width' else: raise ModelError( "oop_axis must be either 'width', 'length' or None") self._oop_axis = oop_axis self._add_to_stack('oop_axis', oop_axis)
def build_id2hash_dict(self): for mtype in self.unordered_models: if mtype not in self.id2hash_dict: # Catch any custom objects self.id2hash_dict[mtype] = OrderedDict() for unique_hash in self.unordered_models[mtype]: if self.reset_ids is False: obj_id = self.unordered_models[mtype][unique_hash]['id'] if obj_id in self.id2hash_dict[mtype]: raise ModelError( 'Duplicate id: {0} for model type: {1}'.format( obj_id, mtype)) else: obj_id = len(self.id2hash_dict[mtype]) + 1 self.id2hash_dict[mtype][obj_id] = unique_hash
def add_to_dict(self, an_object, export_none=False, extras=None): """ Convert models to json serialisable output :param an_object: An instance of a model object :param extras: A dictionary of extra variables that should be :return: """ if an_object.id is None and self.reset_ids is False: raise ModelError( "id must be set on object before adding to output.") if hasattr(an_object, "base_type"): mtype = an_object.base_type elif hasattr(an_object, "type"): if an_object.type in standard_types: mtype = an_object.type else: mtype = "custom_type" else: raise ModelError( "Object does not have attribute 'base_type' or 'type', cannot add to output." ) if mtype not in self.unordered_models: # Catch any custom objects self.unordered_models[mtype] = {} if hasattr(an_object, "add_to_dict"): an_object.add_to_dict(self.unordered_models, export_none=export_none) elif hasattr(an_object, "to_dict"): self.unordered_models[mtype][ an_object.unique_hash] = an_object.to_dict( compression=self.compression, export_none=export_none) else: raise ModelError( "Object does not have method 'to_dict', cannot add to output.")
def bay_lengths(self, bay_lengths): if len(bay_lengths) != self.n_bays: raise ModelError( "bay_lengths does not match number of bays (%i)." % self.n_bays) self._bay_lengths = np.array(bay_lengths)
def interstorey_heights(self, heights): if len(heights) != self.n_storeys: raise ModelError( "Specified heights must match number of storeys (%i)." % self.n_storeys) self._interstorey_heights = np.array(heights)