|
| 1 | +#= |
| 2 | +
|
| 3 | +# 312 : Periodic Poisson 3D |
| 4 | +([source code](@__SOURCE_URL__)) |
| 5 | +
|
| 6 | +This is a simple demonstration and validation of the new restriction based periodic boundary operator. |
| 7 | +
|
| 8 | +An unstructured cube grid is coupled along two axes periodically and restricted to non-zero Dirichlet values at the other to faces. |
| 9 | +
|
| 10 | +The result is a linear function and the error is measured directly. |
| 11 | +
|
| 12 | +Note that the result is independent of the periodic coupling! Therefore the correctness of the |
| 13 | +new QR based column compression is covered by this example. |
| 14 | +
|
| 15 | + |
| 16 | +=# |
| 17 | + |
| 18 | +module Example313_PeriodicPoisson |
| 19 | + |
| 20 | +using ExtendableFEM |
| 21 | +using ExtendableGrids |
| 22 | +using SimplexGridFactory |
| 23 | +using GridVisualize |
| 24 | +using TetGen |
| 25 | +using UnicodePlots |
| 26 | +using StaticArrays |
| 27 | +using LinearAlgebra |
| 28 | +using Test #hide |
| 29 | + |
| 30 | +const reg_left = 1 |
| 31 | +const reg_right = 2 |
| 32 | +const reg_front = 3 |
| 33 | +const reg_back = 4 |
| 34 | +const reg_bottom = 5 |
| 35 | +const reg_top = 6 |
| 36 | + |
| 37 | + |
| 38 | +function create_grid(; h) |
| 39 | + builder = SimplexGridBuilder(; Generator = TetGen) |
| 40 | + |
| 41 | + ## bottom points |
| 42 | + b00 = point!(builder, 0, 0, 0) |
| 43 | + b01 = point!(builder, 0, 1, 0) |
| 44 | + b10 = point!(builder, 1, 0, 0) |
| 45 | + b11 = point!(builder, 1, 1, 0) |
| 46 | + |
| 47 | + ## top points |
| 48 | + t00 = point!(builder, 0, 0, 1) |
| 49 | + t01 = point!(builder, 0, 1, 1) |
| 50 | + t10 = point!(builder, 1, 0, 1) |
| 51 | + t11 = point!(builder, 1, 1, 1) |
| 52 | + |
| 53 | + ## left face |
| 54 | + facetregion!(builder, reg_left) |
| 55 | + facet!(builder, b00, b01, t01, t00) |
| 56 | + |
| 57 | + ## right face |
| 58 | + facetregion!(builder, reg_right) |
| 59 | + facet!(builder, b10, b11, t11, t10) |
| 60 | + |
| 61 | + ## front face |
| 62 | + facetregion!(builder, reg_front) |
| 63 | + facet!(builder, b00, b10, t10, t00) |
| 64 | + |
| 65 | + ## back face |
| 66 | + facetregion!(builder, reg_back) |
| 67 | + facet!(builder, b01, b11, t11, t01) |
| 68 | + |
| 69 | + ## top face |
| 70 | + facetregion!(builder, reg_top) |
| 71 | + facet!(builder, t00, t10, t11, t01) |
| 72 | + |
| 73 | + ## bottom face |
| 74 | + facetregion!(builder, reg_bottom) |
| 75 | + facet!(builder, b00, b10, b11, b01) |
| 76 | + |
| 77 | + |
| 78 | + cellregion!(builder, 1) |
| 79 | + maxvolume!(builder, h) |
| 80 | + regionpoint!(builder, 0.5, 0.5, 0.5) |
| 81 | + |
| 82 | + return simplexgrid(builder) |
| 83 | +end |
| 84 | + |
| 85 | +function main(; |
| 86 | + Plotter = nothing, |
| 87 | + h = 1.0e-3, |
| 88 | + periodic = true, |
| 89 | + kwargs... |
| 90 | + ) |
| 91 | + |
| 92 | + xgrid = create_grid(; h) |
| 93 | + |
| 94 | + FES = FESpace{H1P1{1}}(xgrid) |
| 95 | + |
| 96 | + ## problem description |
| 97 | + PD = ProblemDescription("Periodic Poisson Problem") |
| 98 | + u = Unknown("u"; name = "temperature") |
| 99 | + assign_unknown!(PD, u) |
| 100 | + |
| 101 | + assign_operator!(PD, BilinearOperator([grad(u)]; kwargs...)) |
| 102 | + |
| 103 | + assign_restriction!(PD, BoundaryDataRestriction(u; value = -1, regions = [reg_bottom])) |
| 104 | + assign_restriction!(PD, BoundaryDataRestriction(u; value = +1, regions = [reg_top])) |
| 105 | + |
| 106 | + if periodic |
| 107 | + assign_restriction!(PD, CoupledDofsRestriction(u, reg_left, reg_right)) |
| 108 | + assign_restriction!(PD, CoupledDofsRestriction(u, reg_front, reg_back)) |
| 109 | + end |
| 110 | + |
| 111 | + ## solve |
| 112 | + sol, SC = solve(PD, FES; return_config = true, kwargs...) |
| 113 | + residual(SC) < 1.0e-10 || error("Residual is not zero!") |
| 114 | + |
| 115 | + function exact_error!(out, u, qpinfo) |
| 116 | + # exact solution here is u(x,y,z) = 2z - 1 |
| 117 | + val = qpinfo.x[3] * 2 - 1 |
| 118 | + out[1] = (val - u[1])^2 |
| 119 | + return nothing |
| 120 | + end |
| 121 | + |
| 122 | + plt = plot([grid(u), id(u)], sol; Plotter, width = 1200, height = 800, scene3d = :LScene, slice = :y => 0.5) |
| 123 | + |
| 124 | + ErrorIntegrator = ItemIntegrator(exact_error!, [id(u)]; quadorder = 2) |
| 125 | + L2error = sqrt(sum(evaluate(ErrorIntegrator, sol))) |
| 126 | + |
| 127 | + @show L2error |
| 128 | + |
| 129 | + return L2error, plt |
| 130 | + |
| 131 | +end |
| 132 | + |
| 133 | +generateplots = ExtendableFEM.default_generateplots(Example313_PeriodicPoisson, "example313.png") #hide |
| 134 | +function runtests() #hide |
| 135 | + error1, _ = main(periodic = true) #hide |
| 136 | + @test error1 < 1.0e-12 #hide |
| 137 | + |
| 138 | + error2, _ = main(periodic = false) #hide |
| 139 | + @test error2 < 1.0e-12 #hide |
| 140 | + |
| 141 | + return nothing #hide |
| 142 | +end #hide |
| 143 | + |
| 144 | +end # module |
0 commit comments