def __init__(self, filename, parameters={}, verbose=False): """ Loads a model from a DLL, and initialises it :param filename: The path of the file to load the model from :param parameters: Dictionary containing parameters of the model to set. The keys of this dictionary should be strings containing the names of the parameters, and the values should be strings, bools or ints containing the values of the parameters :param verbose: If True, extra debugging information is printed """ self.__filename = filename self._loader = CADI.CADIDll() self._loader.openDll(filename) self._broker = self._loader.CreateCADIBroker() factories = self._broker.GetSimulationFactories() factory = factories[0] callbacks_enable = '\001' * CADI.CADI_SIM_CB_Count errors = [] @CADIcallback def Error(severity, errorCode, erroneousParameterId, message): """ Error(severity, errorCode, erroneousParameterId, message) Recieves error information during instantiation """ errors.append("CADIErrorCallback::Error(severity=%s, error=%s, paramId=%d, message=%s)" % (CADIFACT_SEVERITY2str(severity), CADIFACT_ERROR2str(errorCode), erroneousParameterId, repr(message))) parameter_dict = ParameterDict(factory.GetParameterInfos()) for (key, value) in parameters.iteritems(): # This will raise an exception for unknown parameters parameter_dict[key] = value # This will raise an exception for parameters set to a value of # the wrong type or out of range parameter_array = parameter_dict._get_parameter_array() self._sim_callback = Model.SimulationCallback(self, verbose) self._sim_error_callback = PyCADIErrorCallback(Error) simulation = factory.Instantiate(parameter_array, self._sim_error_callback, self._sim_callback, callbacks_enable) if simulation is None: raise TargetError("Instantiation failed: " + ", ".join(errors)) factory.Release() Model.__init__(self, simulation, verbose)
def get_model_parameters(cls, filename): """Get a dictionary containing the default parameters for the model :param filename: The path of the file to load the model parameters from """ loader = CADI.CADIDll() loader.openDll(filename) broker = loader.CreateCADIBroker() factory = broker.GetSimulationFactories()[0] return ParameterDict(factory.GetParameterInfos())