Naruki-Ichihara / fenics_optimize

optfx is a module of the FEniCS computing platform for multiphysics optimization problems.
https://naruki-ichihara.github.io/fenics_optimize/
MIT License
6 stars 2 forks source link

Example in documentation gives 'function' object is not iterable #6

Closed coen1111 closed 2 years ago

coen1111 commented 2 years ago

Describe the bug I have copied the example in the documentation to an example.py file and have runned it in the Singularity container, which will result in TypeError: 'function' object is not iterable.

To Reproduce

Singularity> python3 example.py
No protocol specified
Calling FFC just-in-time (JIT) compiler, this may take some time.
Traceback (most recent call last):
  File "example.py", line 52, in <module>
    op.MMAoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)
  File "/fenics-optimize/fenics_optimize/Optimizer.py", line 35, in MMAoptimize
    for pack in zip(constraints, constraints_targets):
TypeError: 'function' object is not iterable
Singularity>

Changing in rule 52 to to HSLOptimize instead of MMAoptimize will result in a different error.

op.HSLoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)

Singularity> python3 example.py
No protocol specified
Traceback (most recent call last):
  File "example.py", line 52, in <module>
    op.HSLoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)
  File "/fenics-optimize/fenics_optimize/Optimizer.py", line 92, in HSLoptimize
    cl = [-1e19] * len(constraints_targets)
TypeError: object of type 'NoneType' has no len()

Expected behavior A flawless execution of the example.

Screenshots N.A.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

Naruki-Ichihara commented 2 years ago

Hello, coen1111. Thank you for your report. I have updated the example for the poisson problem in the docs. Would you try out the new example script? Thanks.

from dolfin import *
from dolfin_adjoint import *
import numpy as np
import fenics_optimize as op

m = 0.3    # Target rate of the material amount
p = 5      # Penalty parameter
eps = 1e-3 # Material lower bound
R = 0.1    # Helmholtz filter radius
n = 256    # Resolution

def k(a):
    return eps + (1 - eps) * a ** p

mesh = UnitSquareMesh(n, n)
X = FunctionSpace(mesh, 'CG', 1)
f = interpolate(Constant(1e-2), X)

class Left(SubDomain):
    def inside(self, x, on_boundary):
        gamma = 1/n + eps
        return x[0] == 0.0 and 0.5 - gamma < x[1] < 0.5 + gamma and on_boundary

U = FunctionSpace(mesh, 'CG', 1)
T = Function(U, name='Temperature')
t = TrialFunction(U)
dt = TestFunction(U)
bc = DirichletBC(U, Constant(0.0), Left())

@op.with_derivative([X])
def forward(x):
    rho = op.helmholtzFilter(x[0], X, R=R)
    rho.rename('label', 'control')
    a = inner(grad(t), k(rho)*grad(dt))*dx
    L = f*dt*dx
    A, b = assemble_system(a, L, bc)
    T_s = op.AMGsolver(A, b).solve(T, U, False)
    J = assemble(inner(grad(T_s), k(rho)*grad(T_s))*dx)
    return J

@op.with_derivative([X])
def constraint(xs):
    rho_bulk = project(Constant(1.0), X)
    rho_0 = assemble(rho_bulk*dx)
    rho_f = assemble(xs[0]*dx)
    rel = rho_f/rho_0
    return rel - m

problemSize = Function(X).vector().size()
x0 = np.ones(problemSize) * m

op.MMAoptimize(problemSize, x0, forward, [constraint], [0], maxeval=100, bounds=[0, 1], rel=1e-20)
coen1111 commented 2 years ago

Hi @Naruki-Ichihara,

Thanks for your response. I tried your updated example and it seems to work.

Greetings,

coen1111