Skip to content

[WIP] Convert OpenFOAM templates to resource-based .txt templates#4

Closed
julianschmoll with Copilot wants to merge 1 commit into
openfoam-calculix-test-couplingfrom
copilot/convert-f-strings-to-templates
Closed

[WIP] Convert OpenFOAM templates to resource-based .txt templates#4
julianschmoll with Copilot wants to merge 1 commit into
openfoam-calculix-test-couplingfrom
copilot/convert-f-strings-to-templates

Conversation

Copilot AI commented Jan 23, 2026

Copy link
Copy Markdown

Coding agent has begun work on Convert OpenFOAM templates to resource-based .txt templates and use str.format and will update this pull request as work progresses.

Original prompt

please do the rest as well for the rest of the functions. replace the long f strings with a txt file in the resources folder and use .format and the template loading method

TITLE: Convert OpenFOAM templates to resource-based .txt templates and use str.format

USER INTENT:

  • Replace inline Python f-strings / dict-style placeholders used to generate OpenFOAM files with plain text templates placed in resources/openfoam/*.txt and load/format them at runtime using Python's str.format.
  • Ensure templates use named placeholders, escape literal braces, and validate generated content.
  • Apply this consistently across blockMesh, U, and the remaining field-generation functions in openfoam/generate.py.

TASK DESCRIPTION:

  • Convert existing template generation code that uses inline f-strings or Python-dict-style placeholders into separate .txt templates stored under resources/openfoam/.
  • Use Python's str.format with named placeholders to fill templates (avoid dict-style or f-strings inside code).
  • Escape literal braces in the templates by doubling them ({{ and }}).
  • Ensure placeholder names are consistent with the keys provided by the generation code.
  • Add or keep small runtime checks to validate the produced files contain required tokens.
  • Replace all "long f-strings" in openfoam/generate.py with calls to a template-loading-and-formatting helper that reads resources/openfoam/.txt and runs .format(mapping).
  • Do this for blockMesh, U, and all other field/template-generating functions.

EXISTING (what's been done so far):

  • blockMesh template updated to a .txt template using named placeholders (final iteration uses Lx, Ly, Lz, nCellsX, nCellsY, nCellsZ). File path: resources/openfoam/blockMesh.txt
  • U template created as resources/openfoam/U.txt using named placeholders for boundary condition blocks: {left_bc}, {right_bc}, {bottom_bc}, {top_bc}. All literal braces in OpenFOAM syntax were doubled to survive Python str.format.
  • In openfoam/generate.py, a generate_U implementation was suggested that reuses generate_field(c, 'U') and performs a basic validation check via _validate_field_content to ensure required tokens exist in the generated content.

PENDING (what still needs to be done):

  • Replace all other inline template generation code (other field files and any remaining f-strings) in openfoam/generate.py with .txt templates in resources/openfoam/ and a template-loading/formatting helper.
  • Ensure placeholder names in each .txt file match the keys used in format(mapping) by the generator.
  • Implement the template loader function (e.g., load_template(name) -> text), and update each generate_* function to:
    1. read the appropriate resources/openfoam/.txt,
    2. call text.format(**mapping) with a mapping derived from the config object c,
    3. run validation checks similar to _validate_field_content for each file type.
  • Add or update tests/CI to ensure formatting doesn't fail (missing placeholders cause KeyError) and generated files contain the expected tokens/structures.
  • Confirm and update any code that previously relied on dict-style placeholders (the earliest assistant reply used physicalSizeX/physicalSizeY) to use the final chosen names (Lx/Ly/Lz).
  • Review and unify boundary condition placeholder conventions (e.g., left_bc vs explicit bc block text). Decide how mapping will supply the full boundary-block strings (the template expects full bc blocks like "type fixedValue;\nvalue uniform (0 0 0);" or similar).
  • Ensure escaping of any braces in templates is correct across all files.

CODE STATE (files discussed or modified, with important snippets):

  1. resources/openfoam/blockMesh.txt (final template produced in conversation)
convertToMeters 1;

vertices
(
    (0 0 0)                      // 0
    ({Lx} 0 0)                   // 1
    ({Lx} {Ly} 0)                // 2
    (0 {Ly} 0)                   // 3
    (0 0 {Lz})                   // 4
    ({Lx} 0 {Lz})                // 5
    ({Lx} {Ly} {Lz})             // 6
    (0 {Ly} {Lz})                // 7
);

blocks
(
    hex (0 1 2 3 4 5 6 7) ({nCellsX} {nCellsY} {nCellsZ}) simpleGrading (1 1 1)
);

edges
();

boundary
(
    left
    {{
        type patch;
        faces
        (
            (0 4 7 3)
        );
    }}
    right
    {{
        type patch;
        faces
        (
            (1 2 6 5)
        );
    }}
    bottom
    {{
        type wall;
        faces
        (
            (0 1 5 4)
        );
    }}
    top
    {{
        type wall;
        faces
        (
            (3 7 6 2)
        );
    }}
    frontAndBack
    {{
        type empty;
        faces
        (
            (0 3 2 1)
            (4 5 6 7)
        );
    }}
);

mergePatchPairs
();

Notes: Literal OpenFOAM braces are doubled ({{ and }}) so that str.format can be used safely. Required placeholders: Lx, Ly, Lz, nCellsX, nCellsY, nCellsZ.

  1. resources/openfoam/U.txt (final U template produced)
dimensions      [0 1 -1 0 0 0 0];

internalFi...

</details>



<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/julianschmoll/numsim/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

@julianschmoll

Copy link
Copy Markdown
Owner

insane but wrong xD

@julianschmoll julianschmoll deleted the copilot/convert-f-strings-to-templates branch January 23, 2026 21:21
Copilot AI requested a review from julianschmoll January 23, 2026 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants