First time here? Check out the Help page!

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 9 years ago

gg_student's avatar

updated 9 years ago

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

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
5

answered 9 years ago

gg_student's avatar

updated 9 years ago

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));
        }
Preview: (hide)
link

Comments

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

Julien Marrec's avatar Julien Marrec  ( 9 years ago )

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

gg_student's avatar gg_student  ( 9 years ago )

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  ( 6 years ago )

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

reza3ker's avatar reza3ker  ( 6 years ago )

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

Julien Marrec's avatar Julien Marrec  ( 6 years ago )
4

answered 9 years ago

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

Preview: (hide)
link

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  ( 9 years ago )

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  ( 9 years ago )

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

gg_student's avatar gg_student  ( 9 years ago )

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

mdengusiak's avatar mdengusiak  ( 4 years ago )

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: 9 years ago

Seen: 1,253 times

Last updated: Jan 22 '16