class URBANopt::Scenario::SimulationDirOSW

Attributes

feature_id[R]
mapper_class[R]

Public Class Methods

new(scenario, features, feature_names, mapper_class) click to toggle source

SimulationDirOSW creates a OSW file to simulate features, a SimulationMapperBase is invoked to translate features to OSW.

parameters:
  • scenario - ScenarioBase - Scenario containing this SimulationFileBase.

  • features - Array - Array of Features this SimulationFile represents.

  • feature_names - Array - Array of scenario specific names for these Features.

  • mapper_class - String - Name of class derived frmo SimulationMapperBase used to translate feature to simulation OSW.

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 19
def initialize(scenario, features, feature_names, mapper_class)
  super(scenario, features, feature_names)

  if features.size != 1
    raise 'SimulationDirOSW currently cannot simulate more than one feature'
  end

  @feature = features[0]
  @feature_id = @feature.id

  if feature_names.size == 1
    @feature_name = feature_names[0]
  else
    @feature_name = @feature.name
  end

  @mapper_class = mapper_class
end

Public Instance Methods

clear() click to toggle source

Clear the directory that this simulation runs in

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 192
def clear
  dir = run_dir
  FileUtils.mkdir_p(dir) if !File.exist?(dir)
  Dir.glob(File.join(dir, '/*')).each do |f|
    FileUtils.rm_rf(f)
  end
end
create_input_files() click to toggle source

Create run directory and generate simulation OSW, all previous contents of directory are removed The simulation OSW is created by evaluating the mapper_class's create_osw method

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 207
def create_input_files
  clear

  dir = run_dir
  osw = eval("#{@mapper_class}.new.create_osw(scenario, features, feature_names)")
  osw_path = File.join(dir, 'in.osw')
  File.open(osw_path, 'w') do |f|
    f << JSON.pretty_generate(osw)
    # make sure data is written to the disk one way or the other
    begin
      f.fsync
    rescue StandardError
      f.flush
    end
  end
  return osw_path
end
failed_job_path() click to toggle source

Return the failed.job path

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 80
def failed_job_path
  return File.join(run_dir, 'failed.job')
end
finished_job_path() click to toggle source

Return the finished.job path

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 87
def finished_job_path
  return File.join(run_dir, 'finished.job')
end
in_osw() click to toggle source

Return the input OSW

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 60
def in_osw
  result = nil
  if File.exist?(in_osw_path)
    File.open(in_osw_path, 'r') do |f|
      result = JSON.parse(f.read, symbolize_names: true)
    end
  end
  return result
end
in_osw_path() click to toggle source

Return the input OSW path

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 53
def in_osw_path
  return File.join(run_dir, 'in.osw')
end
out_of_date?() click to toggle source

Return true if the simulation is out of date (input files newer than results), false otherwise. Non-existant simulation input files are out of date.

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 115
def out_of_date?
  if !File.exist?(run_dir)
    puts "run_dir '#{run_dir}' does not exist, simulation dir '#{run_dir}' out of date"
    return true
  end

  if !File.exist?(finished_job_path)
    puts "finished_job_path '#{finished_job_path}' does not exist, simulation dir '#{run_dir}' out of date"
    return true
  end

  if !File.exist?(out_osw_path)
    puts "out_osw_path '#{out_osw_path}' does not exist, simulation dir '#{run_dir}' out of date"
    return true
  end
  out_osw_time = File.mtime(out_osw_path)

  # array of files that this simulation dir depends on
  dependencies = []
  out_of_date_files = []

  # depends on the in.osw
  dependencies << in_osw_path

  # depends on the feature file
  dependencies << scenario.feature_file.path

  # depends on the mapper classes
  Dir.glob(File.join(scenario.mapper_files_dir, '*')).each do |f|
    dependencies << f
  end

  # depends on the root gemfile
  dependencies << File.join(scenario.root_dir, 'Gemfile')
  dependencies << File.join(scenario.root_dir, 'Gemfile.lock')

  # todo, read in the in.osw and depend on all the measures

  # check if out of date
  dependencies.each do |f|
    if File.exist?(f)
      if File.mtime(f) > out_osw_time
        out_of_date_files << f
      end
    else
      puts "Dependency file '#{f}' does not exist"
    end
  end

  if !out_of_date_files.empty?
    puts "Files [#{out_of_date_files.join(',')}] are newer than '#{out_osw_path}', simulation dir '#{run_dir}' out of date"
    return true
  end

  return false
end
out_osw() click to toggle source

Return the output OSW

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 101
def out_osw
  result = nil
  if File.exist?(out_osw_path)
    File.open(out_osw_path, 'r') do |f|
      result = JSON.parse(f.read, symbolize_names: true)
    end
  end
  return result
end
out_osw_path() click to toggle source

Return the output OSW path

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 94
def out_osw_path
  return File.join(run_dir, 'out.osw')
end
run_dir() click to toggle source

Return the directory that this simulation will run in.

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 43
def run_dir
  raise 'Feature ID not set' if @feature_id.nil?
  raise 'Scenario run dir not set' if scenario.run_dir.nil?

  return File.join(scenario.run_dir, "#{@feature_id}/")
end
simulation_status() click to toggle source

Return simulation status one of {'Not Started', 'Started', 'Complete', 'Failed'}

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 175
def simulation_status
  if File.exist?(failed_job_path)
    return 'Failed'
  elsif File.exist?(started_job_path)
    if File.exist?(finished_job_path)
      return 'Complete'
    else
      return 'Failed'
    end
  end

  return 'Not Started'
end
started_job_path() click to toggle source

Return the started.job path

# File lib/urbanopt/scenario/simulation_dir_osw.rb, line 73
def started_job_path
  return File.join(run_dir, 'started.job')
end