def load_model(file_name): ''' load the model :param file: the location of the .vpm file to be loaded. :exception: raises a :class:`~EMAExceptions.VensimError` if the model cannot be loaded. .. note: only works for .vpm files ''' debug("executing COMMAND: SIMULATE>SPECIAL>LOADMODEL|"+file_name) try: command(r"SPECIAL>LOADMODEL|"+file_name) except VensimWarning as w: warning(str(w)) raise VensimError("vensim file not found")
def _run_case(self, case): """ Method for running an instantiated model structure. This method should always be implemented. :param case: keyword arguments for running the model. The case is a dict with the names of the uncertainties as key, and the values to which to set these uncertainties. .. note:: This method should always be implemented. """ for key, value in case.iteritems(): try: self.netlogo.command(self.command_format.format(key, value)) except jpype.JavaException as e: warning('variable {0} throws exception: {}'.format( (key, str(e))))
def get_data(filename, varname, step=1): ''' Retrieves data from simulation runs or imported data sets. :param filename: the name of the .vdf file that contains the data :param varname: the name of the variable to retrieve data on :param step: steps used in slicing. Defaults to 1, meaning the full recored time series is returned. :return: an array with the values for varname over the simulation ''' vval = [] try: vval, tval = vensimDLLwrapper.get_data(filename, varname) except VensimWarning as w: warning(str(w)) return vval
def run_simulation(file_name): ''' Convenient function to run a model and store the results of the run in the specified .vdf file. The specified output file will be overwritten by default :param file: the location of the outputfile :exception: raises a :class:`~EMAExceptions.VensimError` if running the model failed in some way. ''' try: debug(" executing COMMAND: SIMULATE>RUNNAME|"+file_name+"|O") command("SIMULATE>RUNNAME|"+file_name+"|O") debug(r"MENU>RUN|o") command(r"MENU>RUN|o") except VensimWarning as w: warning((str(w))) raise VensimError(str(w))
def set_value(variable, value): ''' set the value of a variable to value current implementation only works for lookups and normal values. In case of a list, a lookup is assumed, else a normal value is assumed. See the DSS reference supplement, p. 58 for details. :param variable: name of the variable to set. :param value: the value for the variable. **note**: the value can be either a list, or an float/integer. If it is a list, it is assumed the variable is a lookup. ''' if type(value) == types.ListType: command(r"SIMULATE>SETVAL|"+variable+"("+ str(value)[1:-1] + ")") else: try: command(r"SIMULATE>SETVAL|"+variable+"="+str(value)) except VensimWarning: warning('variable: \'' +variable+'\' not found')
def run_model(self, case): """ Method for running an instantiated model structure. This method should always be implemented. :param case: keyword arguments for running the model. The case is a dict with the names of the uncertainties as key, and the values to which to set these uncertainties. .. note:: This method should always be implemented. """ for key, value in case.iteritems(): try: self.netlogo.command(self.command_format.format(key, value)) except jpype.JavaException as e: warning('variable {0} throws exception: {}'.format((key, str(e)))) debug("model parameters set successfully") # finish setup and invoke run self.netlogo.command("setup") commands = [] fns = {} for outcome in self.outcomes: if outcome.time: name = outcome.name fn = r'{0}{3}{1}{2}'.format(self.working_directory, name, ".txt", os.sep) fns[name] = fn fn = '"{}"'.format(fn) fn = fn.replace(os.sep, '/') if self.netlogo.report('is-agentset? {}'.format(name)): # if name is name of an agentset, we # assume that we should count the total number of agents nc = r'{2} {0} {3} {4} {1}'.format(fn, name, "file-open", 'file-write', 'count') else: # it is not an agentset, so assume that it is # a reporter / global variable nc = r'{2} {0} {3} {1}'.format(fn, name, "file-open", 'file-write') commands.append(nc) c_start = "repeat {} [".format(self.run_length) c_end = "go ]" c_middle = " ".join(commands) command = " ".join((c_start, c_middle, c_end)) self.netlogo.command(command) # after the last go, we have not done a write for the outcomes # so we do that now self.netlogo.command(c_middle) self.netlogo.command("file-close-all") self._handle_outcomes(fns)
base vensim warning ''' pass class VensimError(EMAError): ''' base Vensim error ''' pass try: vensim = ctypes.windll.vendll32 except (WindowsError, AttributeError): sys.stderr.write("vensim dll not found, vensim functionality not available\n") warning("vensim dll not found, vensim functionality not available") del sys def be_quiet(quietflag): ''' this allows you to turn off the work in progress dialog that Vensim displays during simulation and other activities, and also prevent the appearance of yes or no dialogs. use 0 for normal interaction, 1 to prevent the appearance of any work in progress windows, and 2 to also prevent the appearance of any interrogative dialogs' ''' if quietflag > 2: raise VensimError("incorrect value for quietflag")
from expWorkbench import debug, info, warning from expWorkbench.ema_exceptions import EMAError __all__ = ['NetLogoException', 'NetLogoLink'] if sys.platform=='win32': NETLOGO_HOME = r'C:\Program Files (x86)\NetLogo 5.0.4' jar_separator = ";" # jars are separated by a ; on Windows elif sys.platform=='darwin': jar_separator = ":" # jars are separated by a : on MacOS NETLOGO_HOME = r'/Applications/NetLogo 5.0.4' else: # TODO should raise and exception which is subsequently cached and # transformed into a a warning just like excel and vensim warning('netlogo support not available') PYNETLOGO_HOME = os.path.dirname(os.path.abspath(__file__)) class NetLogoException(Exception): pass class NetLogoLink(): def __init__(self, gui=False, thd=False): ''' Create a link with netlogo. Underneath, the netlogo jvm is started throuhg jpype.