diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 99729e31..faad8fd6 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -3,6 +3,7 @@ align_matrix = true align_pair_arrow = true align_struct_field = true always_for_in = true +always_use_return = true conditional_to_if = true for_in_replacement = "in" format_docstrings = false diff --git a/benchmark/profiling.jl b/benchmark/profiling.jl index 60b5ab44..af46faf9 100644 --- a/benchmark/profiling.jl +++ b/benchmark/profiling.jl @@ -13,10 +13,10 @@ for file in readdir(NORSE_PATH; join = false) end # Add another line to rep-periods-data.csv and rep-periods-mapping.csv open(joinpath(input_dir, "rep-periods-data.csv"), "a") do io - println(io, "3,$new_rp_length,0.1") + return println(io, "3,$new_rp_length,0.1") end open(joinpath(input_dir, "rep-periods-mapping.csv"), "a") do io - println(io, "216,3,1") + return println(io, "216,3,1") end # Add profiles to flow and asset open(joinpath(input_dir, "flows-profiles.csv"), "a") do io diff --git a/docs/src/91-developer.md b/docs/src/91-developer.md index 2963be0a..58be6a92 100644 --- a/docs/src/91-developer.md +++ b/docs/src/91-developer.md @@ -198,6 +198,7 @@ We will try to follow these during development and reviews. - List obvious objects, e.g., `using JuMP: @variable`, since `@variable` is obviously from JuMP in this context, or `using Graph: SimpleDiGraph`, because it's a constructor with an obvious name. - For other objects inside `Package`, use `using Package: Package` and explicitly call `Package.A` to use it, e.g., `DataFrames.groupby`. - List all `using` in . +- Explicitly state what a function will `return`, including `return nothing`. ## Contributing Workflow @@ -441,8 +442,7 @@ See the file for an example of profiling code. When publishing a new version of the model to the Julia Registry, follow this procedure: > **Note:** -> To be able to register, you need to be a member of the organisation TulipaEnergy and have your visibility set to public:
-> ![Screenshot of public members of TulipaEnergy on GitHub](./images/PublicMember.png) +> To be able to register, you need to be a member of the organisation TulipaEnergy and have your visibility set to public:
> ![Screenshot of public members of TulipaEnergy on GitHub](./images/PublicMember.png) 1. Click on the `Project.toml` file on GitHub. diff --git a/src/constraints/consumer.jl b/src/constraints/consumer.jl index da186088..94a2bfcc 100644 --- a/src/constraints/consumer.jl +++ b/src/constraints/consumer.jl @@ -19,7 +19,7 @@ function add_consumer_constraints!(model, constraints, graph, sets) # - Balance constraint (using the lowest temporal resolution) df = filter(:asset => ∈(Ac), constraints[:highest_in_out].indices; view = true) - model[:consumer_balance] = [ + return model[:consumer_balance] = [ @constraint( model, incoming_flow_highest_in_out_resolution[row.index] - diff --git a/src/constraints/conversion.jl b/src/constraints/conversion.jl index 99e2d085..d7f367e1 100644 --- a/src/constraints/conversion.jl +++ b/src/constraints/conversion.jl @@ -17,7 +17,7 @@ function add_conversion_constraints!(model, constraints, sets) df = filter(:asset => ∈(Acv), constraints[:lowest].indices; view = true) incoming = constraints[:lowest].expressions[:incoming] outgoing = constraints[:lowest].expressions[:outgoing] - model[:conversion_balance] = [ + return model[:conversion_balance] = [ @constraint( model, incoming[row.index] == outgoing[row.index], diff --git a/src/constraints/energy.jl b/src/constraints/energy.jl index 39dbdee3..42531807 100644 --- a/src/constraints/energy.jl +++ b/src/constraints/energy.jl @@ -29,7 +29,7 @@ function add_energy_constraints!(model, constraints, graph) ] # - Minimum outgoing energy within each period block - model[:min_energy_inter_rp] = [ + return model[:min_energy_inter_rp] = [ @constraint( model, constraints[:min_energy_inter_rp].expressions[:outgoing][row.index] ≥ diff --git a/src/constraints/group.jl b/src/constraints/group.jl index 006d5829..481bb53a 100644 --- a/src/constraints/group.jl +++ b/src/constraints/group.jl @@ -42,7 +42,7 @@ function add_group_constraints!(model, variables, graph, sets, groups) groups_with_min_investment_limit = (group for group in groups if !ismissing(group.min_investment_limit)) - model[:investment_group_min_limit] = [ + return model[:investment_group_min_limit] = [ @constraint( model, investment_group[group] ≥ group.min_investment_limit, diff --git a/src/constraints/hub.jl b/src/constraints/hub.jl index ee51e061..e8a842fd 100644 --- a/src/constraints/hub.jl +++ b/src/constraints/hub.jl @@ -17,7 +17,7 @@ function add_hub_constraints!(model, constraints, sets) outgoing_flow_highest_in_out_resolution = constraints[:highest_in_out].expressions[:outgoing] # - Balance constraint (using the lowest temporal resolution) df = filter(:asset => ∈(Ah), constraints[:highest_in_out].indices; view = true) - model[:hub_balance] = [ + return model[:hub_balance] = [ @constraint( model, incoming_flow_highest_in_out_resolution[row.index] == diff --git a/src/constraints/transport.jl b/src/constraints/transport.jl index 3d2221d1..e2199adb 100644 --- a/src/constraints/transport.jl +++ b/src/constraints/transport.jl @@ -71,7 +71,7 @@ function add_transport_constraints!(model, variables, graph, sets) ] # - Min transport flow limit - model[:min_transport_flow_limit] = [ + return model[:min_transport_flow_limit] = [ @constraint( model, flow[row.index] ≥ -lower_bound_transport_flow[idx], diff --git a/src/io.jl b/src/io.jl index 059673fe..909bdc84 100644 --- a/src/io.jl +++ b/src/io.jl @@ -97,7 +97,7 @@ function create_internal_structures(connection) @assert table_name in ("asset_$year_prefix", "flow_$year_prefix") result = _query_data_per_year(table_name, col, year_col; where_pairs...) - Dict(row[Symbol(year_col)] => getproperty(row, Symbol(col)) for row in result) + return Dict(row[Symbol(year_col)] => getproperty(row, Symbol(col)) for row in result) end _query_data_per_both_years(table_name, col; where_pairs...) = begin @@ -331,7 +331,7 @@ function save_solution_to_file(output_folder, energy_problem::EnergyProblem) if !energy_problem.solved error("The energy_problem has not been solved yet.") end - save_solution_to_file(output_folder, energy_problem.graph, energy_problem.solution) + return save_solution_to_file(output_folder, energy_problem.graph, energy_problem.solution) end """ @@ -507,7 +507,7 @@ The starting value is the value of the previous grouped timesteps or periods or The ending value is the value for the grouped timesteps or periods. """ function _interpolate_storage_level!(df, time_column) - DataFrames.flatten( + return DataFrames.flatten( DataFrames.transform( df, [time_column, :value, :processed_value] => diff --git a/src/run-scenario.jl b/src/run-scenario.jl index c38e59a7..096ee172 100644 --- a/src/run-scenario.jl +++ b/src/run-scenario.jl @@ -40,7 +40,7 @@ function run_scenario( if log_file != "" open(log_file, "w") do io - show(io, to) + return show(io, to) end end diff --git a/src/tmp.jl b/src/tmp.jl index 787e93f8..7e0cadb0 100644 --- a/src/tmp.jl +++ b/src/tmp.jl @@ -233,7 +233,7 @@ function tmp_create_partition_tables(connection) end _append_given_durations(appender, row, durations) end - DuckDB.close(appender) + return DuckDB.close(appender) end function tmp_create_union_tables(connection) @@ -280,7 +280,7 @@ function tmp_create_union_tables(connection) ) # Union of all assets, and incoming and outgoing flows - DuckDB.execute( + return DuckDB.execute( connection, "CREATE OR REPLACE TEMP TABLE t_union_all AS SELECT DISTINCT asset, year, rep_period, time_block_start, time_block_end @@ -297,7 +297,7 @@ function _append_lowest_helper(appender, group, s, e) end DuckDB.append(appender, s) DuckDB.append(appender, e) - DuckDB.end_row(appender) + return DuckDB.end_row(appender) end function tmp_create_lowest_resolution_table(connection) diff --git a/src/variables/investments.jl b/src/variables/investments.jl index 339bc4a8..3070e3a6 100644 --- a/src/variables/investments.jl +++ b/src/variables/investments.jl @@ -17,7 +17,7 @@ function _create_investment_variable!( base_name = "$name[" * join(keys_from_row(row), ",") * "]" ) for row in eachrow(this_var.indices) ] - this_var.lookup = OrderedDict( + return this_var.lookup = OrderedDict( keys_from_row(row) => var for (var, row) in zip(this_var.container, eachrow(this_var.indices)) ) diff --git a/utils/csv-modifications.jl b/utils/csv-modifications.jl index 654a16c6..647dc4d9 100644 --- a/utils/csv-modifications.jl +++ b/utils/csv-modifications.jl @@ -41,9 +41,9 @@ Saves the CSV file at `path`. """ function write_tulipa_csv(tulipa_csv::TulipaCSV, path) open(path, "w") do io - println(io, join(tulipa_csv.units, ",")) + return println(io, join(tulipa_csv.units, ",")) end - CSV.write(path, tulipa_csv.csv; append = true, writeheader = true) + return CSV.write(path, tulipa_csv.csv; append = true, writeheader = true) end """ @@ -59,7 +59,7 @@ The `content` can be a value or a vector of proper size. function add_column(tulipa_csv::TulipaCSV, unit::String, colname, content, position::Int) @debug "Adding column $colname ($unit) at $position" insert!(tulipa_csv.units, position, unit) - insertcols!(tulipa_csv.csv, position, Symbol(colname) => content) + return insertcols!(tulipa_csv.csv, position, Symbol(colname) => content) end function add_column( @@ -69,7 +69,7 @@ function add_column( unit = "", position = size(tulipa_csv.csv, 2) + 1, ) - add_column(tulipa_csv, unit, colname, content, position) + return add_column(tulipa_csv, unit, colname, content, position) end """ @@ -136,7 +136,7 @@ end function change_file(f, path) tulipa_csv = TulipaCSV(path) f(tulipa_csv) - write_tulipa_csv(tulipa_csv, path) + return write_tulipa_csv(tulipa_csv, path) end input_files_folders = [ diff --git a/utils/scripts/assets-data-revamp-2.jl b/utils/scripts/assets-data-revamp-2.jl index 05669c3f..95e1a247 100644 --- a/utils/scripts/assets-data-revamp-2.jl +++ b/utils/scripts/assets-data-revamp-2.jl @@ -31,7 +31,7 @@ apply_to_files_named("asset-both.csv") do path # Remove from asset-both df = t_both_csv.csv - unit, _ = remove_column(t_both_csv, "units_on_cost") + return unit, _ = remove_column(t_both_csv, "units_on_cost") end end end @@ -54,7 +54,7 @@ apply_to_files_named("flow-both.csv") do path # Remove from flow-both df = t_both_csv.csv - unit, _ = remove_column(t_both_csv, "variable_cost") + return unit, _ = remove_column(t_both_csv, "variable_cost") end end end diff --git a/utils/scripts/assets-data-revamp.jl b/utils/scripts/assets-data-revamp.jl index 23b7574b..1437a7ce 100644 --- a/utils/scripts/assets-data-revamp.jl +++ b/utils/scripts/assets-data-revamp.jl @@ -79,7 +79,7 @@ apply_to_files_named("asset.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -115,7 +115,7 @@ apply_to_files_named("flow.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -157,7 +157,7 @@ apply_to_files_named("asset-milestone.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -190,7 +190,7 @@ apply_to_files_named("flow-milestone.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -235,7 +235,7 @@ apply_to_files_named("asset-commission.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -275,7 +275,7 @@ apply_to_files_named("flow-commission.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -319,7 +319,7 @@ apply_to_files_named("asset-both.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end @@ -362,7 +362,7 @@ apply_to_files_named("flow-both.csv"; include_missing = true) do path ", ) |> DataFrame - tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] + return tcsv.units = ["" for _ in 1:size(tcsv.csv, 2)] end end diff --git a/utils/scripts/create-multiyear-data.jl b/utils/scripts/create-multiyear-data.jl index c7087560..a9532c2e 100644 --- a/utils/scripts/create-multiyear-data.jl +++ b/utils/scripts/create-multiyear-data.jl @@ -31,15 +31,15 @@ cd(multiyear_folder) do change_file(filename) do tcsv df_2050 = copy(tcsv.csv) df_2050.year .= 2050 - tcsv.csv = [tcsv.csv; df_2050] + return tcsv.csv = [tcsv.csv; df_2050] end end change_file("assets-data.csv") do tcsv - tcsv.csv[end, :active] = "false" + return tcsv.csv[end, :active] = "false" end change_file("flows-data.csv") do tcsv - tcsv.csv[end, :active] = "false" + return tcsv.csv[end, :active] = "false" end end diff --git a/utils/scripts/prepare-for-multiyear.jl b/utils/scripts/prepare-for-multiyear.jl index 143bbfdd..b4cdbbac 100644 --- a/utils/scripts/prepare-for-multiyear.jl +++ b/utils/scripts/prepare-for-multiyear.jl @@ -9,7 +9,7 @@ run(`git restore test/inputs/ benchmark/EU/`) for filename in ["assets-data.csv", "flows-data.csv"] apply_to_files_named(filename) do path change_file(path) do tcsv - add_column(tcsv, "year", 2030; position = 5) + return add_column(tcsv, "year", 2030; position = 5) end end end @@ -24,7 +24,7 @@ apply_to_files_named("year-data.csv"; include_missing = true) do path touch(path) change_file(path) do tcsv tcsv.units = ["", "h"] - tcsv.csv = DataFrame(:year => [2030], :length => [8760]) + return tcsv.csv = DataFrame(:year => [2030], :length => [8760]) end end @@ -53,7 +53,7 @@ apply_to_files_named("timeframe-data.csv"; include_missing = true) do path ), ) tcsv.units = ["", ""] - add_column(tcsv, "year", 2030; position = 1) + return add_column(tcsv, "year", 2030; position = 1) end end @@ -69,7 +69,7 @@ for filename in [ change_file(path) do tcsv idx = findfirst(==("rep_period"), names(tcsv.csv)) @assert idx !== nothing - add_column(tcsv, "year", 2030; position = idx) + return add_column(tcsv, "year", 2030; position = idx) end end end @@ -81,7 +81,7 @@ for filename in ["profiles-timeframe.csv", "rep-periods-mapping.csv"] change_file(path) do tcsv idx = findfirst(==("period"), names(tcsv.csv)) @assert idx !== nothing - add_column(tcsv, "year", 2030; position = idx) + return add_column(tcsv, "year", 2030; position = idx) end end end @@ -99,7 +99,7 @@ for filename in [ change_file(path) do tcsv idx = findfirst(==("profile_type"), names(tcsv.csv)) @assert idx !== nothing - add_column(tcsv, "year", 2030; position = idx) + return add_column(tcsv, "year", 2030; position = idx) end end end @@ -108,13 +108,13 @@ end # Maybe this should be renamed to `assets-year-partitions.csv`? apply_to_files_named("assets-timeframe-partitions.csv") do path change_file(path) do tcsv - add_column(tcsv, "year", 2030; position = 2) + return add_column(tcsv, "year", 2030; position = 2) end end # And don't forget groups apply_to_files_named("groups-data.csv") do path change_file(path) do tcsv - add_column(tcsv, "year", 2030; position = 2) + return add_column(tcsv, "year", 2030; position = 2) end end diff --git a/utils/scripts/split-assets-year-data.jl b/utils/scripts/split-assets-year-data.jl index 0f52460c..46c8a930 100644 --- a/utils/scripts/split-assets-year-data.jl +++ b/utils/scripts/split-assets-year-data.jl @@ -17,7 +17,7 @@ apply_to_files_named("vintage-assets-data.csv"; include_missing = true) do path t_assets_data = TulipaCSV(joinpath(dirname(path), "assets-data.csv")) change_file(path) do tcsv tcsv.units = ["", "", "kEUR/MW/year", "kEUR/MW/year"] - tcsv.csv = + return tcsv.csv = t_assets_data.csv[:, [:name, :commission_year, :fixed_cost, :investment_cost]] |> unique end end @@ -33,7 +33,7 @@ apply_to_files_named("graph-assets-data.csv") do path add_column(tcsv, "discount_rate", 0.05) remove_column(t_assets_data, :capacity) - remove_column(t_assets_data, :technical_lifetime) + return remove_column(t_assets_data, :technical_lifetime) end end end @@ -50,7 +50,7 @@ apply_to_files_named("vintage-flows-data.csv"; include_missing = true) do path t_assets_data = TulipaCSV(joinpath(dirname(path), "flows-data.csv")) change_file(path) do tcsv tcsv.units = ["asset_name", "asset_name", "kEUR/MW"] - tcsv.csv = t_assets_data.csv[:, [:from_asset, :to_asset, :investment_cost]] |> unique + return tcsv.csv = t_assets_data.csv[:, [:from_asset, :to_asset, :investment_cost]] |> unique end end @@ -63,7 +63,7 @@ apply_to_files_named("graph-flows-data.csv") do path append!(tcsv.units, ["MW"]) leftjoin!(tcsv.csv, df; on = [:from_asset, :to_asset]) - remove_column(t_flows_data, :capacity) + return remove_column(t_flows_data, :capacity) end end end diff --git a/utils/scripts/split-graph-data.jl b/utils/scripts/split-graph-data.jl index d0fe9a47..280e1f46 100644 --- a/utils/scripts/split-graph-data.jl +++ b/utils/scripts/split-graph-data.jl @@ -26,12 +26,12 @@ apply_to_files_named("assets-data.csv") do path tcsv.csv = unique(old_tcsv.csv[:, indices]) idx = findfirst(names(tcsv.csv) .== "investment_cost") - add_column(tcsv, "investment_method", "simple"; unit = "{none;simple;compact}") + return add_column(tcsv, "investment_method", "simple"; unit = "{none;simple;compact}") end indices = setdiff(1:length(old_tcsv.units), columnindex.(Ref(old_tcsv.csv), static_cols)) old_tcsv.units = old_tcsv.units[indices] - old_tcsv.csv = old_tcsv.csv[:, indices] + return old_tcsv.csv = old_tcsv.csv[:, indices] end end @@ -50,17 +50,17 @@ apply_to_files_named("flows-data.csv") do path indices = columnindex.(Ref(old_tcsv.csv), cols) tcsv.units = old_tcsv.units[indices] - tcsv.csv = unique(old_tcsv.csv[:, indices]) + return tcsv.csv = unique(old_tcsv.csv[:, indices]) end indices = setdiff(1:length(old_tcsv.units), columnindex.(Ref(old_tcsv.csv), static_cols)) old_tcsv.units = old_tcsv.units[indices] - old_tcsv.csv = old_tcsv.csv[:, indices] + return old_tcsv.csv = old_tcsv.csv[:, indices] end end apply_to_files_named("year-data.csv") do path change_file(path) do tcsv - add_column(tcsv, "is_milestone", true; unit = "{true;false}") + return add_column(tcsv, "is_milestone", true; unit = "{true;false}") end end