CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Coupling Openfoam solver and keras trained neutral network model (https://www.cfd-online.com/Forums/openfoam-programming-development/241657-coupling-openfoam-solver-keras-trained-neutral-network-model.html)

AhmedHafez March 11, 2022 04:38

Coupling Openfoam solver and keras trained neutral network model
 
Hello,
I try to couple simpleFoam solver and keras neutral network trained model.
Anyone have an idea to do so?

the following link suggest to use frugally-deep header library or #include <Python.h>

https://www.researchgate.net/publica...itutive_models

I installed frugally-deep and check it is working with ordinary C++ program
However when I try to implement the same in OpenFoam I got compilation error.

Anyone have an idea how to utilize frugally-deep in openFoam or even have an alternative way to use keras trained model.

below is added code to simpleFoam and the error
Code:

...........................
#include <fdeep/fdeep.hpp>
int main(int argc, char *argv[])
{
.........................
        const auto model = fdeep::load_model("fdeep_model.json");
        const auto result = model.predict(
        {fdeep::tensor(fdeep::tensor_shape(static_cast<std::size_t>(4)),
        std::vector<float>{1, 2, 3, 4})});
        std::cout << fdeep::show_tensors(result) << std::endl;
..........................
}

The error

Code:


In file included from /usr/local/include/fplus/fplus.hpp:21,
                from /usr/local/include/fdeep/common.hpp:31,
                from /usr/local/include/fdeep/fdeep.hpp:9,
                from adFoam.C:83:
/usr/local/include/fplus/optimize.hpp: In lambda function:
/usr/local/include/fplus/optimize.hpp:119:24: error: call of overloaded ‘sqrt(double&)’ is ambiguous
  119 |        return sqrt(acc);
      |                        ^
In file included from /usr/include/features.h:461,
                from /usr/include/x86_64-linux-gnu/c++/9/bits/os_defines.h:39,
                from /usr/include/x86_64-linux-gnu/c++/9/bits/c++config.h:528,
                from /usr/include/c++/9/cstdint:38,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/int16.H:40,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/int.H:40,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/label.H:42,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/labelList.H:44,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/UPstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Pstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/parRun.H:39,
                from /usr/lib/openfoam/openfoam2106/src/finiteVolume/lnInclude/fvCFD.H:4,
                from adFoam.C:77:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:143:1: note: candidate: ‘double sqrt(double)’
  143 | __MATHCALL (sqrt,, (_Mdouble_ __x));
      | ^~~~~~~~~~
In file included from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/scalar.H:42,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/zeroI.H:30,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/zero.H:157,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/UList.H:51,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/List.H:46,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/labelList.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/UPstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Pstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/parRun.H:39,
                from /usr/lib/openfoam/openfoam2106/src/finiteVolume/lnInclude/fvCFD.H:4,
                from adFoam.C:77:
/usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Scalar.H:184:11: note: candidate: ‘Foam::floatScalar Foam::sqrt(Foam::floatScalar)’
  184 | transFunc(sqrt)
      |          ^~~~
/usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/floatScalar.H:92:15: note: in definition of macro ‘transFunc’
  92 | inline Scalar func(const Scalar s)                              \
      |              ^~~~
In file included from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/scalar.H:43,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/zeroI.H:30,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/zero.H:157,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/UList.H:51,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/List.H:46,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/labelList.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/UPstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Pstream.H:45,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/parRun.H:39,
                from /usr/lib/openfoam/openfoam2106/src/finiteVolume/lnInclude/fvCFD.H:4,
                from adFoam.C:77:
/usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Scalar.H:184:11: note: candidate: ‘Foam::doubleScalar Foam::sqrt(Foam::doubleScalar)’
  184 | transFunc(sqrt)
      |          ^~~~
/usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/doubleScalar.H:93:15: note: in definition of macro ‘transFunc’
  93 | inline Scalar func(const Scalar s)                              \
      |              ^~~~
In file included from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/TimeState.H:41,
                from /usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/Time.H:50,
                from /usr/lib/openfoam/openfoam2106/src/finiteVolume/lnInclude/fvCFD.H:6,
                from adFoam.C:77:
/usr/lib/openfoam/openfoam2106/src/OpenFOAM/lnInclude/dimensionedScalar.H:68:19: note: candidate: ‘Foam::dimensionedScalar Foam::sqrt(const dimensionedScalar&)’
  68 | dimensionedScalar sqrt(const dimensionedScalar&);
      |                  ^~~~
make: *** [/usr/lib/openfoam/openfoam2106/wmake/rules/General/transform:35: Make/linux64GccDPInt32Opt/simple2Foam.o] Error 1


joshwilliams March 11, 2022 06:41

I think I recall trying Frugally deep but I could not wrap my head around it. In the end I ended up using keras2cpp. My supervisor's former student has a github repo where they use it and I found it pretty useful -- it is probably a good starting point for your implementation.


I find the keras2cpp to be much simpler (just one file that you include in compilation). It requires your network architecture in a json file, and then your weights as a .h5 file, which you can get like so:
Code:

with open("./cpp_models/model_arch.json", "w") as fout:
  fout.write(model.to_json())
model.save("cpp_models/model_weights.h5")


Then there is a file called `dump_to_simple_cpp.py' or something in the keras2cpp library which can be used to generate a model that can be read by the keras2cpp C++ code. I remember I had to make some modifications to this `dump' file as I think it had some issues recognising input layers, so I skipped them. There were a few other things specific to my case.

AhmedHafez March 11, 2022 13:33

Thank you for your reply ,I will check it.

Quote:

Originally Posted by joshwilliams (Post 823963)
I think I recall trying Frugally deep but I could not wrap my head around it. In the end I ended up using keras2cpp. My supervisor's former student has a github repo where they use it and I found it pretty useful -- it is probably a good starting point for your implementation.


I find the keras2cpp to be much simpler (just one file that you include in compilation). It requires your network architecture in a json file, and then your weights as a .h5 file, which you can get like so:
Code:

with open("./cpp_models/model_arch.json", "w") as fout:
  fout.write(model.to_json())
model.save("cpp_models/model_weights.h5")


Then there is a file called `dump_to_simple_cpp.py' or something in the keras2cpp library which can be used to generate a model that can be read by the keras2cpp C++ code. I remember I had to make some modifications to this `dump' file as I think it had some issues recognising input layers, so I skipped them. There were a few other things specific to my case.



All times are GMT -4. The time now is 17:38.