Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

How do I run a simulation by using c#?

asked 2016-01-12 09:24:26 -0500

gg_student gravatar image

updated 2016-01-22 07:02:41 -0500

It is possible to run the simulation by using c# API? Instead to open the model and click on the run tab, I want to run the simulation like this model.runSimulation(). I need this to get the sqlFile from EnergyPlus. Otherwise the Path "/Model/Run/5-EnergyPlus-0" is emtpy.

Thanks for helping

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-01-22 04:36:42 -0500

gg_student gravatar image

updated 2016-01-22 06:25:02 -0500

Here is my translation of the ruby project to c#.

  • Step 1: Variables
  • Step 2: Create IDF
  • Step 3: Start RunManager
  • Step 4: Found EnergyPlus.exe File
  • Step 5: Simulation
  • Step 6: Read SQL File

       //_path must be your Folder of Saving the Project z.B. C://OpenStudio//Projects//...
        string e _runDirStr =_path + "\\run";
        string _sqlPathStr = _runDirStr + "\\EnergyPlus\\eplusout.sql";
        string _epDirStr = "C:\\EnergyPlusV8-4-0";
        string _epPathStr = _epDirStr + "\\energyplus.exe";
        string idd_pathStr = _epDirStr + "\\Energy+.idd";
        string  outputPathStr = _runDirStr;
        Path _epPath = new Path(_epPathStr);
        Path _sqlPath = new Path(_sqlPathStr);
        SimulationControl control = _model.getSimulationControl();
        control.setRunSimulationforSizingPeriods(false); //do not use DesignDays
        control.setRunSimulationforWeatherFileRunPeriods(true); //use Weatherfile
        //Export IDF
        string idf_name, osm_name;
        idf_name = "simulation_in.idf";
        osm_name = "simulation_ThesisModel.osm";
        EnergyPlusForwardTranslator translator = new EnergyPlusForwardTranslator();
        Workspace idf = translator.translateModel(_model);
        //IdfFile idf_file =  _model.toIdfFile();
        _idfPathStr = _runDirStr + "\\" + idf_name;
        osmPathStr = _runDirStr + "\\" + osm_name; Path(_idfPathStr),true); Path(osmPathStr), true);
        Path runManagerPath = new Path(_runDirStr + "\\simulation_runmanager.db");
        RunManager run = new RunManager(runManagerPath, true, false, false, false);
        ToolInfo tool = new ToolInfo(_epPath);
        Job job = JobFactory.createEnergyPlusJob(tool, new Path(idd_pathStr), new Path(_idfPathStr), _weatherFilePath, new Path(outputPathStr));
        run.enqueue(job, true);
        //start Simulation
        //run.runWorkflow(new Path(job.toJSON()),new Path(_idfPath),new Path(_runDir), new Tools(),"");
        Workflow sim = new Workflow(job);
        sim.create(new Path(outputPathStr), new Path(new Path(_idfPathStr)));
        //read results
        if (System.IO.File.Exists(_sqlPathStr))
            _model.setSqlFile(new SqlFile(_sqlPath));
edit flag offensive delete link more


Great job, we should accept your answer but it can't be done until January 29th

Julien Marrec gravatar imageJulien Marrec ( 2016-01-22 06:26:08 -0500 )edit

Thank you. I will do it at January 29th.

gg_student gravatar imagegg_student ( 2016-01-22 14:25:08 -0500 )edit

hi, I used your code, everything was fine. Visual Studio doesn't find two classes. " RunManager , Workflow , Job " Where can I find their assemblies?

reza3ker gravatar imagereza3ker ( 2018-09-08 05:06:56 -0500 )edit

Where can I find examples of OpenStudio API that are written by C #?

reza3ker gravatar imagereza3ker ( 2018-09-08 05:09:49 -0500 )edit

@reza3ker, you should open your own question for clarity.

Julien Marrec gravatar imageJulien Marrec ( 2018-09-10 03:42:51 -0500 )edit

answered 2016-01-12 09:39:35 -0500

I suggest you check out the DOE Prototype project. It's written in Ruby but it's readable even if you know nothing about Ruby. You just have to transcribe this bit where they launch a run and get some results out of the sql file to C#: Prototype.Model.rb

edit flag offensive delete link more


Thank you. That is it! I could translate it to c# and know I can run the simulation.

gg_student gravatar imagegg_student ( 2016-01-13 04:49:38 -0500 )edit

Can you post an answer with your C# translation in there so that it can help others in the future? Or if you don't want, mark this answer as accepted please

Julien Marrec gravatar imageJulien Marrec ( 2016-01-14 09:23:11 -0500 )edit

I already not finish know. But when I have the full translation, I will post it!

gg_student gravatar imagegg_student ( 2016-01-14 09:49:38 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer


Question Tools

1 follower


Asked: 2016-01-12 09:24:26 -0500

Seen: 369 times

Last updated: Jan 22 '16