How to make a "cutback" in a loop?

Hi,

I’m using legacy FEniCS and want to make a cutback in a loop, i.e., I want to change the step size in the loop where the problem is being solved.

The code is like this:

...
problem = NonlinearVariationalProblem(weakform, w, BC, J=a)
solver = NonlinearVariationalSolver(problem)

while (t <= t_total):
   t += dt
   (iteration, converged) = solver.solve()
   ...

and when the solver cannot solve the problem (error occurs), the loop ends and the simulation is also terminated.
But I want to change the step size (dt) since the error occurs, rather than terminate the loop and the simulation.

I did something like this:

...
while (t <= t_total):
   t += dt
   (iteration, converged) = solver.solve()
   if converged == False:
      t -= dt 
      dt = dt/2
      continue
   ...

But it doesn’t work. How can I do this?

Thank you.

I assume the dt parameter show’s up in the NonlinearProblem?
Then you’d have to make that a Constant, and you can assign a new value to that constant.

See Using Constant or float for scalar constant

Hi Stein,
yes, dt is defined somewhere above the NonlinearProblem,
e.g.,

...
t = 0
t_total = 100
dt = Constant(0.1)
problem = NonlinearVariationalProblem(weakform, w, BC, J=a)
solver = NonlinearVariationalSolver(problem)
while(t <= t_total)
   t += dt
   (iteration, converged) = solver.solve()
   ...

and I just want to update dt when the solver couldn’t solve the problem.
But with my code, when the solver can’t solve the problem, the code just terminates and cannot update dt value.

Right. I address this in the rest of my message:

Did you try that?

Or do you mean to say that the solver throws an error upon non-convergence? Then you’d have to catch that error in a try-except block.

Try-except block makes it! Thank you very much

1 Like