Skip to content

Add experimental compile to linear solver. #1306

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

Merged
merged 1 commit into from
Jun 23, 2025
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
11 changes: 11 additions & 0 deletions torax/_src/solver/linear_theta_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# limitations under the License.

"""The LinearThetaMethod solver class."""
import functools

import jax
from torax._src import state
from torax._src import xnp
from torax._src.config import runtime_params_slice
from torax._src.core_profiles import convertors
from torax._src.fvm import calc_coeffs
Expand All @@ -28,6 +31,14 @@
class LinearThetaMethod(solver_lib.Solver):
"""Time step update using theta method, linearized on coefficients at t."""

@functools.partial(
xnp.jit,
static_argnames=[
'self',
'static_runtime_params_slice',
'evolving_names',
],
)
def _x_new(
self,
dt: jax.Array,
Expand Down
19 changes: 19 additions & 0 deletions torax/_src/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from torax._src.sources import source_models as source_models_lib
from torax._src.sources import source_profiles
from torax._src.transport_model import transport_model as transport_model_lib
import typing_extensions


class Solver(abc.ABC):
Expand Down Expand Up @@ -65,6 +66,24 @@ def __init__(
self.neoclassical_models = neoclassical_models
self.static_runtime_params_slice = static_runtime_params_slice

def __hash__(self) -> int:
return hash((
self.static_runtime_params_slice,
self.transport_model,
self.source_models,
self.pedestal_model,
))

def __eq__(self, other: typing_extensions.Self) -> bool:
return (
self.static_runtime_params_slice == other.static_runtime_params_slice
and self.static_runtime_params_slice
== other.static_runtime_params_slice
and self.transport_model == other.transport_model
and self.source_models == other.source_models
and self.pedestal_model == other.pedestal_model
)

@functools.cached_property
def evolving_names(self) -> tuple[str, ...]:
"""The names of core_profiles variables that are evolved by the solver."""
Expand Down
6 changes: 6 additions & 0 deletions torax/tests/sim_experimental_compile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@
class SimExperimentalCompileTest(sim_test_case.SimTestCase):

@parameterized.named_parameters(
# Using newton raphson non linear solver.
(
'test_iterhybrid_rampup',
'test_iterhybrid_rampup.py',
),
# Using linear solver.
(
'test_iterhybrid_predictor_corrector',
'test_iterhybrid_predictor_corrector.py',
),
)
def test_run_simulation_experimental_compile(
self,
Expand Down