Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix feasibilty and slow convergences issues related bii_target_apr24 #760

Merged
merged 11 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
-

### added
-
- **start_scripts** added `lock_timeout` as option to `start_run` function

### removed
-

### fixed
-
- **44_biodiversity** bugfix i44_biome_share, code cleanup, added scaling of `q44_bii`

## [4.9.0] - 2024-12-05

Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Imports:
m4fsdp,
madrat,
magclass (>= 6.14.0),
magpie4 (>= 2.15.8),
magpie4 (>= 2.16.1),
MagpieNCGains,
magpiesets (>= 0.46.1),
mip,
mrcommons,
patchwork,
piamenv (>= 0.5.5),
piamInterfaces (>= 0.37.1),
piamInterfaces (>= 0.40.3),
piamutils,
quitte,
raster,
Expand Down
1 change: 0 additions & 1 deletion modules/44_biodiversity/bii_target/declarations.gms
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ positive variables
parameters
p44_bii_lower_bound(t,i,biome44) Interpolated lower bound for BII over time (1)
p44_start_value(i,biome44) Start value for BII lower bound (1)
p44_target_value(i,biome44) Target value for BII lower bound (1)
i44_biome_share(j,biome44) Share of biome type in each spatial unit (1)
i44_biome_area_reg(i,biome44) Area of range-rarity weighted biome type in each region (mio. ha)
;
Expand Down
4 changes: 2 additions & 2 deletions modules/44_biodiversity/bii_target/equations.gms
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
q44_bii(i2,biome44)$(i44_biome_area_reg(i2,biome44) > 0) ..
v44_bii(i2,biome44)
=e=
sum((cell(i2,j2),potnatveg,landcover44), vm_bv(j2,landcover44,potnatveg) * i44_biome_share(j2,biome44))
sum((cell(i2,j2),landcover44,potnatveg), vm_bv(j2,landcover44,potnatveg) * i44_biome_share(j2,biome44))
/ i44_biome_area_reg(i2,biome44);

*' For each of the 71 biomes, the BII has to meet a minium level based on `s44_bii_lower_bound`.
*' `v44_bii_missing` is a technical variable to maintain feasibility in case `v44_bii` cannot be increased.

q44_bii_target(i2,biome44)$(i44_biome_area_reg(i2,biome44) > 0) ..
q44_bii_target(i2,biome44) ..
v44_bii(i2,biome44) + v44_bii_missing(i2,biome44) =g= sum(ct, p44_bii_lower_bound(ct,i2,biome44));

*' Costs accrue only for `v44_bii_missing`. In the best case costs should be zero or close to zero.
Expand Down
5 changes: 3 additions & 2 deletions modules/44_biodiversity/bii_target/preloop.gms
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*** | Contact: [email protected]

* Calculate biome share
i44_biome_share(j,biome44) =
(f44_biome_area(j,biome44) + 1e-10) / sum(biome44_2, f44_biome_area(j,biome44_2) + 1e-10);
i44_biome_share(j,biome44) = 0;
i44_biome_share(j,biome44)$(sum(biome44_2, f44_biome_area(j,biome44_2)) > 0) =
f44_biome_area(j,biome44) / sum(biome44_2, f44_biome_area(j,biome44_2));

* Set i44_biome_area_reg
i44_biome_area_reg(i,biome44) =
Expand Down
29 changes: 14 additions & 15 deletions modules/44_biodiversity/bii_target/presolve.gms
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,31 @@ loop(i,
v44_bii_missing.fx(i,biome44) = 0;
else
v44_bii.l(i,biome44) =
sum((cell(i,j),potnatveg,landcover44), vm_bv.l(j,landcover44,potnatveg) * i44_biome_share(j,biome44))
sum((cell(i,j),landcover44,potnatveg), vm_bv.l(j,landcover44,potnatveg) * i44_biome_share(j,biome44))
/ i44_biome_area_reg(i,biome44);
);
);
);

if (m_year(t) = s44_start_year AND s44_bii_lower_bound > 0,
* The start value for the linear interpolation is the BII at biome level in the start year.
p44_start_value(i,biome44)$(m_year(t) = s44_start_year) = v44_bii.l(i,biome44);
p44_start_value(i,biome44) = v44_bii.l(i,biome44);
* The target value for the linear interpolation is the lower bound defined in `s44_bii_lower_bound`.
p44_target_value(i,biome44) = s44_bii_lower_bound;
* Linear increase of BII target values at biome level from start year to target year, and constant values thereafter.
p44_bii_lower_bound(t2,i,biome44) = p44_start_value(i,biome44) + ((m_year(t2) - s44_start_year) / (s44_target_year - s44_start_year)) * (s44_bii_lower_bound - p44_start_value(i,biome44));
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) > s44_target_year) = s44_bii_lower_bound;
* Avoid implausible values
p44_bii_lower_bound(t2,i,biome44)$(p44_bii_lower_bound(t2,i,biome44) >= 1) = 1;
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) < s44_start_year) = 0;
p44_bii_lower_bound(t2,i,biome44)$(i44_biome_area_reg(i,biome44) <= 0) = 0;
);

if (m_year(t) <= sm_fix_SSP2,
v44_bii.lo(i,biome44) = 0;
else
* Linear increase of BII target values at biome level from start year to target year, and constant values thereafter.
p44_bii_lower_bound(t2,i,biome44) = p44_start_value(i,biome44) + ((m_year(t2) - s44_start_year) / (s44_target_year - s44_start_year)) * (p44_target_value(i,biome44) - p44_start_value(i,biome44));
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) > s44_target_year) = p44_target_value(i,biome44);
v44_bii.lo(i,biome44) = p44_bii_lower_bound(t,i,biome44);
if(c44_bii_decrease = 0,
p44_bii_lower_bound(t,i,biome44)$(v44_bii.l(i,biome44) >= p44_target_value(i,biome44)) = v44_bii.l(i,biome44);
elseif c44_bii_decrease = 1,
p44_bii_lower_bound(t,i,biome44)$(v44_bii.l(i,biome44) >= p44_target_value(i,biome44)) = p44_target_value(i,biome44);
v44_bii.lo(i,biome44)$(v44_bii.l(i,biome44) >= s44_bii_lower_bound) = v44_bii.l(i,biome44);
);
p44_bii_lower_bound(t,i,biome44)$(p44_bii_lower_bound(t,i,biome44) >= 1) = 1;
p44_bii_lower_bound(t,i,biome44)$(m_year(t) < s44_start_year) = 0;
p44_bii_lower_bound(t,i,biome44)$(i44_biome_area_reg(i,biome44) <= 0) = 0;
* The lower bound of `v44_bii` is set to `p44_bii_lower_bound` to avoid a reduction of BII in combination with `v44_bii_missing`.
v44_bii.lo(i,biome44) = p44_bii_lower_bound(t,i,biome44);
display p44_bii_lower_bound;
);

1 change: 1 addition & 0 deletions modules/44_biodiversity/bii_target/realization.gms
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $Ifi "%phase%" == "sets" $include "./modules/44_biodiversity/bii_target/sets.gms
$Ifi "%phase%" == "declarations" $include "./modules/44_biodiversity/bii_target/declarations.gms"
$Ifi "%phase%" == "input" $include "./modules/44_biodiversity/bii_target/input.gms"
$Ifi "%phase%" == "equations" $include "./modules/44_biodiversity/bii_target/equations.gms"
$Ifi "%phase%" == "scaling" $include "./modules/44_biodiversity/bii_target/scaling.gms"
$Ifi "%phase%" == "preloop" $include "./modules/44_biodiversity/bii_target/preloop.gms"
$Ifi "%phase%" == "presolve" $include "./modules/44_biodiversity/bii_target/presolve.gms"
$Ifi "%phase%" == "postsolve" $include "./modules/44_biodiversity/bii_target/postsolve.gms"
Expand Down
8 changes: 8 additions & 0 deletions modules/44_biodiversity/bii_target/scaling.gms
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
*** | MAgPIE License Exception, version 1.0 (see LICENSE file).
*** | Contact: [email protected]

q44_bii.scale(i,biome44) = 1e10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the scaling needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on recommendations from the CONOPT support. Without the scaling, some test runs are infeasible.

3 changes: 1 addition & 2 deletions modules/44_biodiversity/bii_target_apr24/declarations.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand All @@ -16,7 +16,6 @@ positive variables
parameters
p44_bii_lower_bound(t,i,biome44) Interpolated lower bound for BII over time (1)
p44_start_value(i,biome44) Start value for BII lower bound (1)
p44_target_value(i,biome44) Target value for BII lower bound (1)
i44_biome_share(j,biome44) Share of biome type in each spatial unit (1)
i44_biome_area_reg(i,biome44) Area of range-rarity weighted biome type in each region (mio. ha)
;
Expand Down
6 changes: 3 additions & 3 deletions modules/44_biodiversity/bii_target_apr24/equations.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand All @@ -14,13 +14,13 @@
q44_bii(i2,biome44)$(i44_biome_area_reg(i2,biome44) > 0) ..
v44_bii(i2,biome44)
=e=
sum((cell(i2,j2),potnatveg,landcover44), vm_bv(j2,landcover44,potnatveg) * f44_rr_layer(j2) * i44_biome_share(j2,biome44))
sum((cell(i2,j2),landcover44,potnatveg), vm_bv(j2,landcover44,potnatveg) * f44_rr_layer(j2) * i44_biome_share(j2,biome44))
/ i44_biome_area_reg(i2,biome44);

*' For each of the 71 biomes, the BII has to meet a minium level based on `s44_bii_lower_bound`.
*' `v44_bii_missing` is a technical variable to maintain feasibility in case `v44_bii` cannot be increased.

q44_bii_target(i2,biome44)$(i44_biome_area_reg(i2,biome44) > 0) ..
q44_bii_target(i2,biome44) ..
v44_bii(i2,biome44) + v44_bii_missing(i2,biome44) =g= sum(ct, p44_bii_lower_bound(ct,i2,biome44));

*' Costs accrue only for `v44_bii_missing`. In the best case costs should be zero or close to zero.
Expand Down
2 changes: 1 addition & 1 deletion modules/44_biodiversity/bii_target_apr24/input.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand Down
2 changes: 1 addition & 1 deletion modules/44_biodiversity/bii_target_apr24/postsolve.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand Down
7 changes: 4 additions & 3 deletions modules/44_biodiversity/bii_target_apr24/preloop.gms
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
*** | MAgPIE License Exception, version 1.0 (see LICENSE file).
*** | Contact: [email protected]

* Calculate biome share
i44_biome_share(j,biome44) =
(f44_biome_area(j,biome44) + 1e-10) / sum(biome44_2, f44_biome_area(j,biome44_2) + 1e-10);
i44_biome_share(j,biome44) = 0;
i44_biome_share(j,biome44)$(sum(biome44_2, f44_biome_area(j,biome44_2)) > 0) =
f44_biome_area(j,biome44) / sum(biome44_2, f44_biome_area(j,biome44_2));

* Set i44_biome_area_reg
i44_biome_area_reg(i,biome44) =
Expand Down
30 changes: 14 additions & 16 deletions modules/44_biodiversity/bii_target_apr24/presolve.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand All @@ -13,33 +13,31 @@ loop(i,
v44_bii_missing.fx(i,biome44) = 0;
else
v44_bii.l(i,biome44) =
sum((cell(i,j),potnatveg,landcover44), vm_bv.l(j,landcover44,potnatveg) * f44_rr_layer(j) * i44_biome_share(j,biome44))
sum((cell(i,j),landcover44,potnatveg), vm_bv.l(j,landcover44,potnatveg) * f44_rr_layer(j) * i44_biome_share(j,biome44))
/ i44_biome_area_reg(i,biome44);
);
);
);

if (m_year(t) = s44_start_year AND s44_bii_lower_bound > 0,
* The start value for the linear interpolation is the BII at biome level in the start year.
p44_start_value(i,biome44)$(m_year(t) = s44_start_year) = v44_bii.l(i,biome44);
p44_start_value(i,biome44) = v44_bii.l(i,biome44);
* The target value for the linear interpolation is the lower bound defined in `s44_bii_lower_bound`.
p44_target_value(i,biome44) = s44_bii_lower_bound;
* Linear increase of BII target values at biome level from start year to target year, and constant values thereafter.
p44_bii_lower_bound(t2,i,biome44) = p44_start_value(i,biome44) + ((m_year(t2) - s44_start_year) / (s44_target_year - s44_start_year)) * (s44_bii_lower_bound - p44_start_value(i,biome44));
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) > s44_target_year) = s44_bii_lower_bound;
* Avoid implausible values
p44_bii_lower_bound(t2,i,biome44)$(p44_bii_lower_bound(t2,i,biome44) >= 1) = 1;
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) < s44_start_year) = 0;
p44_bii_lower_bound(t2,i,biome44)$(i44_biome_area_reg(i,biome44) <= 0) = 0;
);

if (m_year(t) <= sm_fix_SSP2,
v44_bii.lo(i,biome44) = 0;
else
* Linear increase of BII target values at biome level from start year to target year, and constant values thereafter.
p44_bii_lower_bound(t2,i,biome44) = p44_start_value(i,biome44) + ((m_year(t2) - s44_start_year) / (s44_target_year - s44_start_year)) * (p44_target_value(i,biome44) - p44_start_value(i,biome44));
p44_bii_lower_bound(t2,i,biome44)$(m_year(t2) > s44_target_year) = p44_target_value(i,biome44);
v44_bii.lo(i,biome44) = p44_bii_lower_bound(t,i,biome44);
if(c44_bii_decrease = 0,
p44_bii_lower_bound(t,i,biome44)$(v44_bii.l(i,biome44) >= p44_target_value(i,biome44)) = v44_bii.l(i,biome44);
elseif c44_bii_decrease = 1,
p44_bii_lower_bound(t,i,biome44)$(v44_bii.l(i,biome44) >= p44_target_value(i,biome44)) = p44_target_value(i,biome44);
v44_bii.lo(i,biome44)$(v44_bii.l(i,biome44) >= s44_bii_lower_bound) = v44_bii.l(i,biome44);
);
p44_bii_lower_bound(t,i,biome44)$(p44_bii_lower_bound(t,i,biome44) >= 1) = 1;
p44_bii_lower_bound(t,i,biome44)$(m_year(t) < s44_start_year) = 0;
p44_bii_lower_bound(t,i,biome44)$(i44_biome_area_reg(i,biome44) <= 0) = 0;
* The lower bound of `v44_bii` is set to `p44_bii_lower_bound` to avoid a reduction of BII in combination with `v44_bii_missing`.
v44_bii.lo(i,biome44) = p44_bii_lower_bound(t,i,biome44);
display p44_bii_lower_bound;
);

3 changes: 2 additions & 1 deletion modules/44_biodiversity/bii_target_apr24/realization.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand All @@ -19,6 +19,7 @@ $Ifi "%phase%" == "sets" $include "./modules/44_biodiversity/bii_target_apr24/se
$Ifi "%phase%" == "declarations" $include "./modules/44_biodiversity/bii_target_apr24/declarations.gms"
$Ifi "%phase%" == "input" $include "./modules/44_biodiversity/bii_target_apr24/input.gms"
$Ifi "%phase%" == "equations" $include "./modules/44_biodiversity/bii_target_apr24/equations.gms"
$Ifi "%phase%" == "scaling" $include "./modules/44_biodiversity/bii_target_apr24/scaling.gms"
$Ifi "%phase%" == "preloop" $include "./modules/44_biodiversity/bii_target_apr24/preloop.gms"
$Ifi "%phase%" == "presolve" $include "./modules/44_biodiversity/bii_target_apr24/presolve.gms"
$Ifi "%phase%" == "postsolve" $include "./modules/44_biodiversity/bii_target_apr24/postsolve.gms"
Expand Down
8 changes: 8 additions & 0 deletions modules/44_biodiversity/bii_target_apr24/scaling.gms
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
*** | MAgPIE License Exception, version 1.0 (see LICENSE file).
*** | Contact: [email protected]

q44_bii.scale(i,biome44) = 1e10;
2 changes: 1 addition & 1 deletion modules/44_biodiversity/bii_target_apr24/sets.gms
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*** | (C) 2008-2023 Potsdam Institute for Climate Impact Research (PIK)
*** | (C) 2008-2024 Potsdam Institute for Climate Impact Research (PIK)
*** | authors, and contributors see CITATION.cff file. This file is part
*** | of MAgPIE and licensed under AGPL-3.0-or-later. Under Section 7 of
*** | AGPL-3.0, you are granted additional permissions described in the
Expand Down
2 changes: 1 addition & 1 deletion scripts/start/projects/project_FSEC_Scenarios.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ for (scenarioName in c(

# Start runs
cfg <- fsecScenario(scenario = scenarioName)
start_run(cfg = cfg, codeCheck = codeCheck)
start_run(cfg = cfg, codeCheck = codeCheck, lock_timeout = 6)
}
4 changes: 4 additions & 0 deletions scripts/start/test_runs.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,28 @@ codeCheck <- FALSE

### Business-as-usual
cfg <- fsecScenario(scenario = "c_BAU")
cfg$force_replace <- TRUE
pascal-sauer marked this conversation as resolved.
Show resolved Hide resolved
cfg$results_folder <- "output/:title:"
cfg$results_folder_highres <- "output"
start_run(cfg = cfg, codeCheck = codeCheck)

### NatureSparing
cfg <- fsecScenario(scenario = "b_NatureSparing")
cfg$force_replace <- TRUE
cfg$results_folder <- "output/:title:"
cfg$results_folder_highres <- "output"
start_run(cfg = cfg, codeCheck = codeCheck)

### LandscapeElements
cfg <- fsecScenario(scenario = "a_LandscapeElements")
cfg$force_replace <- TRUE
cfg$results_folder <- "output/:title:"
cfg$results_folder_highres <- "output"
start_run(cfg = cfg, codeCheck = codeCheck)

### FSDP Scenario
cfg <- fsecScenario(scenario = "e_FSDP")
cfg$force_replace <- TRUE
cfg$results_folder <- "output/:title:"
cfg$results_folder_highres <- "output"
start_run(cfg = cfg, codeCheck = codeCheck)
4 changes: 2 additions & 2 deletions scripts/start_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ download_and_update <- function(cfg) {
}


start_run <- function(cfg, scenario = NULL, codeCheck = TRUE, lock_model = TRUE) {
start_run <- function(cfg, scenario = NULL, codeCheck = TRUE, lock_model = TRUE, lock_timeout = 1) {

timePrepareStart <- Sys.time()

Expand All @@ -222,7 +222,7 @@ start_run <- function(cfg, scenario = NULL, codeCheck = TRUE, lock_model = TRUE)
withr::defer(setwd(maindir))

if(lock_model) {
lock_id <- gms::model_lock(timeout1 = 1)
lock_id <- gms::model_lock(timeout1 = lock_timeout)
withr::defer(gms::model_unlock(lock_id))
}

Expand Down