Peano
Loading...
Searching...
No Matches
Quantities.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from typing import List, Union
4
5
7 solver,
8 quantities: List[Union[str, int]],
9) -> List[int]:
10 """!
11
12 Translate names and indices of the unknowns Q into sorted indices
13
14 @param solver: exahype2.api.Solver
15 The solver whose entries of Q we refer to.
16
17 @param quantities: List[str or int]
18 A string is the name of a variable as passed to the solver (e.g.
19 "lapse" or "G" for exagrype.api.FOCCZ4Solver) and selects all of its
20 entries. An int is the index of one entry of Q. The list must not be
21 empty.
22
23 @return List[int]
24 Sorted indices without duplicates.
25
26 """
27 entries = solver.unknowns
28 if len(quantities) == 0:
29 raise ValueError(f"Solver {solver.name}: the list of quantities is empty")
30 names = solver.variable_names_per_entry()[:entries]
31 indices = set()
32 for quantity in quantities:
33 if isinstance(quantity, bool) or not isinstance(quantity, (int, str)):
34 raise TypeError(
35 f"Solver {solver.name}: quantities have to be names (str) or "
36 f"indices (int) of entries of Q, but got {quantity!r}"
37 )
38 if isinstance(quantity, int):
39 if not 0 <= quantity < entries:
40 raise ValueError(
41 f"Solver {solver.name}: quantity index {quantity} is out of "
42 f"range, as only {entries} entries can be selected"
43 )
44 indices.add(quantity)
45 else:
46 matches = [
47 index
48 for index, name in enumerate(names)
49 if name == quantity or name.startswith(quantity + "[")
50 ]
51 if not matches:
52 raise ValueError(
53 f"Solver {solver.name}: {quantity!r} is not the name of a "
54 f"variable which can be selected. Available names are "
55 f"{sorted(set(name.split('[')[0] for name in names))}."
56 )
57 indices.update(matches)
58 return sorted(indices)
List[int] resolve_quantities(solver, List[Union[str, int]] quantities)
Translate names and indices of the unknowns Q into sorted indices.
Definition Quantities.py:9