Skip to content

Commit 60ba2b9

Browse files
kndehaannoracato
andauthored
Split naphtha carrier from crude oil (#1607)
* Scaffold for naphtha migration * Add default dumps for naphtha migration * Update values in naphtha migration dumps --------- Co-authored-by: Nora Schinkel <ncschinkel@gmail.com>
1 parent 8607f3b commit 60ba2b9

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
class NaphthaMigration < ActiveRecord::Migration[7.1]
2+
include ETEngine::ScenarioMigration
3+
4+
RelativeChange = Struct.new(:input_oil, :input_naphtha, :default_key, :naphtha_default_key) do
5+
def skip?(scenario)
6+
!scenario.user_values.key?(input_oil)
7+
end
8+
9+
# Calculates and sets the inputs for crude oil and naphtha on the scenario.
10+
# Uses the old and new defaults (start values) of the inputs.
11+
def set_new_values(scenario, old_defaults, new_defaults)
12+
if change = relative_change(scenario, old_defaults)
13+
# If a change can be calculated: the inputs are set to the
14+
# relative change times the new default for both oil and naptha
15+
scenario.user_values[input_oil] = new_defaults[default_key] * change
16+
scenario.user_values[input_naphtha] = new_defaults[naphtha_default_key] * change
17+
else
18+
# If change can't be calculated, set naphtha to zero, oil stays
19+
# unchanged
20+
scenario.user_values[input_naphtha] = 0
21+
end
22+
end
23+
24+
# Returns the relative change of the input, based on the old default
25+
# of the input, and what it was set to by the user
26+
def relative_change(scenario, defaults)
27+
unless defaults[default_key].zero?
28+
return scenario.user_values[input_oil] / defaults[default_key]
29+
end
30+
end
31+
end
32+
33+
# Inputs that need to be recalculated when they were set by the user.
34+
CHANGES = [
35+
# Normal energetic
36+
RelativeChange.new(
37+
input_oil: 'industry_chemicals_other_burner_crude_oil_share',
38+
input_naphtha: 'industry_chemicals_other_burner_naphtha_share',
39+
default_key: 'industry_chemicals_other_burner_crude_oil_share',
40+
naphtha_default_key: 'industry_chemicals_other_burner_naphtha_share'
41+
),
42+
# Normal non-energetic
43+
RelativeChange.new(
44+
input_oil: 'industry_chemicals_other_crude_oil_non_energetic_share',
45+
input_naphtha: 'industry_chemicals_other_naphtha_non_energetic_share',
46+
default_key: 'industry_useful_demand_for_chemical_other_crude_oil_non_energetic_share',
47+
naphtha_default_key: 'industry_useful_demand_for_chemical_other_naphtha_non_energetic_share'
48+
),
49+
# External coupling energetic
50+
RelativeChange.new(
51+
input_oil: 'external_coupling_industry_chemical_other_burner_crude_oil_share',
52+
input_naphtha: 'external_coupling_industry_chemical_other_burner_naphtha_share',
53+
default_key: 'industry_chemicals_other_burner_crude_oil_share', # same start value
54+
naphtha_default_key: 'industry_chemicals_other_burner_naphtha_share'
55+
),
56+
# External coupling non-energetic
57+
RelativeChange.new(
58+
input_oil: 'external_coupling_industry_chemical_other_non_energetic_crude_oil_share',
59+
input_naphtha: 'external_coupling_industry_chemical_other_non_energetic_naphtha_share',
60+
default_key: 'industry_useful_demand_for_chemical_other_crude_oil_non_energetic_share', # same start value
61+
naphtha_default_key: 'industry_useful_demand_for_chemical_other_naphtha_non_energetic_share'
62+
)
63+
].freeze
64+
65+
RENAME = %w[fertilizers refineries other].to_h do |sector|
66+
[
67+
"external_coupling_energy_chemical_#{sector}_transformation_external_coupling_node_crude_oil_output_share",
68+
"external_coupling_energy_chemical_#{sector}_transformation_external_coupling_node_naphtha_output_share"
69+
]
70+
end.freeze
71+
72+
def up
73+
# Load up defaults from dumps
74+
old_defaults = load_defaults('old')
75+
new_defaults = load_defaults('new')
76+
77+
migrate_scenarios do |scenario|
78+
CHANGES.each do |change|
79+
next if change.skip?(scenario)
80+
# NOTE: Some datasets do not work yet on the naphtha branch, skip them
81+
next unless new_defaults.key?(scenario.area_code)
82+
83+
change.set_new_values(
84+
scenario,
85+
old_defaults[scenario.area_code],
86+
new_defaults[scenario.area_code]
87+
)
88+
end
89+
RENAME.each do |old_input, new_input|
90+
if scenario.user_values.key?(old_input)
91+
scenario.user_values[new_input] = scenario.user_values.delete(old_input)
92+
end
93+
end
94+
end
95+
end
96+
97+
def load_defaults(tag)
98+
JSON.load(File.read(
99+
Rails.root.join(
100+
"db/migrate/#{File.basename(__FILE__, '.rb')}/dataset_values_#{tag}.json"
101+
)
102+
))
103+
end
104+
end

db/migrate/20250722144555_naphtha_migration/dataset_values_new.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

db/migrate/20250722144555_naphtha_migration/dataset_values_old.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

db/schema.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2025_07_02_112742) do
13+
ActiveRecord::Schema[7.1].define(version: 2025_07_22_144555) do
1414
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
1515
t.string "name", limit: 191, null: false
1616
t.string "record_type", limit: 191, null: false
@@ -122,8 +122,8 @@
122122
t.string "source"
123123
t.binary "user_values", size: :long
124124
t.binary "balanced_values", size: :medium
125-
t.binary "active_couplings", size: :medium
126125
t.binary "metadata", size: :medium
126+
t.binary "active_couplings", size: :medium
127127
t.index ["created_at"], name: "index_scenarios_on_created_at"
128128
end
129129

@@ -161,7 +161,6 @@
161161
t.string "user_email"
162162
end
163163

164-
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
165164
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
166165
add_foreign_key "forecast_storage_orders", "scenarios"
167166
add_foreign_key "heat_network_orders", "scenarios"

0 commit comments

Comments
 (0)