Skip to content

Commit cee09e8

Browse files
committed
Round share graph values to 14
1 parent 8db16bb commit cee09e8

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

lib/atlas/exporter/graph_exporter.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ def to_h
4545

4646
private
4747

48+
PRECISION = 14
49+
50+
# Internal: Recursively rounds all Float values in a hash to PRECISION decimal places.
51+
def round_floats(value)
52+
case value
53+
when Float
54+
value.round(PRECISION)
55+
when Hash
56+
value.transform_values { |v| round_floats(v) }
57+
when Array
58+
value.map { |v| round_floats(v) }
59+
else
60+
value
61+
end
62+
end
63+
4864
# Internal: Given an array of +nodes+, creates a hash containing all
4965
# of the node demands and attributes.
5066
#
@@ -73,7 +89,7 @@ def nodes_hash(nodes)
7389

7490
attributes.delete(:queries)
7591

76-
hash[node.key] = attributes
92+
hash[node.key] = round_floats(attributes)
7793
end
7894
end
7995

@@ -96,14 +112,14 @@ def edges_hash(edges)
96112

97113
attributes.delete(:queries)
98114

99-
hash[model.key] = attributes
115+
hash[model.key] = round_floats(attributes)
100116
end
101117

102118
# Yay coupling carrier special cases!
103119
EnergyEdge.all.each do |edge|
104120
if edge.carrier == :coupling_carrier
105-
data[edge.key] = edge.to_hash
106-
data[edge.key][:share] = edge.child_share
121+
data[edge.key] = round_floats(edge.to_hash)
122+
data[edge.key][:share] = edge.child_share.to_f.round(PRECISION)
107123
end
108124
end
109125

@@ -122,7 +138,7 @@ def slots_hash(slots)
122138
if slot.get(:model).is_a?(Atlas::Slot::Elastic)
123139
hash[slot.carrier] = :elastic
124140
else
125-
hash[slot.carrier] = slot.share.to_f
141+
hash[slot.carrier] = slot.share.to_f.round(PRECISION)
126142
end
127143
end
128144

lib/atlas/graph_values.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def initialize(derived_dataset)
4343
@derived_dataset = derived_dataset
4444
end
4545

46+
PRECISION = 14
47+
4648
def values
4749
@values ||= YAML.load_file(graph_values_path)
4850
end
@@ -59,7 +61,7 @@ def for(element, attribute = nil)
5961

6062
def set(element_key, attribute, value)
6163
values[element_key.to_s] ||= {}
62-
values[element_key.to_s].deep_merge!(Hash[attribute.to_s, value])
64+
values[element_key.to_s].deep_merge!(Hash[attribute.to_s, round_all_floats(value)])
6365
end
6466

6567
alias_method :to_h, :values
@@ -69,12 +71,27 @@ def create!
6971
save("--- {}")
7072
end
7173

72-
def save(yaml = values.to_yaml)
74+
def save(yaml = nil)
75+
yaml ||= round_all_floats(values).to_yaml
7376
File.write(graph_values_path, yaml)
7477
end
7578

7679
private
7780

81+
# Internal: Recursively rounds all Float values to PRECISION decimal places.
82+
def round_all_floats(value)
83+
case value
84+
when Float
85+
value.round(PRECISION)
86+
when Hash
87+
value.transform_values { |v| round_all_floats(v) }
88+
when Array
89+
value.map { |v| round_all_floats(v) }
90+
else
91+
value
92+
end
93+
end
94+
7895
def validate_presence_of_init_keys
7996
values.each_pair do |_, values|
8097
next if values.blank?

spec/atlas/exporter/graph_exporter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
mother.set(:max_demand, 50)
199199
mother.get(:model).max_demand = 50.0
200200

201-
expect(nodes[:mother][:max_demand]).to eq(50)
201+
expect(nodes[:mother][:max_demand]).to be_within(1e-13).of(50)
202202
end
203203

204204
it 'is not exported when not specified in the document' do

spec/atlas/graph_values_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module Atlas; describe GraphValues do
5151
graph_values.set(node.key.to_s, :demand, 50.0)
5252
graph_values.save
5353

54-
expect(graph_values.to_h['bar']['demand']).to eq(50.0)
54+
expect(graph_values.to_h['bar']['demand']).to be_within(1e-13).of(50.0)
5555
end
5656
end
5757

@@ -66,7 +66,7 @@ module Atlas; describe GraphValues do
6666
graph_values.set(node.key.to_s, :demand, 50.0)
6767
graph_values.save
6868

69-
expect(graph_values.to_h['bar']['demand']).to eq(50.0)
69+
expect(graph_values.to_h['bar']['demand']).to be_within(1e-13).of(50.0)
7070
end
7171
end
7272

0 commit comments

Comments
 (0)