Class: URBANopt::REopt::REoptGHPPostProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reopt/reopt_ghp_post_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_dir, system_parameter, modelica_result, reopt_ghp_assumptions = nil, api_key = nil) ⇒ REoptGHPPostProcessor

Returns a new instance of REoptGHPPostProcessor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 17

def initialize(run_dir, system_parameter, modelica_result, reopt_ghp_assumptions = nil, api_key = nil)
  # initialize @@logger
  @@logger ||= URBANopt::REopt.reopt_logger

  @api_key = api_key
  @reopt_ghp_output_district = nil
  @reopt_ghp_output_building = []
  @reopt_ghp_assumptions_hash = nil
  @reopt_ghp_assumptions = nil
  @system_parameter = nil
  @system_parameter_hash = nil
  @modelica_result = nil
  @building_ids = nil
  @run_dir = run_dir

  if !reopt_ghp_assumptions.nil?
    @reopt_ghp_assumptions = reopt_ghp_assumptions
    File.open(reopt_ghp_assumptions, 'r') do |file|
      @reopt_ghp_assumptions_input_hash = JSON.parse(file.read, symbolize_names: true)
    end
  end

  if !system_parameter.nil?
    @system_parameter = system_parameter
    File.open(system_parameter, 'r') do |file|
      @system_parameter_input_hash = JSON.parse(file.read, symbolize_names: true)
    end
    # Determine loop order
    loop_order = File.join(File.dirname(system_parameter), '_loop_order.json')
    if File.exist?(loop_order)
      File.open(loop_order, 'r') do |file|
        loop_order_input = JSON.parse(file.read, symbolize_names: true)
        # Check the type of the parsed data
        if loop_order_input.is_a?(Array)
          @loop_order_input_hash = loop_order_input
          @loop_order_input_hash.each do |item|
            puts "Building IDs in group: #{item[:list_bldg_ids_in_group].inspect}"
            puts "GHE IDs in group: #{item[:list_ghe_ids_in_group].inspect}"
          end
        elsif loop_order_input.is_a?(Hash)
          @loop_order_input_hash = [loop_order_input] # Wrap in array if a single object
          @loop_order_input_hash.each do |item|
            puts "Building IDs in group: #{item[:list_bldg_ids_in_group].inspect}"
            puts "GHE IDs in group: #{item[:list_ghe_ids_in_group].inspect}"
          end
        else
          puts 'Unexpected JSON structure'
        end
      end
    end

  end

  if !modelica_result.nil?
    @modelica_result_input = modelica_result
  end
end

Instance Attribute Details

#loop_order_input_hashObject

Returns the value of attribute loop_order_input_hash.



75
76
77
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 75

def loop_order_input_hash
  @loop_order_input_hash
end

#modelica_result_inputObject

Returns the value of attribute modelica_result_input.



75
76
77
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 75

def modelica_result_input
  @modelica_result_input
end

#reopt_ghp_assumptions_input_hashObject

Returns the value of attribute reopt_ghp_assumptions_input_hash.



75
76
77
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 75

def reopt_ghp_assumptions_input_hash
  @reopt_ghp_assumptions_input_hash
end

#run_dirObject

Returns the value of attribute run_dir.



75
76
77
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 75

def run_dir
  @run_dir
end

#system_parameter_input_hashObject

Returns the value of attribute system_parameter_input_hash.



75
76
77
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 75

def system_parameter_input_hash
  @system_parameter_input_hash
end

Instance Method Details

#run_reopt_lcca(system_parameter_hash: nil, reopt_ghp_assumptions_hash: nil, modelica_result: nil) ⇒ Object

# Create REopt input and output building report



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
# File 'lib/urbanopt/reopt/reopt_ghp_post_processor.rb', line 78

def run_reopt_lcca(system_parameter_hash: nil, reopt_ghp_assumptions_hash: nil, modelica_result: nil)
  adapter = URBANopt::REopt::REoptGHPAdapter.new

  # if these arguments are specified, use them
  if !system_parameter_hash.nil?
    @system_parameter_input_hash = system_parameter_hash
  end

  if !reopt_ghp_assumptions_hash.nil?
    @reopt_ghp_assumptions_input_hash = reopt_ghp_assumptions_hash
  end

  if !modelica_result.nil?
    @modelica_result_input = modelica_result
  end

  # Create folder for REopt input files only if they dont exist
  reopt_ghp_dir = File.join(@run_dir, 'reopt_ghp')
  reopt_ghp_input = File.join(reopt_ghp_dir, 'reopt_ghp_inputs')
  unless Dir.exist?(reopt_ghp_dir)
    FileUtils.mkdir_p(reopt_ghp_dir)
  end
  unless Dir.exist?(reopt_ghp_input)
    FileUtils.mkdir_p(reopt_ghp_input)
  end

  reopt_ghp_output = File.join(reopt_ghp_dir, 'reopt_ghp_outputs')
  unless Dir.exist?(reopt_ghp_output)
    FileUtils.mkdir_p(reopt_ghp_output)
  end

  # get building IDs from _loop_order.json
  building_ids = []
  ghp_ids = []
  @loop_order_input_hash.each do |loop|
    building_ids.concat(loop[:list_bldg_ids_in_group].flatten)
    ghp_ids.concat(loop[:list_ghe_ids_in_group].flatten)
  end

  building_ids.each do |building_id|
    # create REopt building input file for all buildings in loop order list in GHP scenario
    reopt_input_building = adapter.create_reopt_input_building_ghp(@run_dir, @system_parameter_input_hash, @reopt_ghp_assumptions_input_hash, building_id, @modelica_result_input)
    #create REopt building input file for all buildings in loop order list in BAU scenario
    reopt_input_building_bau = adapter.create_reopt_input_building_bau(@run_dir, @system_parameter_input_hash, @reopt_ghp_assumptions_input_hash, building_id, @modelica_result_input)
  end
  ghp_ids.each do |ghp_id|
    # create REopt district input file
    reopt_input_district = adapter.create_reopt_input_district_ghp(@run_dir, @system_parameter_input_hash, @reopt_ghp_assumptions_input_hash, ghp_id, @modelica_result_input)
  end

  Dir.foreach(reopt_ghp_input) do |input_file|
    # Skip '.' and '..' (current and parent directory entries)
    next if input_file == '.' || input_file == '..'

    reopt_ghp_input_file_path = File.join(reopt_ghp_input, input_file)

    reopt_input_data = nil

    File.open(reopt_ghp_input_file_path, 'r') do |f|
      reopt_input_data = JSON.parse(f.read)
    end

    base_name = File.basename(input_file, '.json')

    # reopt_ghp_output_file
    reopt_output_file = File.join(reopt_ghp_output, "#{base_name}_output.json")
    # call the REopt API
    api = URBANopt::REopt::REoptGHPAPI.new(reopt_input_data, @api_key, reopt_output_file)
    api.get_api_results

  end

  ## POST PROCESS RESULTS
  ghp_results = URBANopt::REopt::REoptGHPResult.new
  results = ghp_results.result_calculate(reopt_ghp_dir)
end