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

How do I run a simulation by using c#?

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

gg_student's avatar

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

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
5

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

gg_student's avatar

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

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);
    
        System.IO.Directory.CreateDirectory(_path);
        System.IO.Directory.CreateDirectory(_runDirStr);
    
        SimulationControl control = _model.getSimulationControl();
        control.setDoPlantSizingCalculation(true);
        control.setDoZoneSizingCalculation(true);
        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;
        idf.save(new Path(_idfPathStr),true);
        _model.save(new Path(osmPathStr), true);
    
        //Runmanager
        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);
        run.setPaused(false);
        run.workPending();
    
        //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

Comments

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

Julien Marrec's avatar Julien Marrec  ( 2016-01-22 06:26:08 -0600 )edit

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

gg_student's avatar gg_student  ( 2016-01-22 14:25:08 -0600 )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's avatar reza3ker  ( 2018-09-08 05:06:56 -0600 )edit

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

reza3ker's avatar reza3ker  ( 2018-09-08 05:09:49 -0600 )edit

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

Julien Marrec's avatar Julien Marrec  ( 2018-09-10 03:42:51 -0600 )edit
4

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

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

Comments

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

gg_student's avatar gg_student  ( 2016-01-13 04:49:38 -0600 )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's avatar Julien Marrec  ( 2016-01-14 09:23:11 -0600 )edit

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

gg_student's avatar gg_student  ( 2016-01-14 09:49:38 -0600 )edit

@gg_student did you finished your translation? would be great to see some example

mdengusiak's avatar mdengusiak  ( 2021-01-28 06:28:34 -0600 )edit

Your Answer

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

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

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

Seen: 1,200 times

Last updated: Jan 22 '16