Skip to content

Commit

Permalink
[SCFD-2695] Reworked the manipulate restart mechanism (#575)
Browse files Browse the repository at this point in the history
* Reworked the manipulate restart mechanisim

* Fix format
  • Loading branch information
benflexcompute authored Nov 14, 2024
1 parent 4e4d3c2 commit 84cbf8b
Show file tree
Hide file tree
Showing 34 changed files with 483 additions and 63 deletions.
34 changes: 22 additions & 12 deletions flow360/component/simulation/models/volume_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ExpressionInitialConditionBase(Flow360BaseModel):
:paramref:`Fluid.initial_condition`.
"""

type: Literal["expression"] = pd.Field("expression", frozen=True)
type_name: Literal["expression"] = pd.Field("expression", frozen=True)
constants: Optional[Dict[str, StringExpression]] = pd.Field(
None, description="The expression for the initial condition."
)
Expand All @@ -96,21 +96,29 @@ class NavierStokesInitialCondition(ExpressionInitialConditionBase):
"""
:class:`NavierStokesInitialCondition` class for specifying the
:paramref:`Fluid.initial_condition`.
By default
"""

rho: StringExpression = pd.Field(description="Density")
u: StringExpression = pd.Field(description="X-direction velocity")
v: StringExpression = pd.Field(description="Y-direction velocity")
w: StringExpression = pd.Field(description="Z-direction velocity")
p: StringExpression = pd.Field(description="Pressure")
type_name: Literal["NavierStokesInitialCondition"] = pd.Field(
"NavierStokesInitialCondition", frozen=True
)
rho: StringExpression = pd.Field("rho", description="Density")
u: StringExpression = pd.Field("u", description="X-direction velocity")
v: StringExpression = pd.Field("v", description="Y-direction velocity")
w: StringExpression = pd.Field("w", description="Z-direction velocity")
p: StringExpression = pd.Field("p", description="Pressure")


class NavierStokesModifiedRestartSolution(NavierStokesInitialCondition):
type: Literal["restartManipulation"] = pd.Field("restartManipulation", frozen=True)
type_name: Literal["NavierStokesModifiedRestartSolution"] = pd.Field(
"NavierStokesModifiedRestartSolution", frozen=True
)


class HeatEquationInitialCondition(ExpressionInitialConditionBase):
type_name: Literal["HeatEquationInitialCondition"] = pd.Field(
"HeatEquationInitialCondition", frozen=True
)
temperature: StringExpression = pd.Field()


Expand Down Expand Up @@ -149,10 +157,12 @@ class Fluid(PDEModelBase):

material: FluidMaterialTypes = pd.Field(Air(), description="The material propetry of fluid.")

initial_condition: Optional[
Union[NavierStokesModifiedRestartSolution, NavierStokesInitialCondition]
] = pd.Field(
None, discriminator="type", description="The initial condition of the fluid solver."
initial_condition: Union[NavierStokesModifiedRestartSolution, NavierStokesInitialCondition] = (
pd.Field(
NavierStokesInitialCondition(),
discriminator="type_name",
description="The initial condition of the fluid solver.",
)
)

# pylint: disable=fixme
Expand Down
34 changes: 32 additions & 2 deletions flow360/component/simulation/translator/solver_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
ActuatorDisk,
BETDisk,
Fluid,
NavierStokesInitialCondition,
NavierStokesModifiedRestartSolution,
PorousMedium,
Rotation,
Solid,
Expand Down Expand Up @@ -65,6 +67,7 @@
update_dict_recursively,
)
from flow360.component.simulation.unit_system import LengthType
from flow360.component.simulation.utils import is_exact_instance
from flow360.exceptions import Flow360TranslationError


Expand Down Expand Up @@ -771,6 +774,32 @@ def boundary_spec_translator(model: SurfaceModelTypes, op_acousitc_to_static_pre
return boundary


def get_navier_stokes_initial_condition(
initial_condition: Union[NavierStokesInitialCondition, NavierStokesModifiedRestartSolution]
):
"""Translate the initial condition for NavierStokes"""
if is_exact_instance(initial_condition, NavierStokesInitialCondition):
initial_condition_dict = {
"type": "initialCondition",
}
elif is_exact_instance(initial_condition, NavierStokesModifiedRestartSolution):
initial_condition_dict = {
"type": "restartManipulation",
}
else:
raise Flow360TranslationError(
f"Invalid NavierStokes initial condition type: {type(initial_condition)}",
input_value=initial_condition,
location=["models"],
)
raw_dict = dump_dict(initial_condition)
for key in ["rho", "u", "v", "w", "p", "constants"]:
if key not in raw_dict: # not set
continue
initial_condition_dict[key] = raw_dict[key]
return initial_condition_dict


# pylint: disable=too-many-statements
# pylint: disable=too-many-branches
# pylint: disable=too-many-locals
Expand Down Expand Up @@ -923,8 +952,9 @@ def get_solver_json(
}
)

if model.initial_condition:
translated["initialCondition"] = dump_dict(model.initial_condition)
translated["initialCondition"] = get_navier_stokes_initial_condition(
model.initial_condition
)

##:: Step 7: Get BET and AD lists
if has_instance_in_list(input_params.models, BETDisk):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

from flow360.component.simulation.models.solver_numerics import NoneSolver
from flow360.component.simulation.models.surface_models import SurfaceModelTypes, Wall
from flow360.component.simulation.models.volume_models import (
Fluid,
NavierStokesInitialCondition,
Rotation,
Solid,
)
from flow360.component.simulation.models.volume_models import Fluid, Rotation, Solid
from flow360.component.simulation.outputs.outputs import (
IsosurfaceOutput,
ProbeOutput,
Expand Down Expand Up @@ -260,18 +255,6 @@ def _validate_cht_has_heat_transfer(params):
"In `Solid` model, the initial condition needs to be specified "
"for unsteady simulations."
)

for model in params.models:
if isinstance(model, Fluid) and isinstance(
model.initial_condition, NavierStokesInitialCondition
):
for model_solid in params.models:
if isinstance(model_solid, Solid) and model_solid.initial_condition is None:
raise ValueError(
"In `Solid` model, the initial condition needs to be specified "
"when the `Fluid` model uses expression as initial condition."
)

return params


Expand Down
8 changes: 8 additions & 0 deletions tests/ref/simulation/service_init_geometry.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@
}
},
"type": "Fluid",
"initial_condition": {
"type_name": "NavierStokesInitialCondition",
"rho": "rho",
"u": "u",
"v": "v",
"w": "w",
"p": "p"
},
"navier_stokes_solver": {
"absolute_tolerance": 1e-10,
"relative_tolerance": 0.0,
Expand Down
19 changes: 13 additions & 6 deletions tests/ref/simulation/service_init_volume_mesh.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@
{
"type": "Wall",
"entities": {
"stored_entities": []
"stored_entities": []
},
"name": "Wall",
"use_wall_function": false
},
{
"type": "Freestream",
"entities": {
"stored_entities": []
"stored_entities": []
},
"name": "Freestream"
},
Expand All @@ -104,6 +104,14 @@
}
}
},
"initial_condition": {
"type_name": "NavierStokesInitialCondition",
"rho": "rho",
"u": "u",
"v": "v",
"w": "w",
"p": "p"
},
"type": "Fluid",
"navier_stokes_solver": {
"absolute_tolerance": 1e-10,
Expand Down Expand Up @@ -156,7 +164,7 @@
"rotation_correction": false
},
"transition_model_solver": {
"type_name": "None"
"type_name": "None"
}
}
],
Expand Down Expand Up @@ -193,8 +201,7 @@
"private_attribute_registry_bucket_name": "SurfaceEntityType",
"private_attribute_entity_type_name": "Surface",
"name": "*",
"private_attribute_sub_components": [
]
"private_attribute_sub_components": []
}
]
},
Expand All @@ -208,4 +215,4 @@
"units": "m"
}
}
}
}
11 changes: 0 additions & 11 deletions tests/simulation/params/test_validators_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,6 @@ def test_cht_solver_settings_validator(
outputs=[surface_output_with_residual_heat_solver],
)

message = "In `Solid` model, the initial condition needs to be specified "
"when the `Fluid` model uses expression as initial condition."

# Invalid simulation params
with SI_unit_system, pytest.raises(ValueError, match=re.escape(message)):
_ = SimulationParams(
models=[fluid_model_with_initial_condition, solid_model_without_initial_condition],
time_stepping=Steady(),
outputs=[surface_output_with_residual_heat_solver],
)


def test_transition_model_solver_settings_validator():
transition_model_solver = TransitionModelSolver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,13 @@
"thermalConductivity": 0.0239367,
"volumetricHeatSource": 0.001
}
},
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,13 @@
"animationFrequencyTimeAverage": -1,
"animationFrequencyTimeAverageOffset": 0,
"startAverageIntegrationStep": -1
},
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,13 @@
],
"outputFormat": "paraview",
"startAverageIntegrationStep": -1
},
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
8 changes: 8 additions & 0 deletions tests/simulation/translator/ref/Flow360_XV15HoverMRF.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,13 @@
"animationFrequencyTimeAverage": -1,
"animationFrequencyTimeAverageOffset": 0,
"startAverageIntegrationStep": -1
},
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
10 changes: 9 additions & 1 deletion tests/simulation/translator/ref/Flow360_actuator_disk.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,13 @@
"circumferential":[-0.0001, -0.003, 0]
}
}
]
],
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
8 changes: 8 additions & 0 deletions tests/simulation/translator/ref/Flow360_heatFluxCylinder.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,13 @@
"animationFrequencyTimeAverage": -1,
"animationFrequencyTimeAverageOffset": 0,
"startAverageIntegrationStep": -1
},
"initialCondition": {
"p": "p",
"rho": "rho",
"type": "initialCondition",
"u": "u",
"v": "v",
"w": "w"
}
}
Loading

0 comments on commit 84cbf8b

Please # to comment.