Skip to content

Commit 592f193

Browse files
refactor: import sort logic from tools/sort_ref_json instead of duplicating
1 parent a035172 commit 592f193

3 files changed

Lines changed: 20 additions & 32 deletions

File tree

flow360/_autohooks/sort_json.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Autohooks plugin: sort keys in staged JSON files under tests/."""
22

3-
import json
3+
import importlib.util
44
from pathlib import Path
55

66
from autohooks.api import ok
@@ -11,32 +11,16 @@
1111
)
1212
from autohooks.api.path import match
1313

14+
_TOOLS_DIR = Path(__file__).resolve().parent.parent.parent / "tools"
15+
_spec = importlib.util.spec_from_file_location("sort_ref_json", _TOOLS_DIR / "sort_ref_json.py")
16+
_module = importlib.util.module_from_spec(_spec)
17+
_spec.loader.exec_module(_module)
18+
process_file = _module.process_file
19+
1420
DEFAULT_INCLUDE = ("*.json",)
1521
TESTS_DIR = Path(__file__).resolve().parent.parent.parent / "tests"
1622

1723

18-
def _sort_keys(obj):
19-
"""Recursively sort dictionary keys. List order is preserved."""
20-
if isinstance(obj, dict):
21-
return {k: _sort_keys(v) for k, v in sorted(obj.items())}
22-
if isinstance(obj, list):
23-
return [_sort_keys(item) for item in obj]
24-
return obj
25-
26-
27-
def _sort_file(path: Path) -> bool:
28-
"""Sort keys in-place. Returns True if file was already sorted."""
29-
raw = path.read_text(encoding="utf-8")
30-
data = json.loads(raw)
31-
sorted_content = json.dumps(_sort_keys(data), indent=4) + "\n"
32-
33-
if raw == sorted_content:
34-
return True
35-
36-
path.write_text(sorted_content, encoding="utf-8")
37-
return False
38-
39-
4024
def precommit(config=None, report_progress=None, **kwargs): # pylint: disable=unused-argument
4125
"""Sort JSON keys in staged test reference files."""
4226
files = [
@@ -54,14 +38,11 @@ def precommit(config=None, report_progress=None, **kwargs): # pylint: disable=u
5438

5539
with stash_unstaged_changes(files):
5640
for f in files:
57-
try:
58-
already_sorted = _sort_file(f.absolute_path())
59-
if already_sorted:
60-
ok(f"Already sorted: {f.path}")
61-
else:
62-
ok(f"Sorted keys in: {f.path}")
63-
except (json.JSONDecodeError, UnicodeDecodeError):
64-
ok(f"Skipped (not valid JSON): {f.path}")
41+
already_sorted = process_file(f.absolute_path(), check=False)
42+
if already_sorted:
43+
ok(f"Already sorted: {f.path}")
44+
else:
45+
ok(f"Sorted keys in: {f.path}")
6546

6647
if report_progress:
6748
report_progress.update()

flow360/component/simulation/models/volume_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,12 @@ class BETDisk(MultiConstructorBaseModel):
819819
+ "Must be orthogonal to the rotation axis (Cylinder.axis). Only the direction is used—the "
820820
+ "vector need not be unit length. Must be specified for unsteady BET Line (blade_line_chord > 0).",
821821
)
822+
collective_pitch: AngleType = pd.Field(
823+
0 * u.deg,
824+
description="Collective pitch angle applied as a uniform offset to all blade twist values. "
825+
+ "Positive value increases the angle of attack at every radial station.",
826+
json_schema_extra={"exclude_if_default": True},
827+
)
822828
tip_gap: Union[Literal["inf"], LengthType.NonNegative] = pd.Field(
823829
"inf",
824830
description="Dimensional distance between blade tip and solid bodies to "

flow360/component/simulation/translator/solver_translator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,10 +1367,11 @@ def bet_disk_translator(model: BETDisk, is_unsteady: bool):
13671367
"""BET disk translator"""
13681368
model_dict = convert_tuples_to_lists(remove_units_in_dict(dump_dict(model)))
13691369
model_dict["alphas"] = [alpha.to("degree").value.item() for alpha in model.alphas]
1370+
collective_pitch_deg = model.collective_pitch.to("degree").value.item()
13701371
model_dict["twists"] = [
13711372
{
13721373
"radius": bet_twist.radius.value.item(),
1373-
"twist": bet_twist.twist.to("degree").value.item(),
1374+
"twist": bet_twist.twist.to("degree").value.item() + collective_pitch_deg,
13741375
}
13751376
for bet_twist in model.twists
13761377
]

0 commit comments

Comments
 (0)