### Re: Solution to the sine-gordon equation

```On Thu, Mar 21, 2019 at 1:14 PM Meier Quintin  wrote:
>
> Dear Daniel,
> Thanks a lot for the help. I got it to work by adding a transientterm().
>
> If I try to use your second trick though:
>
> eq = (DiffusionTerm(coeff=(1.0), var=phi) - numerix.sin(phi) + phi *
> numerix.cos(phi)-ImplicitSourceTerm(numerix.cos(phi)) ==
> TransientTerm(var=phi))

I think this has the parentheses in the wrong place. Does the code
that I included in my last email work for you as is?

This (without the line break)

eq = TransientTerm() == DiffusionTerm() - numerix.sin(phi) + phi *
numerix.cos(phi) - ImplicitSourceTerm(numerix.cos(phi))

> I get the error:
>
> ExplicitVariableError: Terms with explicit Variables cannot mix with Terms
> with implicit Variables.

That's because of the parentheses I think.

> Also, I dont fully understand the expression: I assume the implicit term is
> to neutralize the  phi * numerix.cos(phi), so should it not be:
> phi*ImplicitSourceTerm(numerix.cos(phi))?

ImplicitSourceTerm includes the extra variable multiplier
automatically. It put the source into the matrix diagonal to stabilize
the solution.

> Also: if I would want a second derivative in time in there to solve the
> time-dependent problem, would the best way to do that to define a second
> variable:
>
> dphidt=TransientTerm(var=phi)
>
> and solve the coupled equations or is there a different way?

Yes, right, it can be split into two hyperbolic equations, but FiPy
doesn't do well with those types of equations. It doesn't have the
correct numerical schemes. I think CLAWPACK might be a better bet for
that.

--
Daniel Wheeler
___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

```

### Re: Solution to the sine-gordon equation

```Dear Daniel,
Thanks a lot for the help. I got it to work by adding a transientterm().

If I try to use your second trick though:

eq = (DiffusionTerm(coeff=(1.0), var=phi) - numerix.sin(phi) + phi *
numerix.cos(phi)-ImplicitSourceTerm(numerix.cos(phi)) == TransientTerm(var=phi))

I get the error:

ExplicitVariableError: Terms with explicit Variables cannot mix with Terms with
implicit Variables.

Also, I dont fully understand the expression: I assume the implicit term is to
neutralize the  phi * numerix.cos(phi), so should it not be:
phi*ImplicitSourceTerm(numerix.cos(phi))?

Also: if I would want a second derivative in time in there to solve the
time-dependent problem, would the best way to do that to define a second
variable:

dphidt=TransientTerm(var=phi)

and solve the coupled equations or is there a different way?

Best,
Quintin

On 21 Mar 2019, at 16:48, Daniel Wheeler
mailto:daniel.wheel...@gmail.com>> wrote:

Hi Qunitin,

I think you need to relax the updates on this. Introducing a transient
term does that. I think that the b vector causes and instability. The
b vector is

phi_old - delta_t * sin(phi_old)

with a transient term in the equation. For small phi, this should
never be negative, since phi should only grow, but for delta_t > 1
this can be negative. It's the same reason that explicit diffusion has
a time step restriction.

You can get around this restriction by linearizing the source term so
that the source looks like

- numerix.sin(phi) + phi * numerix.cos(phi) -
ImplicitSourceTerm(numerix.cos(phi))

There is now no time step restriction since the b vector is just
"phi_old" when phi is small.

After 100 sweeps, the non-linearized source is at a residual of 0.0022
(with dt=1) and the linearized source is at 0.0009 (with dt=1000),
which is somewhat better.

Possibly using Newton iterations could improve the convergence rate further.

Hope that helps.

Cheers,

Daniel

import numpy as np
from fipy import *
nx = 1
dx = 0.01
mesh = Grid1D(nx=nx, dx=dx)
phi = CellVariable(mesh=mesh, name="phi", hasOld=True)
phi.constrain(2*np.pi, where=mesh.facesLeft)
phi.constrain(0., where=mesh.facesRight)

# requires dt < 1
#eq = TransientTerm() == DiffusionTerm() - numerix.sin(phi)

# no dt requirment
eq = TransientTerm() == DiffusionTerm() - numerix.sin(phi) + phi *
numerix.cos(phi) - ImplicitSourceTerm(numerix.cos(phi))
sweeps=1000

view = Viewer(phi)
for i in range(sweeps):
res = eq.sweep(var=phi, dt=1000)
print(res)
view.plot()
phi.updateOld()
raw_input('stopped')

On Thu, Mar 21, 2019 at 7:26 AM Meier Quintin
mailto:quintin.me...@mat.ethz.ch>> wrote:

Dear fipy community,
I’m new to FiPy, so forgive me if this is a trivial question.
I’m trying to find the kink solution to the (static) sine-gordon equation using
this simply piece of code.

import numpy as np
from fipy import *
nx = 1
dx = 0.01
mesh = Grid1D(nx=nx,dx=dx)
phi = CellVariable(mesh=mesh, name=“Phi")
phi.constrain(2*np.pi, where=mesh.facesLeft)
phi.constrain(0., where=mesh.facesRight)
eq = (DiffusionTerm(coeff=(1.0), var=phi) - numerix.sin(phi) == 0.0)
sweeps=10
for i in range(sweeps):
eq.sweep(var=phi)
MatplotlibViewer(phi)

Unfortunately, I do not manage to get a numerically stable solution, the first
sweep gives me a straight line and additional sweeps lead to numerical
instabilities without convergence.
I would be very thankful if someone could hint me to what I’m doing wrong.

Best wishes,
Quintin Meier

___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

--
Daniel Wheeler

___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

```

### Re: Solution to the sine-gordon equation

```On Thu, Mar 21, 2019 at 11:48 AM Daniel Wheeler
wrote:
>
> with a transient term in the equation. For small phi, this should
> never be negative, since phi should only grow, but for delta_t > 1

That's wrong, phi should shrink, but not go negative.

--
Daniel Wheeler
___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

```

### Re: Solution to the sine-gordon equation

```Hi Qunitin,

I think you need to relax the updates on this. Introducing a transient
term does that. I think that the b vector causes and instability. The
b vector is

phi_old - delta_t * sin(phi_old)

with a transient term in the equation. For small phi, this should
never be negative, since phi should only grow, but for delta_t > 1
this can be negative. It's the same reason that explicit diffusion has
a time step restriction.

You can get around this restriction by linearizing the source term so
that the source looks like

- numerix.sin(phi) + phi * numerix.cos(phi) -
ImplicitSourceTerm(numerix.cos(phi))

There is now no time step restriction since the b vector is just
"phi_old" when phi is small.

After 100 sweeps, the non-linearized source is at a residual of 0.0022
(with dt=1) and the linearized source is at 0.0009 (with dt=1000),
which is somewhat better.

Possibly using Newton iterations could improve the convergence rate further.

Hope that helps.

Cheers,

Daniel

import numpy as np
from fipy import *
nx = 1
dx = 0.01
mesh = Grid1D(nx=nx, dx=dx)
phi = CellVariable(mesh=mesh, name="phi", hasOld=True)
phi.constrain(2*np.pi, where=mesh.facesLeft)
phi.constrain(0., where=mesh.facesRight)

# requires dt < 1
#eq = TransientTerm() == DiffusionTerm() - numerix.sin(phi)

# no dt requirment
eq = TransientTerm() == DiffusionTerm() - numerix.sin(phi) + phi *
numerix.cos(phi) - ImplicitSourceTerm(numerix.cos(phi))
sweeps=1000

view = Viewer(phi)
for i in range(sweeps):
res = eq.sweep(var=phi, dt=1000)
print(res)
view.plot()
phi.updateOld()
raw_input('stopped')

On Thu, Mar 21, 2019 at 7:26 AM Meier Quintin  wrote:
>
> Dear fipy community,
> I’m new to FiPy, so forgive me if this is a trivial question.
> I’m trying to find the kink solution to the (static) sine-gordon equation
> using this simply piece of code.
>
> import numpy as np
> from fipy import *
> nx = 1
> dx = 0.01
> mesh = Grid1D(nx=nx,dx=dx)
> phi = CellVariable(mesh=mesh, name=“Phi")
> phi.constrain(2*np.pi, where=mesh.facesLeft)
> phi.constrain(0., where=mesh.facesRight)
> eq = (DiffusionTerm(coeff=(1.0), var=phi) - numerix.sin(phi) == 0.0)
> sweeps=10
> for i in range(sweeps):
> eq.sweep(var=phi)
> MatplotlibViewer(phi)
>
> Unfortunately, I do not manage to get a numerically stable solution, the
> first sweep gives me a straight line and additional sweeps lead to numerical
> instabilities without convergence.
> I would be very thankful if someone could hint me to what I’m doing wrong.
>
> Best wishes,
> Quintin Meier
>
> ___
> fipy mailing list
> fipy@nist.gov
> http://www.ctcms.nist.gov/fipy
>   [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

--
Daniel Wheeler

___
fipy mailing list
fipy@nist.gov
http://www.ctcms.nist.gov/fipy
[ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

```