[docs]defreadConfigFile(filename):''' Read a configuration file in both, YAML or penRed internal format, and returns the associated dictionary. '''try:f=open(configFile)d=yaml.load(f,Loader=yaml.SafeLoader)returndexcept:yamlstr=simulation.configFile2YAML(filename)returnyaml.safe_load(yamlstr)
[docs]defwriteConfigFile(filename,d):''' Write a configuration file in penRed internal format, from the data stored in the dictionary. '''configText=simulation.dict2SectionString(d)withopen(filename,"w")asf:f.write(configText)
[docs]defrunFromFile(configFile="config.in",statusTime=20,configLog="config.log",simulationLog="simulation.log"):''' Configures and runs a simulation from the specified file. Reports the simulation status periodically. '''#Set logssimulation.setConfigurationLog(str(configLog))simulation.setSimulationLog(str(simulationLog))#Create simulation objectsimu=simulation.create()#Try to get the configuration from yaml or penred internal formattry:f=open(configFile)d=yaml.load(f,Loader=yaml.SafeLoader)simu.configure(d)except:simu.configFromFile(configFile)print("Configuration set\n")#Start the simulation asynchronouslysimu.simulate(True)#Simulation started, check status every 30 secondsprint("Simulation started\n")whilesimu.isSimulating():try:time.sleep(statusTime)except:time.sleep(20)status=simu.stringifyStatus()foreinstatus:print(e)print("Simulation finished")