Adding an argument to slv.solve() for prediction of trained model

Hi,

I’m now trying to use PointwiseMonitor to predict my angle of attack of airfoil using my trained model.

In my code, the general structure is:

@modulus.main(config_path="conf", config_name=config_name)

def run(cfg: ModulusConfig) -> None:

adding BC, eqn constraints, inferencer, monitor etc
...

# make solver
slv = Solver(cfg, domain)

# start solver
slv.solve()

if __name__ == "__main__":
    run()

Now I plan to run in ‘eval’ mode and do prediction and adding some arguments like:

# make solver
slv = Solver(cfg, domain)

# start solver
slv.solve(angle_attack)

Is this possible? I tried but the variable “angle_attack” is not inserted correctly:

UnboundLocalError: local variable 'angle_attack' referenced before assignment

So how should I solve this problem?

Thank you.

Update on my problem.

I am now using slv.eval().

I am trying to modify the source code such that I can use:

slv.eval(AoA_test)

I tried to change /opt/conda/lib/python3.8/site-packages/modulus-22.9-py3.8.egg/modulus/solver/solver.py:

def eval(self,AoA_test):
        self._eval(AoA_test)

and /opt/conda/lib/python3.8/site-packages/modulus-22.9-py3.8.egg/modulus/trainer.py

def _eval(
        self,AoA_test
    ):

But somehow it still gives the same error. So what should be the way to insert argument?

UnboundLocalError: local variable ‘angle_attack’ referenced before assignment

This error means you havent defined this variable in your training script.

I am trying to modify the source code such that I can use:
slv.eval(AoA_test)

Instead of modifying Modulus-Sym source consider the following:

Create a numpy dictionary of inputs + attack angles such as {x:[0,0,0,0], y:[1,1,1,1], aoa:[0.25, 0.35, 0.45, 0.5]} to get the outputs at point (0,1). Then create a monitor from that dictionary. Then the solver.eval() will evaluate all the given configurations. You can then increase the batch size of the Monitor to increase the calculation speed.

Or just loop inside over all your AoA’s in your trainer and create a new Monitor / solver for each angle of attack (not a very good solution).

But if you want to do just querying that feeds into some other process, then maybe writing a custom monitor/util would be best (should be achievable)

Hi ngeneva,

I need to use slv.eval(AoA_test) within an optimization loop, to couple with an optimizer something like:

while opt_no <= 3:

        opt_no = opt_no + 1

        .. optmization routine

        lift = slv.eval(AoA)

        ...

The AoA will be determined on the fly during the optimization by the optimizer. That’s why I can’t create a numpy dict beforehand.

Hence, I thought maybe I can do a simple hack on the source code. Else, I will look into creating a new Pointwise monitor, which can accept an argument and also give a value immediately instead of saving to a csv file (and then we need to read it out).

Hi @tsltaywb

I see, yes for a in the loop type connection this hack would be the quickest. One thing to make sure isn’t happening is that your geometry is changing (Im assuming AoA is adjusting inlet velocity). These primitives that get added the domain sample their points once on initialization by default, so if your geometry (airfoil boundary in the x,y space) is changing then you need to re-initialize and resample. If your just changing the angle of u_infinity then you’re totally fine.

If this is something that you’re planning on spending a lot of time using, I would recommend trying to build your own Monitor for this (or even eval function) to make it a little more clean and so it can return the output in memory.

(me personally I would manually load the model on my own then call my custom monitor directly in this for loop, but this would require a little more familiarity with modulus-sym source code.)

@ngeneva Sure, thanks for the tips. Will give it a try.

I didn’t see it mentioned here but I came across it in the docs. It looks like you can also specify the following line in your Hydra config
run_mode: 'eval'

https://docs.nvidia.com/deeplearning/modulus/modulus-v2209/user_guide/features/configuration.html#config

1 Like