|
44 | 44 | from rmgpy.pdep.me import generate_full_me_matrix, states_to_configurations |
45 | 45 | from rmgpy.rmg.reactionmechanismsimulator_reactors import to_julia |
46 | 46 |
|
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 | + |
59 | 71 |
|
60 | 72 |
|
61 | 73 | def get_initial_condition(network, x0, indices): |
@@ -167,11 +179,6 @@ def get_rate_coefficients_SLS(network, T, P, method="mexp", neglect_high_energy_ |
167 | 179 | tau = np.abs(1.0 / fastest_reaction) |
168 | 180 |
|
169 | 181 | 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 | | - ) |
175 | 182 | f = Main.seval( |
176 | 183 | """ |
177 | 184 | function f(du, u, M, t) |
|
0 commit comments