|
3 | 3 | from pathlib import Path |
4 | 4 | from typing import Any, ClassVar |
5 | 5 |
|
6 | | -import numpy as np |
7 | 6 | from attrs import fields |
8 | 7 | from modflow_devtools.dfn import Dfn, Field |
9 | 8 | from packaging.version import Version |
10 | 9 | from xattree import asdict as xattree_asdict |
11 | 10 | from xattree import xattree |
12 | 11 |
|
13 | | -from flopy4.mf6.constants import FILL_DNODATA, MF6 |
| 12 | +from flopy4.mf6.constants import MF6 |
14 | 13 | from flopy4.mf6.spec import field, fields_dict, to_field |
| 14 | +from flopy4.mf6.utils.grid_utils import update_maxbound |
15 | 15 | from flopy4.uio import IO, Loader, Writer |
16 | 16 |
|
17 | | - |
18 | | -def update_maxbound(instance, attribute, new_value): |
19 | | - """ |
20 | | - Generalized function to update maxbound when period block arrays change. |
21 | | -
|
22 | | - This function automatically finds all period block arrays in the instance |
23 | | - and calculates maxbound based on the maximum number of non-default values |
24 | | - across all arrays. |
25 | | -
|
26 | | - Args: |
27 | | - instance: The package instance |
28 | | - attribute: The attribute being set (from attrs on_setattr) |
29 | | - new_value: The new value being set |
30 | | -
|
31 | | - Returns: |
32 | | - The new_value (unchanged) |
33 | | - """ |
34 | | - |
35 | | - period_arrays = [] |
36 | | - instance_fields = fields(instance.__class__) |
37 | | - for f in instance_fields: |
38 | | - if ( |
39 | | - f.metadata |
40 | | - and f.metadata.get("block") == "period" |
41 | | - and f.metadata.get("xattree", {}).get("dims") |
42 | | - ): |
43 | | - period_arrays.append(f.name) |
44 | | - |
45 | | - maxbound_values = [] |
46 | | - for array_name in period_arrays: |
47 | | - if attribute and attribute.name == array_name: |
48 | | - array_val = new_value |
49 | | - else: |
50 | | - array_val = getattr(instance, array_name, None) |
51 | | - |
52 | | - if array_val is not None: |
53 | | - array_data = ( |
54 | | - array_val if array_val.data.shape == array_val.shape else array_val.todense() |
55 | | - ) |
56 | | - |
57 | | - if array_data.dtype.kind in ["U", "S"]: # String arrays |
58 | | - non_default_count = len(np.where(array_data != "")[0]) |
59 | | - else: # Numeric arrays |
60 | | - non_default_count = len(np.where(array_data != FILL_DNODATA)[0]) |
61 | | - |
62 | | - maxbound_values.append(non_default_count) |
63 | | - if maxbound_values: |
64 | | - instance.maxbound = max(maxbound_values) |
65 | | - |
66 | | - return new_value |
67 | | - |
68 | | - |
69 | 17 | COMPONENTS = {} |
70 | 18 | """MF6 component registry.""" |
71 | 19 |
|
|
0 commit comments