This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

adaptive time stepping

0 votes

Hello,
I want to solve a nonlinear PDE in fenics. After a few iterations I want to change the size of the time step: Is that possible?
Here is what I tried, but unfortunately, it doesn't work.

# Time Step
dt = 2.5e-06       
dT = Constant(dt)

# Function F for Newton Method
F = F_1 + dT*F_2   

# Derivative of F for Newton Method
J = derivative(F, u, du)

# Create Nonlinear Problem and Newton Solver
Problem = NonlinearVariationalProblem(F, u, bcs, J)
Solver  = NonlinearVariationalSolver(Problem)

t = 0.0
u.interpolate(InitialConditions())  
i = 0
# Loop
while (i < 500):
    if i == 10:
     dT = 10**4*dT
    # Iterate
    i += 1
    t += dT
    # Store old Solution
    u0.vector()[:] = u.vector()
    # Solve Nonlinear Problem
    Solver.solve()

Can anyone help me?

Thanks a lot!

asked Aug 20, 2013 by lisa FEniCS Novice (280 points)

Please, add some more information. What goes wrong? What is the system you want to solve? And, make your code snippet a minimal example, that can be used to reproduce the error.

2 Answers

+6 votes
 
Best answer

This is a programming error resulting from insufficient understanding how python variables works. Read this for an introduction. Then you should be able understand why this

# Loop
while (i < 500):
    if i == 10:
        #dT = 1e4*dT # this creates new name (with no relation to your form F)
        dT.assign(1e4*dt) # we rather mutate the existing object
        #dT *= 1e4 # we can use this either (*= operator also mutates object (if mutable))
    # Iterate
    ...

should work (not tested).

answered Aug 20, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Aug 20, 2013 by Jan Blechta

Thank you for response and help! Thank you Jan Blechta, that solved my question. It seems to work fine!

0 votes

It is possible to change the time step.

If you want help with your specific code, please post a simpler but complete program, and identify precisely what is not working as you would expect it. Someone trying to help should be able to paste your code into a file and run it.

answered Aug 20, 2013 by Garth N. Wells FEniCS Expert (35,930 points)
...