Physics model parametrization

Thinking about my first test project/learning with Julia I got this option: will receive a Windows DLL implementing a physics model, modelling the behavior of soft soil pushed from a vehicle.
Hi input will be:

  • height of the soft soil respect the terrain
  • physics model parameters
  • position and speed of the vehicle
  • position and rotation of the front blade if the vehicle pushing the soil
    The output will be
  • density if the soil in every grid point
  • force applied on the front blade
    I will get also a dataset with a “ground trouth” value of the terrain behavior in standard conditions.
    Wanted to kindly ask about packages and algorithm allowing me to prototype an optimization system able to set the parameters of the physics model better mimicking the ground trought.

Jump is a very powerful package and well documented: Getting started with JuMP · JuMP

But there are many others…

1 Like

Seems to be a great package, not sure if can be applicable to my case.
I didn’t know the underlying physics model embedded into the DLL, I just need to optimize his behavior comparing his results vs the ground truth simulation, tweaking his parameters treating him as a black box.

I can eventually write an analytical function that generate an “distance” comparing the two dataset (the one coming from the DLL and the ground truth) and try to find the minimum of that function if this is how is supposed to work with JuMP.

Next week will have some data to explain better.

First step (I’m REALLY beginning…)

I have created this complex :grinning: static C++ library, that does compute x*x
How can I call it from Julia, pass a value and retreive the result?

All the other steps regarding the real physics library are data I/O and specific optimization problems, but without this I will not be able to work on the main problem

It will probably be easiest If you define an entrypoint in your c++ code as extern c function, that you then call via julia’s ccall.
You could also have a look at Cxx.jl and CxxWrap.jl.

3 Likes

Thanks @_bernhard

On the contrary of this self created micro test library, I will not have access to the real physics library source code, only to the compiled binary and eventually the header file.

Also the physics library is based on CUDA compute, so I soppose will be safer / easier just to call the binary.

Not sure if this will allow me to use one of the paths you suggested.

Well, without header file the compiled library would be useless.

And if you have the header file you can write a C wrapper yourself and call the functions of the C wrapper directly from Julia. But because the interface to C++ libraries is compiler specific you will need the same compiler that was used to create the library.

I guess CxxWrap would also work, but it is really useful only if your interface is complex.

2 Likes

Tryng to follow @_bernhard and @ufechner7 suggestions, but exporting and calling an external static library is something I have never done in my short past C and Python coder experience.

So starting from the fact in Julia prompt I’m in the library release directory
image

I tried to @ccall

julia> @ccall MathLibrary.Power2(2.0::Cdouble)::Cdouble
ERROR: UndefVarError: MathLibrary not defined

I tried also to modify the header file

// MathLibrary.h

#ifdef __cplusplus

extern "C" {

#endif

namespace MathLibrary
{
    class Arithmetic
    {
    public:
        // Returns a*a
        static double Power2(double a);

    };
}

__declspec(dllexport) void Power2();

#ifdef __cplusplus
}
#endif

But I’m really outside my knowledge and not sure what I’m really doing.

You should define a string like
lib = "MyLib.dll"
And then you can access your function through @ccall as @ccall lib.MyFunction(…

julia> lib = “MathLibrary.lib”
“MathLibrary.lib”

julia> @ccall lib.Power2(2.0::Cdouble)::Cdouble
ERROR: could not load library “MathLibrary.lib”
%1 is not a valid Win32 application.
Stacktrace:
[1] top-level scope
@ .\REPL[4]:1

Reading other threads I supposed was a problem since I used 64-bit Julia with 32 bit application, but the error remains the same both using 32-bit Julia with 32-bit app and 64-bit Julia with 64-bit app.

I think on Windows a shared library has the suffix .dll and not .lib .
Some background: Building Windows DLLs with MinGW

Both .dll and .lib are used in Windows. The .dll is the actual shared library, while the .lib (known as an “import library”) serves to communicate which symbols from the library are exported for use by other code. This is described in the “Import Libraries” section of your link.

Well, but can you use .lib files with Julia? I don’t think so.

Trying with the complete library I need to optimize I can confirm .lib calling didn’t work in Julia, .dll instead work.

Now trying to proceed on another topic: need to generate vertex, normals and index matrix for a 3d model (in fbx or obj like this simple parallepiped) and the library rely on assimp library to generate this data.
Assimp seems not well supported on Julia - or at least I didn’t find any way to call the library in some usable way - so wanted to ask if there is any pure Julia library able to generate these data.

Depends on the context. If you’re using PackageCompiler.jl on Windows to compile a Julia package to a shared library (or really in any context where you’re linking to shared libraries together in Windows), having to deal with both DLLs and their import libraries is pretty common. If you’re calling into C or C++ code from Julia, it’s more common to just work with the DLL directly.