from fenics import *
import matplotlib.pyplot as plt
from IPython.display import HTML, display
import time
import random
def progress(value, max=100):
return HTML("""
Iterations: {value} of {max}
<progress
value='{value}'
max='{max}',
style='width: 100%'
>
{value}
</progress>
""".format(value=value, max=max))
class InitialConditions(UserExpression):
def __init__(self, **kwargs):
random.seed(4 + MPI.rank(MPI.comm_world))
super().__init__(**kwargs)
def eval(self, values, x):
values[0] = 0.00
values[1] = 0.00
values[2] = 0.02*(0.5 - random.random())
values[3] = 0.00
def value_shape(self):
return (4,)
class CahnHilliardEqaution(NonlinearProblem):
def __init__(self, a, Res):
NonlinearProblem.__init__(self)
self.Res = Res
self.a = a
def F(self, b, x):
assemble(self.Res, tensor=b)
def J(self, A, x):
assemble(self.a, tensor=A)
tau = 2.5e-1 #T / n_steps #delta
A1=0.4
mesh = UnitSquareMesh(128,128)
FE = FiniteElement("P", mesh.ufl_cell(), 1) # scalar element
V = FunctionSpace(mesh,MixedElement([FE,FE,FE,FE])) # mixed space
q = Function(V)
dq =TrialFunction(V)
old_q = Function(V)
mu_phi,mu_qe,phi,qe = split(q)
dmu_phi,dmu_qe,dphi,dqe = split(dq)
old_mu_phi,old_mu_qe,old_phi,old_qe = split(old_q)
E = 0.5*inner(grad(phi),grad(phi))*dx + (phi*ln(phi))*dx
Res = derivative(E, q, dq) - inner(mu_phi,dphi)*dx - inner(mu_qe,dqe)*dx
a = derivative(Res, q, dq)
problem = CahnHilliardEqaution(a, Res)
solver = NewtonSolver()
q_init = InitialConditions()
q.interpolate(q_init)
old_q.interpolate(q_init)
file = File("output.pvd", "compressed")
t = 0.0
T = 50*tau
while (t < T):
t += tau
old_q.vector()[:] = q.vector()
z=solver.solve(problem,q.vector())
Please follow the guidelines in Read before posting: How do I get my question answered? when posting a question.
Thank you!.
I tried my best to minimize the code, but!!!
Also, I am working on colab.
I cannot reproduce your problem.
This is very unclear to me. What are you trying to compute here?
You are differentiating in the direction of dq
twice, which to me looks very odd.
What is the energy functional you are trying to minimize?