Hi Onno,
As Stephan correctly (as usual) points out, your primary speed issue is that you are using solve instead of LinearVariationalProblem and LinearVariationalSolver. This means you unnecessarily duplicate a whole load of setup every timestep.
 In this case, the simplest thing to do is to delete your special case for theta = 0 and just have the theta = 0 case run through the nonlinear code path. On my machine this gets you two time units in about two seconds. If you really want to, you can set the
 solver options in the theta = 0 case to switch off the outer nonlinear solve. However my observation is that this makes negligible additional difference because PETSc only does one solver iteration anyway (it does an extra residual evaluation, but this seems
 not to be a significant cost). One would not expect Firedrake to be significantly faster than Matlab in this case because the problem is so tiny.
The reason that you are observing failure in the theta > 0 case is this term:
sqrt(max_value(2.0*h/3.0,0.0))
When this gets differentiated, you get a 1/sqrt(h) term, which has a pole  at h=0 (which is not a huge surprise since the expression is actually not differentiable at h=0). This inserts NaNs into the matrix and the solver dies. Firedrake
 is doing the right thing, it’s just that your problem is not well defined there. You have two options:
Regards,
David
PS. In many circumstances, solver options are also a significant speed factor. However in this case (a) the tiny problem largely makes this irrelevant and (b) because you have a 1D problem, the default ilu(0) preconditioner acts like a
 direct solver, which is almost certainly the best option for tiny problems like this.
From:
<firedrake-bounces@imperial.ac.uk> on behalf of Onno Bokhove <O.Bokhove@leeds.ac.uk>
Reply-To: firedrake <firedrake@imperial.ac.uk>
Date: Sunday, 13 May 2018 at 13:15
To: firedrake <firedrake@imperial.ac.uk>
Subject: [firedrake] nonlinear diffusion with space-time Robin condition
(i) Will & I tried to build a theta-time-step solver including Crank-Nicolson for theta=1/2 but only get it to work for theta=0 (Forward Euler);
 see the attached code at "HERE2" which matches (32) in the attached pdf.
[This is really a test set-up atm for a larger question on dynamic boundary conditions for an elliptic problem, but the system may be amusing
 by itself as a tentative FD-example.)
(ii) The working explicit Forward Euler code for theta=0 is very slow, at least 10x slower than the same algorithm in a FEM matlab code (-).
 See "HERE1" in the attached code; why is the code so slow?
[I have also attached the basic matlab code.]
This example may be amusing because of the space-time Robin condition (14) applied to (11), leading to the Firedrake weak form (32) --when
 time is discretised as well. The question can also be solved, slightly differently wrt the implementation of the Robin boundary condition, using finite differences, which makes for an amusing comparison.
I am not even sure whether this space-time bc and nonlinear diffusion eqn is well-posed but the finite difference and FEM numerics seems to
 be consistent.
Thank you and best wishes,
Onno