|
| 1 | +# Copyright 2024 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""The PedestalPolicy abstract base class. |
| 16 | +
|
| 17 | +Determines potentially changing pedestal settings during simulation. |
| 18 | +""" |
| 19 | +import abc |
| 20 | +import dataclasses |
| 21 | +from typing import Optional |
| 22 | + |
| 23 | +import jax |
| 24 | +import jax.numpy as jnp |
| 25 | +from torax._src import array_typing |
| 26 | +from torax._src import state |
| 27 | + |
| 28 | +# pylint: disable=invalid-name |
| 29 | +# Using physics notation naming convention |
| 30 | + |
| 31 | + |
| 32 | +@jax.tree_util.register_dataclass |
| 33 | +@dataclasses.dataclass(frozen=True) |
| 34 | +class PedestalPolicyState: |
| 35 | + """State of the PedestalPolicy.""" |
| 36 | + |
| 37 | + # Whether to use the pedestal on this time step |
| 38 | + use_pedestal: array_typing.BoolScalar |
| 39 | + # Factor to scale pedestal height by. |
| 40 | + # Not all pedestal policies use this. |
| 41 | + scale_pedestal: Optional[array_typing.FloatScalar] |
| 42 | + |
| 43 | + |
| 44 | +class PedestalPolicy(abc.ABC): |
| 45 | + """Determines potentially changing pedestal settings during simulation. |
| 46 | +
|
| 47 | + Subclass responsbilities: |
| 48 | + - Must set _frozen = True at the end of the subclass __init__ method to |
| 49 | + activate immutability. |
| 50 | + """ |
| 51 | + |
| 52 | + def __setattr__(self, attr, value): |
| 53 | + # pylint: disable=g-doc-args |
| 54 | + # pylint: disable=g-doc-return-or-yield |
| 55 | + """Override __setattr__ to make the class (sort of) immutable. |
| 56 | +
|
| 57 | + Note that you can still do obj.field.subfield = x, so it is not true |
| 58 | + immutability, but this to helps to avoid some careless errors. |
| 59 | + """ |
| 60 | + if getattr(self, "_frozen", False): |
| 61 | + raise AttributeError("PedestalPolicy is immutable.") |
| 62 | + return super().__setattr__(attr, value) |
| 63 | + |
| 64 | + # @abc.abstractmethod |
| 65 | + # def _call_implementation( |
| 66 | + # self, |
| 67 | + # runtime_params: runtime_params_slice.RuntimeParams, |
| 68 | + # geo: geometry.Geometry, |
| 69 | + # core_profiles: state.CoreProfiles, |
| 70 | + # ) -> PedestalModelOutput: |
| 71 | + # """Calculate the pedestal values.""" |
| 72 | + |
| 73 | + @abc.abstractmethod |
| 74 | + def __hash__(self) -> int: |
| 75 | + """Hash function for the pedestal model. |
| 76 | +
|
| 77 | + Needed for jax.jit caching to work. |
| 78 | + """ |
| 79 | + ... |
| 80 | + |
| 81 | + @abc.abstractmethod |
| 82 | + def __eq__(self, other) -> bool: |
| 83 | + """Equality function for the pedestal model.""" |
| 84 | + ... |
0 commit comments