// Copyright (C) 2005-2016 Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see .
//
// Modified by Anders Logg 2011
//
// This program illustrates the use of the DOLFIN nonlinear solver for solving
// the Cahn-Hilliard equation.
//
// The Cahn-Hilliard equation is very sensitive to the chosen parameters and
// time step. It also requires fines meshes, and is often not well-suited to
// iterative linear solvers.
// Begin demo
#include
#include "CahnHilliard2D.h"
#include "CahnHilliard3D.h"
using namespace dolfin;
// Initial conditions
class InitialConditions : public Expression
{
public:
InitialConditions(MPI_Comm comm) : Expression(2)
{ dolfin::seed(2 + dolfin::MPI::rank(comm)); }
void eval(Array& values, const Array& x) const
{
values[0]= 0.63 + 0.02*(0.5 - dolfin::rand());
values[1]= 0.0;
}
};
// User defined nonlinear problem
class CahnHilliardEquation : public NonlinearProblem
{
public:
// Constructor
CahnHilliardEquation(std::shared_ptr F,
std::shared_ptr J) : _F(F), _J(J) {}
// User defined residual vector
void F(GenericVector& b, const GenericVector& x)
{ assemble(b, *_F); }
// User defined assemble of Jacobian
void J(GenericMatrix& A, const GenericVector& x)
{ assemble(A, *_J); }
private:
// Forms
std::shared_ptr _F;
std::shared_ptr _J;
};
int main(int argc, char* argv[])
{
dolfin::init(argc, argv);
// Mesh
auto mesh = std::make_shared(
UnitSquareMesh::create({{96, 96}}, CellType::Type::quadrilateral));
// Create function space and forms, depending on spatial dimension
// of the mesh
std::shared_ptr V;
std::shared_ptr