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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 44

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

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



63
64
65
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 63

def mapper_class
  @mapper_class
end

Instance Method Details

#clearObject

Clear the directory that this simulation runs in



225
226
227
228
229
230
231
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 225

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



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

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



104
105
106
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 104

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

#finished_job_pathObject

Return the finished.job path



111
112
113
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 111

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

#in_oswObject

Return the input OSW



84
85
86
87
88
89
90
91
92
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 84

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



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

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.

Returns:

  • (Boolean)


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

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



125
126
127
128
129
130
131
132
133
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 125

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



118
119
120
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 118

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

#run_dirObject

Return the directory that this simulation will run in.



68
69
70
71
72
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 68

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’



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 207

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



97
98
99
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 97

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