Debian Codegraph @

demo/documented/cahn-hilliard/cpp/main.cpp

raw
1 // Copyright (C) 2005-2016 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Anders Logg 2011
19 //
20 // This program illustrates the use of the DOLFIN nonlinear solver for solving
21 // the Cahn-Hilliard equation.
22 //
23 // The Cahn-Hilliard equation is very sensitive to the chosen parameters and
24 // time step. It also requires fines meshes, and is often not well-suited to
25 // iterative linear solvers.
26
27 // Begin demo
28
29 #include <dolfin.h>
30 #include "CahnHilliard2D.h"
31 #include "CahnHilliard3D.h"
32
33 using namespace dolfin;
34
35 // Initial conditions
36 class InitialConditions : public Expression
37 {
38 public:
39
40 InitialConditions(MPI_Comm comm) : Expression(2)
41 { dolfin::seed(2 + dolfin::MPI::rank(comm)); }
42
43 void eval(Array<double>& values, const Array<double>& x) const
44 {
45 values[0]= 0.63 + 0.02*(0.5 - dolfin::rand());
46 values[1]= 0.0;
47 }
48
49 };
50
51 // User defined nonlinear problem
52 class CahnHilliardEquation : public NonlinearProblem
53 {
54 public:
55
56 // Constructor
57 CahnHilliardEquation(std::shared_ptr<const Form> F,
58 std::shared_ptr<const Form> J) : _F(F), _J(J) {}
59
60 // User defined residual vector
61 void F(GenericVector& b, const GenericVector& x)
62 { assemble(b, *_F); }
63
64 // User defined assemble of Jacobian
65 void J(GenericMatrix& A, const GenericVector& x)
66 { assemble(A, *_J); }
67
68 private:
69
70 // Forms
71 std::shared_ptr<const Form> _F;
72 std::shared_ptr<const Form> _J;
73 };
74
75
76 int main(int argc, char* argv[])
77 {
78 dolfin::init(argc, argv);
79
80 // Mesh
81 auto mesh = std::make_shared<Mesh>(
82 UnitSquareMesh::create({{96, 96}}, CellType::Type::quadrilateral));
83
84
85 // Create function space and forms, depending on spatial dimension
86 // of the mesh
87 std::shared_ptr<FunctionSpace> V;
88 std::shared_ptr<Form> F, J;
89 if (mesh->geometry().dim() == 2)
90 {
91 V = std::make_shared<CahnHilliard2D::FunctionSpace>(mesh);
92 F = std::make_shared<CahnHilliard2D::ResidualForm>(V);
93 J = std::make_shared<CahnHilliard2D::JacobianForm>(V, V);
94 }
95 else if(mesh->geometry().dim() == 3)
96 {
97 V = std::make_shared<CahnHilliard3D::FunctionSpace>(mesh);
98 F = std::make_shared<CahnHilliard3D::ResidualForm>(V);
99 J = std::make_shared<CahnHilliard3D::JacobianForm>(V, V);
100 }
101 else
102 error("This demo only supports two or three spatial dimensions.");
103
104 // Create solution Functions (at t_n and t_{n+1})
105 auto u0 = std::make_shared<Function>(V);
106 auto u = std::make_shared<Function>(V);
107
108 // Set solution to intitial condition
109 InitialConditions u_initial(mesh->mpi_comm());
110 *u0 = u_initial;
111 *u = u_initial;
112
113 // Time stepping and model parameters
114 auto dt = std::make_shared<Constant>(5.0e-6);
115 auto theta = std::make_shared<Constant>(0.5);
116 auto lambda = std::make_shared<Constant>(1.0e-2);
117
118 // Collect coefficient into groups
119 std::map<std::string, std::shared_ptr<const GenericFunction>> coefficients
120 = {{"u", u}, {"lmbda", lambda}, {"dt", dt}, {"theta", theta}};
121
122 // Add extra coefficient for residual
123 std::map<std::string, std::shared_ptr<const GenericFunction>> coefficients_F = coefficients;
124 coefficients_F.insert({"u0", u0});
125
126 // Attach coefficients to form
127 J->set_coefficients(coefficients);
128 F->set_coefficients(coefficients_F);
129
130 double t = 0.0;
131 double T = 50*(*dt);
132
133 // Create user-defined nonlinear problem
134 CahnHilliardEquation cahn_hilliard(F, J);
135
136 // Create nonlinear solver and set parameters
137 NewtonSolver newton_solver;
138 newton_solver.parameters["linear_solver"] = "lu";
139 newton_solver.parameters["convergence_criterion"] = "incremental";
140 newton_solver.parameters["maximum_iterations"] = 10;
141 newton_solver.parameters["relative_tolerance"] = 1e-6;
142 newton_solver.parameters["absolute_tolerance"] = 1e-15;
143
144 // Save initial condition to file
145 File file("cahn_hilliard.pvd", "compressed");
146 file << (*u)[0];
147
148 // Solve
149 while (t < T)
150 {
151 // Update for next time step
152 t += *dt;
153 *u0->vector() = *u->vector();
154
155 // Solve
156 newton_solver.solve(cahn_hilliard, *u->vector());
157
158 // Save function to file
159 file << std::pair<const Function*, double>(&((*u)[0]), t);
160 }
161
162 return 0;
163 }