Coverage for physioblocks / description / flux.py: 100%
53 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
1# SPDX-FileCopyrightText: Copyright INRIA
2#
3# SPDX-License-Identifier: LGPL-3.0-only
4#
5# Copyright INRIA
6#
7# This file is part of PhysioBlocks, a library mostly developed by the
8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA.
9#
10# Authors:
11# - Colin Drieu
12# - Dominique Chapelle
13# - François Kimmig
14# - Philippe Moireau
15#
16# PhysioBlocks is free software: you can redistribute it and/or modify it under the
17# terms of the GNU Lesser General Public License as published by the Free Software
18# Foundation, version 3 of the License.
19#
20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY
21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
23#
24# You should have received a copy of the GNU Lesser General Public License along with
25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>.
27"""
28Declares **Flux** and **DOF** related objects.
29"""
31from collections.abc import Mapping
32from dataclasses import dataclass
34FLUX_TYPE_REGISTER_ID = "flux_type_register"
37class FluxDofTypesRegister:
38 """Stores relations between **Flux types** and **DOFs types**"""
40 def __init__(self) -> None:
41 # Stores relations between flux types and dof types
42 self._fluxes_types: dict[str, str] = {}
44 # Stores relations between dof types and flux types
45 self._dof_types: dict[str, str] = {}
47 @property
48 def flux_dof_couples(self) -> dict[str, str]:
49 """Get all **Flux-DOF** types couples.
51 :return: the **Flux-DOF** couples
52 :rtype: dict[str, str]
53 """
54 return self._fluxes_types.copy()
56 @property
57 def dof_flux_couples(self) -> dict[str, str]:
58 """Get all **DOF-Flux** types couples.
60 :return: the **DOF-Flux** couples
61 :rtype: dict[str, str]
62 """
63 return self._dof_types.copy()
65 def __type_registered(self, type_id: str) -> bool:
66 return type_id in self._fluxes_types or type_id in self._dof_types
68 def get(self, value: str) -> str | None:
69 return self.flux_dof_couples.get(value, None)
71 def update(self, mapping: Mapping[str, str]) -> None:
72 for key, value in mapping.items():
73 if value not in self.dof_flux_couples or key not in self.flux_dof_couples:
74 self.register_flux_dof_couple(key, value)
76 def register_flux_dof_couple(self, flux_type: str, dof_type: str) -> None:
77 """
78 Register a matching **Flux-DOF** type couple.
80 :param flux_type: The flux type to register
81 :type flux_type: str
83 :param dof_type: The matching DOF type
84 :type dof_type: str
86 :raise ValueError: Raise a ValueError when either the **Flux** or the **DOF**
87 type is already registered.
88 """
89 if self.__type_registered(flux_type) is True:
90 raise ValueError(str.format("{0} is already registered", flux_type))
91 if self.__type_registered(dof_type) is True:
92 raise ValueError(str.format("{0} is already registered", dof_type))
94 self._fluxes_types[flux_type] = dof_type
95 self._dof_types[dof_type] = flux_type
97 def unregister_flux_dof_couple(self, type_id: str) -> None:
98 """
99 Unregister the flux or DOF type and its matching type.
101 :param type_id: The flux or DOF type to unregister
102 :type type_id: str
104 :raise ValueError: Raises a ValueError when no flux or DOF type with
105 the given name is registered
106 """
107 if type_id in self._fluxes_types:
108 flux_type = type_id
109 dof_type = self._fluxes_types[flux_type]
111 elif type_id in self._dof_types:
112 dof_type = type_id
113 flux_type = self._dof_types[dof_type]
114 else:
115 raise ValueError(
116 str.format(
117 "No flux or dof type registered with {0}.",
118 str(type_id),
119 )
120 )
122 self._fluxes_types.pop(flux_type)
123 self._dof_types.pop(dof_type)
126__flux_dof_register = FluxDofTypesRegister()
129def get_flux_dof_register() -> FluxDofTypesRegister:
130 """
131 Get the unique register storing the relation between flux types
132 and DOF types.
134 :return: The register mapping flux types with DOF types
135 :rtype: FluxDofTypesRegister
136 """
137 return __flux_dof_register
140@dataclass
141class Dof:
142 """
143 Degrees of freedom description.
144 """
146 dof_id: str
147 """The id of the dof"""
149 dof_type: str
150 """The type of dof (ex: pressure, chemical, etc)"""
152 def __eq__(self, value: object) -> bool:
153 if isinstance(value, Dof):
154 return value.dof_id == self.dof_id
155 if isinstance(value, str):
156 return value == self.dof_id
157 return False