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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Attribute Details

#feature_idObject (readonly)

Returns the value of attribute feature_id.



38
39
40
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 38

def feature_id
  @feature_id
end

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



38
39
40
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 38

def mapper_class
  @mapper_class
end

Instance Method Details

#clearObject

Clear the directory that this simulation runs in



192
193
194
195
196
197
198
# 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_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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 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_pathObject

Return the failed.job path



80
81
82
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 80

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

#finished_job_pathObject

Return the finished.job path



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

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

#in_oswObject

Return the input OSW



60
61
62
63
64
65
66
67
68
# 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_pathObject

Return the input OSW path



53
54
55
# 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?Boolean

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

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
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
# 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_oswObject

Return the output OSW



101
102
103
104
105
106
107
108
109
# 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_pathObject

Return the output OSW path



94
95
96
# File 'lib/urbanopt/scenario/simulation_dir_osw.rb', line 94

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

#run_dirObject

Return the directory that this simulation will run in.



43
44
45
46
47
48
# 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_statusObject

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



175
176
177
178
179
180
181
182
183
184
185
186
187
# 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_pathObject

Return the started.job path



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

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