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

#feature_idObject (readonly)

Returns the value of attribute feature_id.



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

def feature_id
  @feature_id
end

#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



218
219
220
221
222
223
224
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 218

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



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 233

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



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

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

#finished_job_pathObject

Return the finished.job path



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

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

#in_oswObject

Return the input OSW



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

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



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

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.



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

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



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

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



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

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

#run_dirObject

Return the directory that this simulation will run in.



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

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’



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 201

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



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

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