static void Main(string[] args) { //stest = new sinter_SimAspen(); String configFileName = args[0]; String defaultsfile = null; String outfilename = null; StreamReader defaultsStream = null; if (args.Length == 2) { outfilename = args[1]; } else if (args.Length == 3) { defaultsfile = args[1]; outfilename = args[2]; defaultsStream = new StreamReader(defaultsfile); } String workingDir = Path.GetDirectoryName(configFileName); //this function returns 0 if no error //another integer for errors StreamReader inFileStream = new StreamReader(configFileName); string setupString = ""; setupString = inFileStream.ReadToEnd(); inFileStream.Close(); stest = sinter_Factory.createSinter(setupString); stest.workingDir = workingDir; stest.openSim(); //connect to aspen stest.Vis = false; stest.dialogSuppress = true; string defaultsjson; if (defaultsfile != null) { try { defaultsjson = defaultsStream.ReadToEnd(); JObject defaultsDict = JObject.Parse(defaultsjson); stest.sendInputs(defaultsDict); } catch (Exception ex) { Console.WriteLine("Exception reading input file! " + ex.Message); } finally { defaultsStream.Close(); } } else { stest.initializeDefaults(); //Get inputs for defaults } stest.sendInputsToSim(); stest.runSim(); sinter.sinter_AppError runStatus = stest.runStatus; if (runStatus != 0) { throw new System.IO.IOException("Run Failed, defaults are invalid!"); } stest.initializeUnits(); stest.initializeDefaults(); //Get inputs for defaults stest.recvOutputsFromSim(); //Out outputs for canonical values Dictionary <String, Object> outputDict = generateConfigDictionary(stest); JsonSerializerSettings jss = new JsonSerializerSettings(); jss.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; string jsonOutput = JsonConvert.SerializeObject(outputDict, Formatting.Indented, jss); StreamWriter outStream = new StreamWriter(outfilename); outStream.WriteLine(jsonOutput); outStream.Close(); stest.closeSim(); Console.WriteLine("CONVERSION FINISHED. PRESS ANY KEY TO END PROGRAM."); Console.ReadLine(); }
private void doSimulation(String sinterconf, String defaultsfile, String infilename, String outfilename, String canonicalOutputFilename) { String workingDir = Path.GetDirectoryName(sinterconf); StreamReader defaultsStream = null; if (defaultsfile != null) { defaultsStream = new StreamReader(defaultsfile); } StreamReader inStream = new StreamReader(infilename); StreamWriter outStream = new StreamWriter(outfilename); StreamReader canonOutStream = new StreamReader(canonicalOutputFilename); //this function returns 0 if no error //another integer for errors StreamReader inFileStream = new StreamReader(sinterconf); string setupString = ""; setupString = inFileStream.ReadToEnd(); inFileStream.Close(); //ISimulation stest = sinter_Factory.createSinter(setupString); //Need to change this to setup file contents string ISimulation stest = sinter_Factory.createSinter(setupString); //sinter_SetupFile thisSetupFile = sinter_SetupFile.determineFileTypeAndParse(setupString); //ISimulation stest = new sinter_SimExcel(); //stest.setupFile.parseFile(setupString); //((sinter_Sim)stest).makeIOTree(); Debug.WriteLine("SINTER: " + stest.GetType().Name, GetType().Name); stest.workingDir = workingDir; //stest.readSetup(sinterconf); //Read the setup file also opens sim stest.openSim(); //connect to aspen stest.Vis = false; Console.WriteLine(stest.Vis); // stest.Layout(); //figure out spreadsheet layout stest.dialogSuppress = true; stest.resetSim(); //The console version reads in json, the actual version just pulls the dictionary from the database string injson = ""; string defaultsjson = ""; string canonOutJson = ""; JObject inputDict = null; JObject defaultsDict = null; JObject canonOutDict = null; try { injson = inStream.ReadToEnd(); inputDict = JObject.Parse(injson); // JsonConvert.DeserializeObject<Dictionary<String, Object>>(injson); canonOutJson = canonOutStream.ReadToEnd(); canonOutDict = JObject.Parse(canonOutJson); // JsonConvert.DeserializeObject<Dictionary<String, Object>>(injson); if (defaultsStream != null) { defaultsjson = defaultsStream.ReadToEnd(); defaultsDict = JObject.Parse(defaultsjson); // Convert.DeserializeObject<Dictionary<String, Object>>(defaultsjson); } } catch (Exception ex) { Console.WriteLine("Exception reading input file! " + ex.Message); } finally { inStream.Close(); } if (defaultsDict != null) { stest.sendDefaults(defaultsDict); } stest.sendInputs(inputDict); stest.sendInputsToSim(); stest.runSim(); stest.recvOutputsFromSim(); sinter.sinter_AppError runStatus = stest.runStatus; Console.WriteLine("RunStatus: {0}", (int)runStatus); Console.WriteLine("Errors:"); string[] errorList = stest.errorsBasic(); foreach (string error in errorList) { Console.Write(error); } Console.WriteLine(); Console.WriteLine("Warnings:"); string[] warnList = stest.warningsBasic(); foreach (string error in warnList) { Console.Write(error); } Console.WriteLine(); JObject outputDict = stest.getOutputs(); Debug.WriteLine("Output: " + outputDict, GetType().Name); JsonSerializerSettings jss = new JsonSerializerSettings(); jss.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; string jsonOutput = JsonConvert.SerializeObject(outputDict, Formatting.Indented, jss); outStream.WriteLine(jsonOutput); stest.closeSim(); outStream.Close(); JObject outTok = (JObject)outputDict["outputs"]; JObject canOutTok = (JObject)canonOutDict["outputs"]; Assert.IsTrue(outputsEqual(outTok, canOutTok)); // Assert.IsTrue(outputDict.Equals(canonOutDict)); }