Class: OpenStudio::Analysis::Formulation

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/analysis/formulation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display_name) ⇒ Object

Create an instance of the OpenStudio::Analysis::Formulation

Parameters:

  • display_name (String) —

    Display name of the project.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openstudio/analysis/formulation.rb', line 26

def initialize(display_name)
  @display_name = display_name
  @analysis_type = nil
  @outputs = []

  # Initialize child objects (expect workflow)
  @weather_file = WeatherFile.new
  @seed_model = SeedModel.new
  @algorithm = OpenStudio::Analysis::AlgorithmAttributes.new

  # Analysis Zip attributes
  @weather_files = SupportFiles.new
  @seed_models = SupportFiles.new
  @worker_inits = SupportFiles.new
  @worker_finalizes = SupportFiles.new
  @libraries = SupportFiles.new
  #@initialization_scripts = SupportFiles.new
end

Instance Attribute Details

#algorithm ⇒ Object

Returns the value of attribute algorithm.



13
14
15
# File 'lib/openstudio/analysis/formulation.rb', line 13

def algorithm
  @algorithm
end

#analysis_type ⇒ Object

Returns the value of attribute analysis_type.



10
11
12
# File 'lib/openstudio/analysis/formulation.rb', line 10

def analysis_type
  @analysis_type
end

#display_name ⇒ Object

Returns the value of attribute display_name.



11
12
13
# File 'lib/openstudio/analysis/formulation.rb', line 11

def display_name
  @display_name
end

#libraries ⇒ Object (readonly)

Returns the value of attribute libraries.



20
21
22
# File 'lib/openstudio/analysis/formulation.rb', line 20

def libraries
  @libraries
end

#seed_model(file) ⇒ Object (readonly)

Path to the seed model

Parameters:

  • path (String) —

    Path to the seed model. This should be relative.



60
61
62
# File 'lib/openstudio/analysis/formulation.rb', line 60

def seed_model
  @seed_model
end

#seed_models ⇒ Object (readonly)

Returns the value of attribute seed_models.



17
18
19
# File 'lib/openstudio/analysis/formulation.rb', line 17

def seed_models
  @seed_models
end

#weather_file(file) ⇒ Object (readonly)

Path to the weather file (or folder). If it is a folder, then the measures will look for the weather file by name in that folder.

Parameters:

  • path (String) —

    Path to the weather file or folder.



68
69
70
# File 'lib/openstudio/analysis/formulation.rb', line 68

def weather_file
  @weather_file
end

#weather_files ⇒ Object (readonly)

the attributes below are used for packaging data into the analysis zip file



16
17
18
# File 'lib/openstudio/analysis/formulation.rb', line 16

def weather_files
  @weather_files
end

#worker_finalizes ⇒ Object (readonly)

Returns the value of attribute worker_finalizes.



19
20
21
# File 'lib/openstudio/analysis/formulation.rb', line 19

def worker_finalizes
  @worker_finalizes
end

#worker_inits ⇒ Object (readonly)

Returns the value of attribute worker_inits.



18
19
20
# File 'lib/openstudio/analysis/formulation.rb', line 18

def worker_inits
  @worker_inits
end

#workflow ⇒ Object

Initialize or return the current workflow object

Returns:

  • (Object) —

    An OpenStudio::Analysis::Workflow object



48
49
50
# File 'lib/openstudio/analysis/formulation.rb', line 48

def workflow
  @workflow
end

Instance Method Details

#add_directory_to_zip(zipfile, local_directory, relative_zip_directory) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/openstudio/analysis/formulation.rb', line 203

def add_directory_to_zip(zipfile, local_directory, relative_zip_directory)
  # puts "Add Directory #{local_directory}"
  Dir[File.join("#{local_directory}", '**', '**')].each do |file|
    # puts "Adding File #{file}"
    zipfile.add(file.sub(local_directory, relative_zip_directory), file)
  end
  zipfile
end

#add_output(output_hash) ⇒ Object

Add an output of interest to the problem formulation

Parameters:

  • output_hash (Hash) —

    Hash of the output variable in the legacy format

Options Hash (output_hash):

  • :display_name (String) —

    Name to display

  • :display_name_short (String) —

    A shorter display name

  • :metadata_id (String) —

    Link to DEnCity ID in which this output corresponds

  • :name (String) —

    Unique machine name of the variable. Typically this is measure.attribute

  • :export (String) —

    Export the variable to CSV and dataframes from OpenStudio-server

  • :visualize (String) —

    Visualize the variable in the plots on OpenStudio-server

  • :units (String) —

    Units of the variable as a string

  • :variable_type (String) —

    Data type of the variable

  • :objective_function (Boolean) —

    Whether or not this output is an objective function. Default: false

  • :objective_function_index (Integer) —

    Index of the objective function. Default: nil

  • :objective_function_target (Float) —

    Target for the objective function to reach (if defined). Default: nil

  • :scaling_factor (Float) —

    How to scale the objective function(s). Default: nil

  • :objective_function_group (Integer) —

    If grouping objective functions, then group ID. Default: nil



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/openstudio/analysis/formulation.rb', line 88

def add_output(output_hash)
  output_hash = {
      units: '',
      objective_function: false,
      objective_function_index: nil,
      objective_function_target: nil,
      objective_function_group: nil,
      scaling_factor: nil
  }.merge(output_hash)

  # if the objective_function index is nil and the variable is an objective function, then increment and
  # assign and objective function index
  unless output_hash[:objective_function_index]
    values = @outputs.map { |o| o[:objective_function_index] }
    output_hash[:objective_function_index] = values.empty? ? 0 : values.max + 1
  end

  @outputs << output_hash
end

#save(filename, version = 1) ⇒ Boolean

save the file to JSON. Will overwrite the file if it already exists

Parameters:

  • filename (String) —

    Name of file to create. It will create the directory and override the file if it exists.

  • version (Integer) (defaults to: 1) —

    Version of the format to return

Returns:

  • (Boolean)


172
173
174
175
176
177
# File 'lib/openstudio/analysis/formulation.rb', line 172

def save(filename, version = 1)
  FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename)
  File.open(filename, 'w') { |f| f << JSON.pretty_generate(to_hash(version)) }

  true
end

#save_static_data_point(filename, version = 1) ⇒ Boolean

save the data point JSON with the variables set to the static values. Will overwrite the file if it already exists

Parameters:

  • version (Integer) (defaults to: 1) —

    Version of the format to return

Returns:

  • (Boolean)


183
184
185
186
187
# File 'lib/openstudio/analysis/formulation.rb', line 183

def save_static_data_point(filename, version = 1)
  File.open(filename, 'w') { |f| f << JSON.pretty_generate(to_static_data_point_hash(version)) }

  true
end

#save_zip(filename) ⇒ Boolean

save the analysis zip file which contains the measures, seed model, weather file, and init/final scripts

Parameters:

  • filename (String) —

    Name of file to create. It will create the directory and override the file if it exists.

Returns:

  • (Boolean)


193
194
195
196
197
# File 'lib/openstudio/analysis/formulation.rb', line 193

def save_zip(filename)
  FileUtils.mkdir_p File.dirname(filename) unless Dir.exist? File.dirname(filename)

  save_analysis_zip(filename)
end

#to_hash(version = 1) ⇒ Hash

return a hash.

Parameters:

  • version (Integer) (defaults to: 1) —

    Version of the format to return

Returns:



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
# File 'lib/openstudio/analysis/formulation.rb', line 112

def to_hash(version = 1)
  # fail 'Must define an analysis type' unless @analysis_type
  if version == 1
    h = {
        analysis: {
            display_name: @display_name,
            name: @display_name.snake_case,
            output_variables: @outputs,
            problem: {
                analysis_type: @analysis_type,
                algorithm: algorithm.to_hash(version),
                workflow: workflow.to_hash(version)
            },
            seed: @seed_model[:file],
            weather_file: @weather_file[:file],
            file_format_version: version
        }
    }

    # This is a hack right now, but after the initial hash is created go back and add in the objective functions
    # to the the algorithm as defined in the output_variables list
    ofs = @outputs.map { |i| i[:name] if i[:objective_function] }.compact
    if h[:analysis][:problem][:algorithm]
      h[:analysis][:problem][:algorithm][:objective_functions] = ofs
    end

    h
  else
    fail "Version #{version} not defined for #{self.class} and #{__method__}"
  end
end

#to_static_data_point_hash(version = 1) ⇒ Hash

return a hash of the data point with the static variables set

Parameters:

  • version (Integer) (defaults to: 1) —

    Version of the format to return

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/openstudio/analysis/formulation.rb', line 148

def to_static_data_point_hash(version = 1)
  if version == 1
    static_hash = {}
    # TODO: this method should be on the workflow step and bubbled up to this interface
    @workflow.items.map do |item|
      item.variables.map { |v| static_hash[v[:uuid]] = v[:static_value] }
    end

    h = {
        data_point: {
            set_variable_values: static_hash,
            status: 'na',
            uuid: SecureRandom.uuid
        }
    }
    h
  end
end