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.



20
21
22
23
24
25
26
27
28
29
# File 'lib/openstudio/analysis/formulation.rb', line 20

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
end

Instance Attribute Details

#algorithm ⇒ Object

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

#analysis_type ⇒ Object

Returns the value of attribute analysis_type.



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

def analysis_type
  @analysis_type
end

#display_name ⇒ Object

Returns the value of attribute display_name.



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

def display_name
  @display_name
end

#seed_model(path) ⇒ Object (readonly)

Path to the seed model

Parameters:

  • path (String) —

    Path to the seed model. This should be relative.



46
47
48
# File 'lib/openstudio/analysis/formulation.rb', line 46

def seed_model
  @seed_model
end

#weather_file(path) ⇒ 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.



54
55
56
# File 'lib/openstudio/analysis/formulation.rb', line 54

def weather_file
  @weather_file
end

#workflow ⇒ Object

Initialize or return the current workflow object

Returns:

  • (Object) —

    An OpenStudio::Analysis::Workflow object



34
35
36
# File 'lib/openstudio/analysis/formulation.rb', line 34

def workflow
  @workflow
end

Instance Method Details

#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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/openstudio/analysis/formulation.rb', line 74

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:

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

    Version of the format to return

Returns:

  • (Boolean)


157
158
159
160
161
# File 'lib/openstudio/analysis/formulation.rb', line 157

def save(filename, version = 1)
  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)


167
168
169
170
171
# File 'lib/openstudio/analysis/formulation.rb', line 167

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

#to_hash(version = 1) ⇒ Hash

return a hash.

Parameters:

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

    Version of the format to return

Returns:



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

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[:path],
        weather_file: @weather_file[:path],
        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:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/openstudio/analysis/formulation.rb', line 134

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