Skip to content

Commit

Permalink
pyfabm: use correct metadata for coupled diagnostics (#76)
Browse files Browse the repository at this point in the history
* pyfabm: use correct metadata for coupled diagnostics

* pyfabm test: verify all variable names are unique
  • Loading branch information
jornbr authored Jul 3, 2024
1 parent ffa5b2b commit 1e94bf9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
69 changes: 58 additions & 11 deletions src/pyfabm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,12 @@ def __init__(
self.model.fabm.variable_get_metadata(
variable_pointer, ATTRIBUTE_LENGTH, strname, strunits, strlong_name
)
name = strname.value.decode("ascii")
units = strunits.value.decode("ascii")
long_name = strlong_name.value.decode("ascii")
if name is None:
name = strname.value.decode("ascii")
if units is None:
units = strunits.value.decode("ascii")
if long_name is None:
long_name = strlong_name.value.decode("ascii")
self.properties = VariableProperties(self.model, self.variable_pointer)

self.name = name
Expand Down Expand Up @@ -730,11 +733,17 @@ class DiagnosticVariable(Variable):
def __init__(
self,
model: "Model",
name: str,
units: str,
long_name: str,
path: str,
variable_pointer: ctypes.c_void_p,
index: int,
horizontal: bool,
):
Variable.__init__(self, model, variable_pointer=variable_pointer)
Variable.__init__(
self, model, name, units, long_name, path, variable_pointer=variable_pointer
)
self.data = None
self.horizontal = horizontal
self.index = index
Expand All @@ -749,13 +758,13 @@ def output(self) -> bool:
return self.model.fabm.variable_get_output(self.variable_pointer) != 0

def setSave(self, value: bool):
self.model.fabm.set_variable_save(
self.model.pmodel,
vartype = (
HORIZONTAL_DIAGNOSTIC_VARIABLE
if self.horizontal
else INTERIOR_DIAGNOSTIC_VARIABLE,
self.index,
1 if value else 0,
else INTERIOR_DIAGNOSTIC_VARIABLE
)
self.model.fabm.set_variable_save(
self.model.pmodel, vartype, self.index, 1 if value else 0
)

save = property(fset=setSave)
Expand Down Expand Up @@ -1283,18 +1292,56 @@ def updateConfiguration(self, settings=None):
StateVariable(self, ptr, self._bottom_state[i, ...])
)
for i in range(ndiag_interior.value):
self.fabm.get_variable_metadata(
self.pmodel,
INTERIOR_DIAGNOSTIC_VARIABLE,
i + 1,
ATTRIBUTE_LENGTH,
strname,
strunits,
strlong_name,
strpath,
)
ptr = self.fabm.get_variable(
self.pmodel, INTERIOR_DIAGNOSTIC_VARIABLE, i + 1
)
self.interior_diagnostic_variables._data.append(
DiagnosticVariable(self, ptr, i + 1, False)
DiagnosticVariable(
self,
strname.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
strpath.value.decode("ascii"),
ptr,
i + 1,
False,
)
)
for i in range(ndiag_horizontal.value):
self.fabm.get_variable_metadata(
self.pmodel,
HORIZONTAL_DIAGNOSTIC_VARIABLE,
i + 1,
ATTRIBUTE_LENGTH,
strname,
strunits,
strlong_name,
strpath,
)
ptr = self.fabm.get_variable(
self.pmodel, HORIZONTAL_DIAGNOSTIC_VARIABLE, i + 1
)
self.horizontal_diagnostic_variables._data.append(
DiagnosticVariable(self, ptr, i + 1, True)
DiagnosticVariable(
self,
strname.value.decode("ascii"),
strunits.value.decode("ascii"),
strlong_name.value.decode("ascii"),
strpath.value.decode("ascii"),
ptr,
i + 1,
True,
)
)
for i in range(ndependencies_interior.value):
ptr = self.fabm.get_variable(self.pmodel, INTERIOR_DEPENDENCY, i + 1)
Expand Down
6 changes: 6 additions & 0 deletions util/developers/run_all_testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,13 @@ def test_pyfabm(args, testcases: Mapping[str, str]):
print(f" {case}... ", end="")
sys.stdout.flush()
m0d = pyfabm.Model(path)
counts = collections.Counter(v.name for v in m0d.variables).items()
dup = [v for v, c in counts if c > 1]
assert not dup, f"Duplicate variable names in 0D: {dup}"
m1d = pyfabm.Model(path, shape=(5,))
counts = collections.Counter(v.name for v in m1d.variables).items()
dup = [v for v, c in counts if c > 1]
assert not dup, f"Duplicate variable names in 1D: {dup}"
for m in (m0d, m1d):
m.cell_thickness = environment["cell_thickness"]
for d in m.dependencies:
Expand Down

0 comments on commit 1e94bf9

Please sign in to comment.