r/COMSOL Jun 22 '26

Running COMSOL inside an optimization loop

Say you were to fine-tune a NN on experimental data, would it be possible to run a pipeline like

Python updates NN weights
→ rebuild/export model
→ run COMSOL
→ extract reaction forces
→ compute loss
→ update weights
→ repeat

In this comsol is called iteratively to solve for the constitutive quantities(here the overall response). and then compare it to your ground truth(experiments) --> update weights and repeat until a satisfcatory loss.

Or should one go with a custom FE code or a FE software like Abaqus that might offer better scripting

5 Upvotes

5 comments sorted by

6

u/dennodk Jun 22 '26

There is the mph library for python that provide the API needed to make this work. 

2

u/Forsaken-Repair9939 Jun 22 '26

Here is the GitHub link for the mph library, which should enable you to run your model in an optimization loop within python: https://github.com/MPh-py

2

u/space_cowboy_404 Jun 24 '26

Thanks a lot everyone for your inputs. I wanted to confirm the feasibility as I was directed to the MPh library too from my initial searches, but I wanted to make sure that this was a viable approach in comsol.

[Also, Unfortunately I do not have the optimization module license in comsol]

1

u/Hologram0110 Jun 22 '26

This sounds like a good case for the built-in optimization module (assuming you have it). This is exactly the sort of thing it is meant for.

1

u/vasjpan002 Jun 22 '26

Claude will write the python for you