Open bhaveshshrimali opened 3 years ago
Hi @bhaveshshrimali
S(F) = ∇(ψ)(F)
This does not work since the AD machinery is for functions taking Point
i.e. VectorValue
as argument, not 2nd order tensors.
To auto generate a residual and a jacobian you need to do something like:
energy(u) = ∫( ψ∘F∘∇(u) )*dΩ # You also will need to add the external loads here
res(u,v) = gradient(energy,u)
jac(u,du,v) = hessian(energy,u) # I have realized that you have to explicitly build the jacobian like this
op = FEOperator(res, jac, U, V)
I have done some tests and it seems that the compilation of the jacobian is EXTREMELY slow, several minutes. The compilation of the residual is much more acceptable.
BTW, μ
and λ
are not defined in the MWE. Can you update the code with some values?
res(u,v) = gradient(energy,u) jac(u,du,v) = hessian(energy,u)
Thanks a lot @fverdugo ! Indeed this seems to work and not throw the error anymore. Is building the jacobian jac
explicitly needed? Or can I just leave it to be computed under the hood. And is that different from this? I guess not, right?
A side question: how are you computing the individual time for the jacobian compilation? TimerOutputs
? And is this (the slowness) an issue with ForwardDiff
?
BTW,
μ
andλ
are not defined in the MWE. Can you update the code with some values?
Done.
A side question: how are you computing the individual time for the jacobian compilation?
I explicitly call jac
.
u = zero(U)
v = get_cell_shapefuns(V)
du = get_cell_shapefuns_trial(U)
j = jac(u,du,v)
It takes ages.
r = res(u,v)
is much faster
And is this (the slowness) an issue with ForwardDiff?
Possibly, but I don't know.
Consider for instance a usual hyperelasticity problem (#398) which is basically finding the minimizer of a scalar potential energy (Pi): $\arg\min_u \Pi (u)$
It would be useful to know constructs to be able to directly calculate the residual from the energy using automatic differentiation (The residual to jacobian part is already taken care of by Gridap). This is particularly useful in more complicated constitutive relations, where calculating the residual by hand would be time-consuming and prone to algebraic errors.
MWE
Problem:
How to calculate the gradient of the energy? If I change the definition of residual to
everything works fine as it should. Also if I calculate the energy from the solution
uh
, namelyI get the correct result. It is just that the calculation of the residual from the energy using AD that is proving to be tricky.
Sorry for a bit long post, but I hope that the issue is clear :)