# Using Automatic Differentiation with FinEtoolsFlexStructures.jl

**URL:** https://discourse.julialang.org/t/using-automatic-differentiation-with-finetoolsflexstructures-jl/125198
**Category:** Modelling & Simulations
**Created:** [January 25, 2025, 3:25pm UTC](https://discourse.julialang.org/t/using-automatic-differentiation-with-finetoolsflexstructures-jl/125198 "2025-01-25T15:25:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Edan\_Lazerson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edan_lazerson/32/214433_2.png) [@Edan\_Lazerson](https://discourse.julialang.org/u/Edan_Lazerson)
#### Post date: [January 25, 2025, 3:25pm UTC](https://discourse.julialang.org/t/using-automatic-differentiation-with-finetoolsflexstructures-jl/125198/1 "2025-01-25T15:25:22Z")

</div>

Hello everyone,

I am exploring the use of automatic differentiation (AD) with the **FinEtoolsFlexStructures.jl** package and would like to know if anyone has experience with this. Specifically, I aim to compute the derivative of the stiffness matrix K with respect to element thickness t.

Here is a naive approach I’ve tried so far:

1. Defined a `femm` function:

```julia
femm(thickness) = formul.make(IntegDomain(fes, TriRule(1), thickness), mater)

```

1. Created a function to calculate K(t):

```julia
function KT(thickness)
    femm = femm(thickness)
    associategeometry!(femm, geom0)
    K = stiffness(femm, massem, geom0, u0, Rfield0, dchi)
end

```

1. Used the `DifferentiationInterface` to compute the derivative:

```julia
derivative(KT, backend, 0.1)

```

I experimented with a few different backends, but without much success so far.

Eventually, my goal is to compute the gradients of elements with respect to thickness and node location for shape and thickness optimization. However, I currently have limited knowledge of both **FinEtoolsFlexStructures.jl** and **DifferentiationInterface**.

Has anyone attempted something similar, or could you provide insights into how to approach this problem? Any guidance or suggestions would be greatly appreciated!

Thank you!

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [January 27, 2025, 6:56am UTC](https://discourse.julialang.org/t/using-automatic-differentiation-with-finetoolsflexstructures-jl/125198/3 "2025-01-27T06:56:23Z")

</div>

Hi @Edan_Lazerson!

> [@Edan\_Lazerson](#):
>
> I experimented with a few different backends, but without much success so far.

Can you share the errors you got with the various backends you tried? Ideally in a completely reproducible script (including all imports, function definitions and variable bindings)?  
My best guess is that [FinEtoolsFlexStructures.jl](https://github.com/PetrKryslUCSD/FinEtoolsFlexStructures.jl) has some code inside which is not autodiff-friendly. Typically, if you tried ForwardDiff.jl first as a backend (like you should), you might have run into a failed conversion from `Dual` to `Float64`. Seeing that error would allow us to figure out which action to take.

---

<div class="post-metadata">

### Author: ![Edan\_Lazerson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edan_lazerson/32/214433_2.png) [@Edan\_Lazerson](https://discourse.julialang.org/u/Edan_Lazerson)
#### Post date: [January 27, 2025, 6:32pm UTC](https://discourse.julialang.org/t/using-automatic-differentiation-with-finetoolsflexstructures-jl/125198/4 "2025-01-27T18:32:30Z")

</div>

You’re correct—it was indeed due to the use of `Float64`, which caused the error. Petr Krysl, the author of the package, is currently checking the issue to see if it can be resolved. Thanks.
