From f66b7fd104a7e2c0ce43b80b5b423d858d693ebc Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Fri, 16 Feb 2024 14:35:49 +0000
Subject: [PATCH] Ensure sub-model is independent

---
 src/build_sequences.jl | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/build_sequences.jl b/src/build_sequences.jl
index a8157cf..be3da75 100644
--- a/src/build_sequences.jl
+++ b/src/build_sequences.jl
@@ -71,16 +71,16 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Mo
     end
 end
 
-function build_sequence(f::Function, ::Nothing; optimise=false)
-    build_sequence(f, nothing, global_model(), optimise)
-end
-
-function build_sequence(f::Function, scanner::Scanner, optimiser_constructor; optimise=true, kwargs...)
-    model = Model(optimizer_with_attributes(optimiser_constructor, [string(k) => v for (k, v) in kwargs]...))
+function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, optimiser_constructor; optimise=true, kwargs...)
+    if optimise || GLOBAL_MODEL[] == IGNORE_MODEL
+        model = Model(optimizer_with_attributes(optimiser_constructor, [string(k) => v for (k, v) in kwargs]...))
+    else
+        model = global_model()
+    end
     build_sequence(f, scanner, model, optimise)
 end
 
-function build_sequence(f::Function, scanner::Scanner; print_level=2, kwargs...)
+function build_sequence(f::Function, scanner::Union{Nothing, Scanner}; print_level=2, kwargs...)
     build_sequence(f, scanner, Ipopt.Optimizer; print_level=print_level, kwargs...)
 end
 
-- 
GitLab