How to compare two functions at each points

hello, i am new to the fenics project, and recently i have met a problem and i could not solve it.

Here’s what i want to do. I would like my code to solve the p whose value would go from 0 to 1 and it would not decrease while it attain the threshold value.

Here’s part of the code:

V = FunctionSpace(mesh, 'CG', 1)
W = VectorFunctionSpace(mesh, 'CG', 1)
WW = FunctionSpace(mesh, 'DG', 0)
p, q = TrialFunction(V), TestFunction(V)
u, v = TrialFunction(W), TestFunction(W)

unew, uold = Function(W), Function(W)
pnew, pold, Hold = Function(V), Function(V), Function(V)
E_du = ((1.0-pold)**2)*inner(grad(v),sigma(u))*dx
E_phi = (Gc*l*inner(grad(p),grad(q))+((Gc/l)+2.0*H(uold,unew,Hold))\
            *inner(p,q)-2.0*H(uold,unew,Hold)*q)*dx
p_disp = LinearVariationalProblem(lhs(E_du), rhs(E_du), unew, bc_u)
p_phi = LinearVariationalProblem(lhs(E_phi), rhs(E_phi), pnew, bc_phi)
solver_disp = LinearVariationalSolver(p_disp)
solver_phi = LinearVariationalSolver(p_phi)

while t<=1.0:
    t += deltaT
    load.t=t*u_r
    iter = 0
    err = 1

    while err > tol:
        iter += 1
        solver_disp.solve()
        solver_phi.solve()
        err_u = errornorm(unew,uold,norm_type = 'l2',mesh = None)
        err_phi = errornorm(pnew,pold,norm_type = 'l2',mesh = None)
        err = max(err_u,err_phi)
        
        uold.assign(unew)
        pold.assign(pnew)
        Hold.assign(project(psi(unew), WW))

unfortunately, in some cases, while the p value attain 1 it would still decrease back to 0, and that’s exactly what i don’t want. So i want to ask is there any method to control the value so that once the pnew reach like 0.8 and then it won’t decrease anymore, or the pnew less than pold at some points, and at these points, we take the values of pold.

If you can formulate the problem you’re solving for p as an optimization problem, you could use the PETScTAOSolver interface for bound-constrained optimization, as demonstrated here. Since it looks like you’re solving phase-field fracture, you might also take a look at the augmented Lagrangian formulation for the damage irreversibility constraint, published by Wick et al. a few years ago.

1 Like