Skip to content

Conversation

@MengnanLi91
Copy link
Contributor

@MengnanLi91 MengnanLi91 commented Jun 30, 2025

Reason

This PR will develop a MOOSE FMU interface

Design

The current MOOSE FMU interface will leverage web server control capability to communicate with MOOSE simulation. It requires a running MOOSE simulation on side.

Impact

This will be the first step to pack MOOSE as a FMU and integrates with other FMUs for co-simulation

@MengnanLi91 MengnanLi91 self-assigned this Jun 30, 2025
@MengnanLi91 MengnanLi91 force-pushed the MOOSE_FMI branch 2 times, most recently from d106c24 to 1e2adbe Compare June 30, 2025 19:58
@MengnanLi91
Copy link
Contributor Author

MengnanLi91 commented Sep 17, 2025

MengnanLi91 added a commit to MengnanLi91/moose that referenced this pull request Oct 6, 2025
@MengnanLi91 MengnanLi91 force-pushed the MOOSE_FMI branch 3 times, most recently from 42a1b26 to 2b49506 Compare October 7, 2025 15:24
MengnanLi91 added a commit to MengnanLi91/moose that referenced this pull request Oct 7, 2025
Python unit tests need to use platform and
do not have capabilities available
@MengnanLi91 MengnanLi91 marked this pull request as ready for review October 8, 2025 15:43
@moosebuild
Copy link
Contributor

Job Precheck, step Versioner verify on afb834d wanted to post the following:

Versioner templates

Found 14 templates, 0 failed

Versioner influential files

Found 58 influential files, 3 changed, 0 added, 0 removed

package status file
tools CHANGE conda/tools/meta.yaml
moose-dev CHANGE conda/moose-dev/conda_build_config.yaml
moose-dev CHANGE conda/moose-dev/meta.yaml

Versioner versions

Found 9 packages, 2 changed, 0 failed

package status hash to hash version to version
tools CHANGE d73012a a6ed2b3 2025.06.26 2025.10.07
moose-dev CHANGE 32e04eb 4dbab7a 2025.09.18 2025.10.07

Copy link
Member

@loganharbour loganharbour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we sit together tomorrow briefly for you to show me an example with this? Would help with the review quite a bit

@MengnanLi91
Copy link
Contributor Author

Can we sit together tomorrow briefly for you to show me an example with this? Would help with the review quite a bit

Of course! Let's do that

@@ -0,0 +1,145 @@
# MOOSE FMU Interface

The `MOOSEFMU` defines the `Moose2FMU` base class which contains the boilerplate needed to wrap a MOOSE simulation as a Functional Mock-up Unit (FMU). Users only need to implement their own `__init__` and `do_step` methods when deriving from `Moose2FMU`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `MOOSEFMU` defines the `Moose2FMU` base class which contains the boilerplate needed to wrap a MOOSE simulation as a Functional Mock-up Unit (FMU). Users only need to implement their own `__init__` and `do_step` methods when deriving from `Moose2FMU`.
The `MOOSEFMU` defines the `Moose2FMU` Python base class which contains the boilerplate needed to wrap a MOOSE simulation as a Functional Mock-up Unit (FMU). Users only need to implement their own `__init__` and `do_step` methods when deriving from `Moose2FMU`.

`Moose2FMU` accepts a number of keyword arguments that configure how the
underlying MOOSE simulation is launched and interacted with:

- `flag`: Optional flag that is forwarded to the MOOSE input deck.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to pass multiple?
-flag a -flag b ? or -flag a b ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the rules for spaces same as for CLI args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the rules for spaces same as for CLI args?

Yes, this is just pass CLI args to MOOSE control. I can add an example here to show it. I keep all the examples in moose_fmu folder. I can refer that here

`Moose2FMU` provides a small collection of convenience methods that simplify
interacting with a running MOOSE simulation:

- `set_controllable_real` and `set_controllable_vector` push new controllable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vectors are controllable now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MOOSE web server control, controllable_vector is supported so I just include the option here. Not quiet sure if controllable vector actually used anywhere in the code. Maybe under stochastic tools? Here I just provide APIs we have in MOOSE web server control

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check, let's get a test

`get_postprocessor_value` also accept per-call `flag` arguments. Passing custom
flags in these locations allows FMUs to react to user-defined synchronization
points while still benefiting from the built-in defaults. Refer to
[SetupInterface.md]for additional details on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[SetupInterface.md]for additional details on
[SetupInterface.md] for additional details on

MOOSE execute flags.

### Creating a Custom FMU

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a script you could use a listing from instead?


Save the custom class in a Python file and use [`pythonfmu`](https://github.com/NTNU-IHB/PythonFMU) to build the FMU:

```bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we dont include this package in our conda distrib? not through fmpy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We include pythonfmu in conda, but to build a MOOSE fmu customized fmu input, output, you still use pythonfmu build command to do that. We don't need to install pythonfmu package every time we want to build fmu

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see a pythonfmu new package in conda?
Does it come with fmpy?

pythonfmu build custom_moose.py
```

The resulting `.fmu` file can then be imported into any compliant co-simulation environment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The resulting `.fmu` file can then be imported into any compliant co-simulation environment.
The resulting `.fmu` file can then be imported into any standard-compliant co-simulation environment.

@@ -0,0 +1,145 @@
# MOOSE FMU Interface

The `MOOSEFMU` defines the `Moose2FMU` base class which contains the boilerplate needed to wrap a MOOSE simulation as a Functional Mock-up Unit (FMU). Users only need to implement their own `__init__` and `do_step` methods when deriving from `Moose2FMU`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what standard did you implement for FMU ? 2? 3? do we have the whole standard implemented or partiaL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support FMU 2 standard. The whole standard is implemented by pythonFMU and we use that to build MOOSE FMU

Comment on lines +141 to +144
!syntax parameters /Controls/MOOSEFMU

!syntax inputs /Controls/MOOSEFMU

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see a MOOSEFMU class registered? this shouldt work, what am I missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll fix it

Copy link
Contributor

@GiudGiud GiudGiud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great. This is well organized and understandable at a good level

I think the next thing we need is a tutorial on MOOSE-FMU showing:

  • building the FMU
  • running it from python
  • controlling its stepping
  • controlling its inputs
  • plotting its outputs
  • coupling it to another FMU
    which basically matches the tests you created!

`Moose2FMU` provides a small collection of convenience methods that simplify
interacting with a running MOOSE simulation:

- `set_controllable_real` and `set_controllable_vector` push new controllable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check, let's get a test

fmu_utils.set_string(fmu, vr_map, "mode", "active")
fmu_utils.set_bool(fmu, vr_map, "enabled", False)

self.assertEqual(fmu.calls["getReal"], [(1,)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment for these 3, what is this checking? the call log?

fmu = _DummyFmu()
vr_map = {"temperature": 1, "mode": 2, "enabled": 3}

self.assertEqual(fmu_utils.get_real(fmu, vr_map, "temperature"), 12.5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you always need to give the vr_map to a get_something call? that seems inefficient

I would imagine fmu.getReal("temperature") would be enough information ?


Save the custom class in a Python file and use [`pythonfmu`](https://github.com/NTNU-IHB/PythonFMU) to build the FMU:

```bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see a pythonfmu new package in conda?
Does it come with fmpy?

@@ -0,0 +1,601 @@
from pythonfmu import Fmi2Slave
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the copyright header everywhere in the python files

#* This file is part of the MOOSE framework
#* https://mooseframework.inl.gov
#*
#* All rights reserved, see COPYRIGHT for full restrictions
#* https://github.com/idaholab/moose/blob/master/COPYRIGHT
#*
#* Licensed under LGPL 2.1, please see LICENSE for details
#* https://www.gnu.org/licenses/lgpl-2.1.html

(a) rebuild the FMU with the desired dt with DefaultExperiment, or
(b) drive the FMU step-by-step (manual do_step loop) instead of simulate_fmu.
- simulate_fmu integrates *up to* stop_time but does not necessarily perform a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is an issue with fmpy?

I would not expect a do_step at the stop time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to store this or can we generate it in the test suite using a RunCommand?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what test is using this file?


df = pd.DataFrame(result)

df.to_csv("run_fmu.csv", index=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I see how this handled to create a CSV

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyright on all these files

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.

4 participants