Hi, in the code below I am having the error:
File “/home/user/.local/lib/python3.10/site-packages/fenics_adjoint/blocks/solving.py”, line 6, in init
lhs = A.form
AttributeError: ‘Equation’ object has no attribute ‘form’
from dolfin import *
from dolfin_adjoint import *
import matplotlib.pyplot as plt
import numpy as np
# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True,
"eliminate_zeros": True,
"precompute_basis_const": True,
"precompute_ip_const": True}
# Stored strain energy density (compressible neo-Hookean model)
def psi(mu, Ic, J_, lmbda):
return (mu / 2) * (Ic - 3) - mu * ln(J_) + (lmbda / 2) * (ln(J_)) ** 2
def kinematics(u):
v = TestFunction(V_vector) # Test function
d = u.geometric_dimension()
I = Identity(d) # Identity tensor
F = variable(I + grad(u)) # Deformation gradient
C = F.T * F # Right Cauchy-Green tensor
# Invariants of deformation tensors
Ic = tr(C)
J_ = det(F)
return Ic, J_, v, F
def optimization(chi):
max_error = 0
count = 0
for i in max_disp:
u0 = Function(V_vector)
u = Function(V_vector)
step_size = np.linspace(0, i, 100, endpoint=True)
for j in step_size:
disp = Constant([0.0, j])
bcs, dss = bcs_(disp)
Ic, J_, v, F = kinematics(u)
# Stored strain energy density (compressible neo-Hookean model)
Psi = H((chi - 0.5), l2) * psi(mu1, Ic, J_, lmbda1) + (1 - H((chi - 0.5), l2)) * psi(mu2, Ic, J_, lmbda2)
# Total potential energy
Pi = Psi * dx
# Compute first variation of Pi (directional derivative about u in the direction of v)
dPi = derivative(Pi, u, v)
file2 = File("./Results" + "/stress-opt.pvd");
u.assign(u0)
solve(dPi == 0, u, bcs,
form_compiler_parameters=ffc_options)
u0.assign(u)
# Piola-Kirchhoff Stress
stress = diff(Psi, F)
stress_calculation = (project(stress, W_tensor))
file2 << stress_calculation
t = dot(stress, normal)
t = assemble(t[1] * dss(1))
count += 1
controls = File("./Results" + "/control_iterations_guess.pvd")
...
solver = IPOPTSolver(problem, parameters=parameters_ipopt) # , parameters=parameters_ipopt
f_opt = solver.solve()
return f_opt
opt_chi = optimization(chi)
I could not find the reason, I would be happy if you can help me, thanks!