Class: URBANopt::Scenario::SimulationDirOSW

Inherits:
SimulationDirBase show all
Defined in:
lib/urbanopt/scenario/simulation_dir_osw.rb

Instance Attribute Summary collapse

Attributes inherited from SimulationDirBase

#feature_names, #features, #scenario

Instance Method Summary collapse

Constructor Details

#initialize(scenario, features, feature_names, mapper_class) ⇒ SimulationDirOSW

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 54

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

Instance Attribute Details

#feature_idObject (readonly)

Returns the value of attribute feature_id.



73
74
75
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 73

def feature_id
  @feature_id
end

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



73
74
75
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 73

def mapper_class
  @mapper_class
end

Instance Method Details

#clearObject

Clear the directory that this simulation runs in



228
229
230
231
232
233
234
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 228

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_filesObject

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 243

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_pathObject

Return the failed.job path



113
114
115
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 113

def failed_job_path
  return File.join(run_dir, 'failed.job')
end

#finished_job_pathObject

Return the finished.job path



120
121
122
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 120

def finished_job_path
  return File.join(run_dir, 'finished.job')
end

#in_oswObject

Return the input OSW



93
94
95
96
97
98
99
100
101
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 93

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_pathObject

Return the input OSW path



86
87
88
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 86

def in_osw_path
  return File.join(run_dir, 'in.osw')
end

#out_of_date?Boolean

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.



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
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 148

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 csv file
  dependencies << scenario.csv_file

  # 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_oswObject

Return the output OSW



134
135
136
137
138
139
140
141
142
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 134

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_pathObject

Return the output OSW path



127
128
129
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 127

def out_osw_path
  return File.join(run_dir, 'out.osw')
end

#run_dirObject

Return the directory that this simulation will run in.



77
78
79
80
81
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 77

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_statusObject

Return simulation status one of Started’, ‘Started’, ‘Complete’, ‘Failed’



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 211

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_pathObject

Return the started.job path



106
107
108
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 106

def started_job_path
  return File.join(run_dir, 'started.job')
end