public IModelState LoadPersistentState(string persistentStateFile) { VectorJ2N x; double persistentTimeStamp; int timestep; try { if (!File.Exists(persistentStateFile)) { throw new Exception("Could not find file for saved state:" + persistentStateFile); } // read state and time from file StreamReader buff = new StreamReader(persistentStateFile); // Read and parse first line string line = buff.ReadLine(); string[] keyValuePair = line.Split(new[] { '=' }); // expect eg x=[1.0,2.0,3.0] x = new VectorJ2N(keyValuePair[1]); // Read and parse second line line = buff.ReadLine(); keyValuePair = line.Split(new[] { '=' }); // expect eg t=1.0 persistentTimeStamp = Double.Parse(keyValuePair[1]); // Read and parse third line line = buff.ReadLine(); keyValuePair = line.Split(new[] { '=' }); // expect eg timestep=3 timestep = Int32.Parse(keyValuePair[1]); buff.Close(); } catch (IOException e) { ResultsJ2N.PutMessage("Exception: " + e.Message); throw new Exception("Error reading restart file for model"); } // set state and time SavedState saveState = new SavedState(); saveState.time = persistentTimeStamp; saveState.timestep = timestep; saveState.state = x; return(saveState); }
public void Initialize(string workingDir, string configstring) { // // GENERIC PART // // Instance counter thisInstanceNumber = nextinstanceNumber; nextinstanceNumber++; //now parse configuration // // <?xml version="1.0" encoding="UTF-8"?> // <oscillatorConfig> //<simulationTimespan>[0.0,0.05,10.0]</simulationTimespan> //<parameters names="t_damp,omega">[8.0,1.5708]</parameters> //<parameterUncertainty names="t_damp,omega">[1.0,0.1257]</parameterUncertainty> //<systemNoise>{[0.0,0.0],[0.3,0.3]}</systemNoise> //<initialState>[0.8,0.0]</initialState> //<initialStateUncertainty>[0.8,0.8]</initialStateUncertainty> //</oscillatorConfig> // ResultsJ2N.PutMessage("configstring = " + configstring); conf = new ConfigTreeJ2N(workingDir, configstring); // // parse simulationTimespan // // <simulationTimespan>[0.0,0.05,10.0]</simulationTimespan> string timespanstring = conf.GetContentstring("simulationTimespan"); if (timespanstring != null) { IVector timespanVector = new VectorJ2N(timespanstring); AddOrSetParameter(StartTimeId, timespanVector.GetValue(0)); AddOrSetParameter(TimeStepId, timespanVector.GetValue(1)); AddOrSetParameter(StopTimeId, timespanVector.GetValue(2)); ResultsJ2N.PutMessage("simulationTimespan =" + timespanVector); } // start at initial time t = parameterValues[StartTimeId]; timeStep = 0; //counter starts at 0 // // parse parameters // string namestring = conf.GetAsstring("parameters@names", ""); string parstring = conf.GetContentstring("parameters"); if (parstring != null) { IVector parValues = new VectorJ2N(parstring); ResultsJ2N.PutMessage("parameters@names =" + namestring + " parameters=" + parValues); string[] names = namestring.Split(new[] { ',' }); for (int i = 0; i < names.Length; i++) { if (!parameterValues.ContainsKey(names[i])) { parameterValues.Add(names[i], parValues.GetValue(i)); } else { ResultsJ2N.PutMessage("parameter " + names[i] + " was not recognized. Value was ignored."); } } } CreateExchangeItems(); AddExchangeItemsAfterInitialisation(); }