Method: OpenStudio::Workflow::Util::EnergyPlus#call_energyplus

Defined in:
lib/openstudio/workflow/util/energyplus.rb

#call_energyplus(run_directory, energyplus_path = nil, output_adapter = nil, logger = nil, workflow_json = nil) ⇒ Void

Configures and executes the EnergyPlus simulation and checks to see if the simulation was successful

Parameters:

  • run_directory (String)

    Directory to execute the EnergyPlus simulation in. It is assumed that this directory already has the IDF and weather file in it

  • energyplus_path (String) (defaults to: nil)

    (nil) Optional path to override the default path associated with the OpenStudio package being used

  • output_adapter (Object) (defaults to: nil)

    (nil) Optional output adapter to update

  • logger (Object) (defaults to: nil)

    (nil) Optional logger, will log to STDOUT if none provided

Returns:

  • (Void)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/openstudio/workflow/util/energyplus.rb', line 127

def call_energyplus(run_directory, energyplus_path = nil, output_adapter = nil, logger = nil, workflow_json = nil)
  logger ||= ::Logger.new(STDOUT) unless logger

  current_dir = Dir.pwd
  energyplus_path ||= find_energyplus
  logger.info "EnergyPlus path is #{energyplus_path}"
  energyplus_files, energyplus_exe, expand_objects_exe = prepare_energyplus_dir(run_directory, logger, energyplus_path)
  Dir.chdir(run_directory)
  logger.info "Starting simulation in run directory: #{Dir.pwd}"

  if !@options[:skip_expand_objects]
    command = popen_command("\"#{expand_objects_exe}\"")
    logger.info "Running command '#{command}'"
    File.open('stdout-expandobject', 'w') do |file|
      ::IO.popen(command) do |io|
        while (line = io.gets)
          file << line
        end
      end
    end

    # Check if expand objects did anything
    if File.exist? 'expanded.idf'
      FileUtils.mv('in.idf', 'pre-expand.idf', force: true) if File.exist?('in.idf')
      FileUtils.mv('expanded.idf', 'in.idf', force: true)
    end
  end

  # create stdout
  command = popen_command("\"#{energyplus_exe}\" 2>&1")
  logger.info "Running command '#{command}'"
  File.open('stdout-energyplus', 'w') do |file|
    ::IO.popen(command) do |io|
      while (line = io.gets)
        file << line
        output_adapter&.communicate_energyplus_stdout(line)
      end
    end
  end
  r = $?

  logger.info "EnergyPlus returned '#{r}'"
  unless r.to_i.zero?
    logger.warn 'EnergyPlus returned a non-zero exit code. Check the stdout-energyplus log.'
  end

  if File.exist? 'eplusout.err'
    eplus_err = File.read('eplusout.err').force_encoding('ISO-8859-1').encode('utf-8', replace: nil)

    if workflow_json
      begin
        if !@options[:fast]
          workflow_json.setEplusoutErr(eplus_err)
        end
      rescue StandardError => e
        # older versions of OpenStudio did not have the setEplusoutErr method
      end
    end

    if eplus_err =~ /EnergyPlus Terminated--Fatal Error Detected/
      raise 'EnergyPlus Terminated with a Fatal Error. Check eplusout.err log.'
    end
  end

  if File.exist? 'eplusout.end'
    f = File.read('eplusout.end').force_encoding('ISO-8859-1').encode('utf-8', replace: nil)
    warnings_count = f[/(\d*).Warning/, 1]
    error_count = f[/(\d*).Severe.Errors/, 1]
    logger.info "EnergyPlus finished with #{warnings_count} warnings and #{error_count} severe errors"
    if f =~ /EnergyPlus Terminated--Fatal Error Detected/
      raise 'EnergyPlus Terminated with a Fatal Error. Check eplusout.err log.'
    end
  else
    raise 'EnergyPlus failed and did not create an eplusout.end file. Check the stdout-energyplus log.'
  end
rescue StandardError => e
  log_message = "#{__FILE__} failed with #{e.message}, #{e.backtrace.join("\n")}"
  logger.error log_message
  raise log_message
ensure
  logger.info "Ensuring 'clean' directory"
  clean_directory(run_directory, energyplus_files, logger)

  Dir.chdir(current_dir)
  logger.info 'EnergyPlus Completed'
end