Debian Codegraph @

python/demo/undocumented/curl-curl/demo_curl-curl.py

raw
1 """Eddy currents phenomena in low conducting body can be described
2 using electric vector potential and curl-curl operator:
3
4 \nabla \times \nabla \times T = - \frac{\partial B}{\partial t}
5
6 Electric vector potential defined as:
7
8 \nabla \times T = J
9
10 Boundary condition:
11
12 J_n = 0,
13
14 T_t = T_w = 0, \frac{\partial T_n}{\partial n} = 0
15
16 which is naturally fulfilled for zero Dirichlet BC with Nedelec (edge)
17 elements.
18
19 This demo uses the auxiliary Maxwell space multigrid preconditioner
20 from HYPRE (via PETSc). To run this demo, PETSc must be configured
21 with HYPRE, and petsc4py must be available.
22
23 """
24
25 # Copyright (C) 2009-2015 Bartosz Sawicki and Garth N. Wells
26 #
27 # This file is part of DOLFIN.
28 #
29 # DOLFIN is free software: you can redistribute it and/or modify
30 # it under the terms of the GNU Lesser General Public License as published by
31 # the Free Software Foundation, either version 3 of the License, or
32 # (at your option) any later version.
33 #
34 # DOLFIN is distributed in the hope that it will be useful,
35 # but WITHOUT ANY WARRANTY; without even the implied warranty of
36 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 # GNU Lesser General Public License for more details.
38 #
39 # You should have received a copy of the GNU Lesser General Public License
40 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
41 #
42 # Modified by Anders Logg 2011
43
44 from dolfin import *
45 import matplotlib.pyplot as plt
46
47
48 # Check that DOLFIN has been configured with PETSc
49 if not has_petsc():
50 print("This demo requires DOLFIN to be configured with PETSc.")
51 exit()
52
53 # Check that PETSc has been configured with HYPRE
54 if not "hypre_amg" in PETScPreconditioner.preconditioners():
55 print("This demo requires PETSc to be configured with HYPRE.")
56 exit()
57
58 if not has_petsc4py():
59 print("DOLFIN has not been compiled with petsc4py support.")
60 exit()
61
62 # Import petsc4py and check that HYPRE bindings are available
63 from petsc4py import PETSc
64 try:
65 getattr(PETSc.PC, 'getHYPREType')
66 except AttributeError:
67 print("This demo requires a recent petsc4py with HYPRE bindings.")
68 exit()
69
70 # Set PETSc as default linear algebra backend
71 parameters["linear_algebra_backend"] = "PETSc";
72
73 # Load sphere mesh
74 mesh = Mesh("../sphere.xml.gz")
75 mesh = refine(mesh)
76
77 # Define function spaces
78 V = FunctionSpace(mesh, "Nedelec 1st kind H(curl)", 1)
79
80 # Define test and trial functions
81 v = TestFunction(V)
82 u = TrialFunction(V)
83
84 # Define functions for boundary condiitons
85 dbdt = Constant((0.0, 0.0, 1.0))
86 zero = Constant((0.0, 0.0, 0.0))
87
88 # Magnetic field (to be computed)
89 T = Function(V)
90
91 # Dirichlet boundary
92 class DirichletBoundary(SubDomain):
93 def inside(self, x, on_boundary):
94 return on_boundary
95
96 # Boundary condition
97 bc = DirichletBC(V, zero, DirichletBoundary())
98
99 # Forms for the eddy-current equation
100 a = inner(curl(v), curl(u))*dx
101 L = -inner(v, dbdt)*dx
102
103 # Assemble system
104 A, b = assemble_system(a, L, bc)
105
106 # Create PETSc Krylov solver (from petsc4py)
107 ksp = PETSc.KSP()
108 ksp.create(PETSc.COMM_WORLD)
109
110 # Set the Krylov solver type and set tolerances
111 ksp.setType("cg")
112 ksp.setTolerances(rtol=1.0e-8, atol=1.0e-12, divtol=1.0e10, max_it=300)
113
114 # Get the preconditioner and set type (HYPRE AMS)
115 pc = ksp.getPC()
116 pc.setType("hypre")
117 pc.setHYPREType("ams")
118
119 # Build discrete gradient
120 P1 = FunctionSpace(mesh, "Lagrange", 1)
121 G = DiscreteOperators.build_gradient(V, P1)
122
123 # Attach discrete gradient to preconditioner
124 pc.setHYPREDiscreteGradient(as_backend_type(G).mat())
125
126 # Build constants basis for the Nedelec space
127 constants = [Function(V) for i in range(3)]
128 for i, c in enumerate(constants):
129 direction = [1.0 if i == j else 0.0 for j in range(3)]
130 c.interpolate(Constant(direction))
131
132 # Inform preconditioner of constants in the Nedelec space
133 cvecs = [as_backend_type(constant.vector()).vec() for constant in constants]
134 pc.setHYPRESetEdgeConstantVectors(cvecs[0], cvecs[1], cvecs[2])
135
136 # We are dealing with a zero conductivity problem (no mass term), so
137 # we need to tell the preconditioner
138 pc.setHYPRESetBetaPoissonMatrix(None)
139
140 # Set operator for the linear solver
141 ksp.setOperators(as_backend_type(A).mat())
142
143 # Set options prefix
144 ksp.setOptionsPrefix("eddy_")
145
146 # Turn on monitoring of residual
147 opts = PETSc.Options()
148 opts.setValue("-eddy_ksp_monitor_true_residual", None)
149
150 # Solve eddy currents equation (using potential T)
151 ksp.setFromOptions()
152 ksp.solve(as_backend_type(b).vec(), as_backend_type(T.vector()).vec())
153
154 # Show linear solver details
155 ksp.view()
156
157 # Test and trial functions for density equation
158 W = VectorFunctionSpace(mesh, "Lagrange", 1)
159 v = TestFunction(W)
160 u = TrialFunction(W)
161
162 # Solve density equation (use conjugate gradient linear solver)
163 J = Function(W)
164 solve(inner(v, u)*dx == dot(v, curl(T))*dx, J,
165 solver_parameters={"linear_solver": "cg"} )
166
167 # Write solution to PVD/ParaView file
168 file = File("current_density.pvd")
169 file << J
170
171 # Plot solution and hold plot
172 plot(J)
173 plt.show()