carbonfly.field_writer module

Writers for OpenFOAM 0/ field files.

This module generates OpenFOAM initial/boundary condition files under the 0/ directory (e.g., 0/U, 0/T, 0/p, 0/CO2) based on patch-level field specifications.

It is designed to work together with carbonfly.boundary, where each patch spec object exposes a to_dict() representation (e.g., fixedValue, inletOutlet, wall functions). The writer converts these specs into OpenFOAM-compatible boundaryField { … } blocks and assigns a suitable internalField.

carbonfly.field_writer.write_0_field(case_root: Path, field_name: str, internal_value: Any | None, patch_specs: Dict[str, Any], *, dimensions: str | None = None, infer_internal_when_none: bool = False) Path[source]

Write a single field file under 0/ (e.g. 0/U, 0/T, 0/CO2 …).

Parameters:
  • case_root (Path) – Case root path.

  • field_name (str) – Field name (e.g., “U”, “T”, “CO2”).

  • internal_value (Any, optional) – Internal field value (scalar or vec3).

  • patch_specs (Dict[str, Any]) – Mapping patch_name -> field spec object.

  • dimensions (str, optional) – Override dimensions string. If None, uses defaults by name.

  • infer_internal_when_none (bool) – If True and internal_value is None, infer from patch specs.

Returns:

Path to the written 0/<field_name> file.

Return type:

Path

carbonfly.field_writer.write_fields_batch(case_root: Path, fields: Dict[str, Dict[str, Any]], internal_values: Dict[str, Any] | None = None, *, dimensions_map: Dict[str, str] | None = None, infer_internal_when_none: bool = False) Dict[str, Path][source]

Convenience function to write multiple 0/ field files at once.

Parameters:
  • case_root (Path) – Case root path.

  • fields (Dict[str, Dict[str, Any]]) – Mapping field_name -> (patch_name -> spec).

  • internal_values (Dict[str, Any], optional) – Mapping field_name -> internalField value.

  • dimensions_map (Dict[str, str], optional) – Mapping field_name -> dimensions string.

  • infer_internal_when_none (bool) – If True, infer internal values when missing.

Example

fields = {

“U”: {“inlet”: FieldFV((0, 0, -1)), “wall”: FieldFV((0, 0, 0))}, “T”: {“inlet”: FieldFV(293.15), “wall”: FieldZG()}, “CO2”: {“inlet”: FieldFV(4.5e-4)},

} write_fields_batch(case_root, fields)

Returns:

Mapping field_name -> written file path.

Return type:

Dict[str, Path]