Skip to content

Commit d1e6552

Browse files
committed
Delay loading of Julia in pdep/sls
Only load the Julia stuff when you know you need it. Since it might be installed improperly, or might take time to load.
1 parent d74d075 commit d1e6552

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

rmgpy/pdep/sls.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@
4444
from rmgpy.pdep.me import generate_full_me_matrix, states_to_configurations
4545
from rmgpy.rmg.reactionmechanismsimulator_reactors import to_julia
4646

47-
NO_JULIA = False
48-
try:
49-
from juliacall import Main
50-
51-
Main.seval("using ReactionMechanismSimulator.SciMLBase")
52-
Main.seval("using ReactionMechanismSimulator.Sundials")
53-
except Exception as e:
54-
logging.info(
55-
f"Unable to import Julia dependencies, original error: {str(e)}"
56-
". Master equation method 'ode' will not be available on this execution."
57-
)
58-
NO_JULIA = True
47+
class MainProxy:
48+
"""
49+
A proxy for the juliacall Main module, that will replace itself
50+
with the real thing the first time it's accessed.
51+
This is to delay loading Julia until it's really needed.
52+
"""
53+
def __getattr__(self, name):
54+
if hasattr(rmgpy, 'DISABLE_JULIA') and rmgpy.DISABLE_JULIA:
55+
raise ImportError("Julia imports disabled via rmgpy.DISABLE_JULIA flag")
56+
elif os.environ.get('RMG_DISABLE_JULIA', '').lower() in ('1', 'true', 'yes'):
57+
raise ImportError("Julia imports disabled via RMG_DISABLE_JULIA environment variable")
58+
59+
try:
60+
from juliacall import Main as JuliaMain
61+
Main = JuliaMain
62+
Main.seval("using ReactionMechanismSimulator.SciMLBase")
63+
Main.seval("using ReactionMechanismSimulator.Sundials")
64+
except Exception as e:
65+
logging.error("Failed to import Julia and load ReactionmechanismSimulator components needed.")
66+
raise
67+
globals()['Main'] = JuliaMain # Replace proxy with real thing, for next time it's called
68+
return getattr(JuliaMain, name) # Return the attribute for the first time it's called
69+
Main = MainProxy()
70+
5971

6072

6173
def get_initial_condition(network, x0, indices):
@@ -167,11 +179,6 @@ def get_rate_coefficients_SLS(network, T, P, method="mexp", neglect_high_energy_
167179
tau = np.abs(1.0 / fastest_reaction)
168180

169181
if method == "ode":
170-
if NO_JULIA:
171-
raise RuntimeError(
172-
"Required Julia dependencies for method 'ode' are not installed.\n"
173-
"Please follow the steps to install Julia dependencies at https://reactionmechanismgenerator.github.io/RMG-Py/users/rmg/installation/anacondaDeveloper.html."
174-
)
175182
f = Main.seval(
176183
"""
177184
function f(du, u, M, t)

0 commit comments

Comments
 (0)