# Physics model parametrization

**URL:** <https://discourse.julialang.org/t/physics-model-parametrization/79529>\
**Category:** New to Julia\
**Created:** [April 15, 2022, 12:28pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529 "2022-04-15T12:28:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 15, 2022, 12:28pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/1 "2022-04-15T12:28:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 15, 2022, 12:52pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/2 "2022-04-15T12:52:45Z")

</div>

Jump is a very powerful package and well documented: [Getting started with JuMP · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_JuMP/#Getting-started-with-JuMP)

But there are many others…

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 16, 2022, 6:41pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/3 "2022-04-16T18:41:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 22, 2022, 7:22pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/4 "2022-04-22T19:22:49Z")

</div>

First step (I’m REALLY beginning…)

I have created [this](https://ebitblu.sharepoint.com/:u:/s/EBITmax_Shared/EU5vJmkLjPlCtpdMhboO2MEBE4DpWcg3T4THLHrPTHoYbQ?download=1) complex 😀 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

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [April 22, 2022, 7:35pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/5 "2022-04-22T19:35:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 22, 2022, 8:52pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/6 "2022-04-22T20:52:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 22, 2022, 9:54pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/7 "2022-04-22T21:54:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 26, 2022, 8:46pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/8 "2022-04-26T20:46:24Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/d/6/d6dda13695cb6023a2bdb3ad9e1c90883a12470d.png)

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

```julia
// 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.

---

<div class="post-metadata">

**Author:** ![sefffal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sefffal/32/23640_2.png) [@sefffal](https://discourse.julialang.org/u/sefffal)\
**Post date:** [April 26, 2022, 8:58pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/9 "2022-04-26T20:58:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 27, 2022, 7:11pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/10 "2022-04-27T19:11:33Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 28, 2022, 1:38am UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/11 "2022-04-28T01:38:33Z")

</div>

I think on Windows a shared library has the suffix .dll and not .lib .  
Some background: [Building Windows DLLs with MinGW](https://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/)

---

<div class="post-metadata">

**Author:** ![acarta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acarta/32/28474_2.png) [@acarta](https://discourse.julialang.org/u/acarta)\
**Post date:** [April 28, 2022, 9:27pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/12 "2022-04-28T21:27:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 29, 2022, 12:42am UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/13 "2022-04-29T00:42:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davide445](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide445/32/33681_2.png) [@davide445](https://discourse.julialang.org/u/davide445)\
**Post date:** [April 29, 2022, 9:21am UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/14 "2022-04-29T09:21:00Z")

</div>

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](https://ebitblu.sharepoint.com/:u:/s/EBITmax_Shared/EZU9bga-ud1MleTvlIVW5W4BneSYPToWiUOTnOzA6UipsA?download=1)) and the library rely on [assimp](https://github.com/assimp/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.

---

<div class="post-metadata">

**Author:** ![acarta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acarta/32/28474_2.png) [@acarta](https://discourse.julialang.org/u/acarta)\
**Post date:** [April 29, 2022, 1:53pm UTC](https://discourse.julialang.org/t/physics-model-parametrization/79529/15 "2022-04-29T13:53:29Z")

</div>

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.
