# examples.multigrid package¶

## examples.multigrid.mg_test_general_alphabeta_only module¶

Test the general MG solver with a variable coeffcient Helmholtz problem. This ensures we didn’t screw up the base functionality here.

Here we solve:

```alpha phi + div . ( beta grad phi ) = f
```

with:

```alpha = 1.0
beta = 2.0 + cos(2.0*pi*x)*cos(2.0*pi*y)

f = (-16.0*pi**2*cos(2*pi*x)*cos(2*pi*y) - 16.0*pi**2 + 1.0)*sin(2*pi*x)*sin(2*pi*y)
```

This has the exact solution:

```phi = sin(2.0*pi*x)*sin(2.0*pi*y)
```

on [0,1] x [0,1]

We use Dirichlet BCs on phi. For beta, we do not have to impose the same BCs, since that may represent a different physical quantity. Here we take beta to have Neumann BCs. (Dirichlet BCs for beta will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_general_alphabeta_only.alpha(x, y)[source]
examples.multigrid.mg_test_general_alphabeta_only.beta(x, y)[source]
examples.multigrid.mg_test_general_alphabeta_only.f(x, y)[source]
examples.multigrid.mg_test_general_alphabeta_only.gamma_x(x, y)[source]
examples.multigrid.mg_test_general_alphabeta_only.gamma_y(x, y)[source]
examples.multigrid.mg_test_general_alphabeta_only.test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the general MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_general_alphabeta_only.true(x, y)[source]

## examples.multigrid.mg_test_general_beta_only module¶

Test the general MG solver with a variable coeffcient Poisson problem (in essence, we are making this solver act like the variable_coefficient_MG.py solver). This ensures we didn’t screw up the base functionality here.

Here we solve:

```div . ( beta grad phi ) = f
```

with:

```beta = 2.0 + cos(2.0*pi*x)*cos(2.0*pi*y)

f = -16.0*pi**2*(cos(2*pi*x)*cos(2*pi*y) + 1)*sin(2*pi*x)*sin(2*pi*y)
```

This has the exact solution:

```phi = sin(2.0*pi*x)*sin(2.0*pi*y)
```

on [0,1] x [0,1]

We use Dirichlet BCs on phi. For beta, we do not have to impose the same BCs, since that may represent a different physical quantity. Here we take beta to have Neumann BCs. (Dirichlet BCs for beta will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_general_beta_only.alpha(x, y)[source]
examples.multigrid.mg_test_general_beta_only.beta(x, y)[source]
examples.multigrid.mg_test_general_beta_only.f(x, y)[source]
examples.multigrid.mg_test_general_beta_only.gamma_x(x, y)[source]
examples.multigrid.mg_test_general_beta_only.gamma_y(x, y)[source]
examples.multigrid.mg_test_general_beta_only.test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the general MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_general_beta_only.true(x, y)[source]

## examples.multigrid.mg_test_general_constant module¶

Test the general MG solver with a CONSTANT coefficient problem – the same one from the multigrid class test. This ensures we didn’t screw up the base functionality here.

We solve:

```u_xx + u_yy = -2[(1-6x**2)y**2(1-y**2) + (1-6y**2)x**2(1-x**2)]
u = 0 on the boundary
```

this is the example from page 64 of the book A Multigrid Tutorial, 2nd Ed.

The analytic solution is u(x,y) = (x**2 - x**4)(y**4 - y**2)

examples.multigrid.mg_test_general_constant.alpha(x, y)[source]
examples.multigrid.mg_test_general_constant.beta(x, y)[source]
examples.multigrid.mg_test_general_constant.f(x, y)[source]
examples.multigrid.mg_test_general_constant.gamma_x(x, y)[source]
examples.multigrid.mg_test_general_constant.gamma_y(x, y)[source]
examples.multigrid.mg_test_general_constant.test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the general MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_general_constant.true(x, y)[source]

## examples.multigrid.mg_test_general_dirichlet module¶

Test the general MG solver with Dirichlet boundary conditions.

Here we solve:

```alpha phi + div{beta grad phi} + gamma . grad phi = f
```

with:

```alpha = 1.0
beta = cos(2*pi*x)*cos(2*pi*y) + 2.0
gamma_x = sin(2*pi*x)
gamma_y = sin(2*pi*y)

f = (-16.0*pi**2*cos(2*pi*x)*cos(2*pi*y) + 2.0*pi*cos(2*pi*x) +
2.0*pi*cos(2*pi*y) - 16.0*pi**2 + 1.0)*sin(2*pi*x)*sin(2*pi*y)
```

This has the exact solution:

```phi = sin(2.0*pi*x)*sin(2.0*pi*y)
```

on [0,1] x [0,1]

We use Dirichlet BCs on phi.

For the coefficients we do not have to impose the same BCs, since that may represent a different physical quantity. beta is the one that really matters since it must be brought to the edges. Here we take beta to have Neumann BCs. (Dirichlet BCs for beta will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_general_dirichlet.alpha(x, y)[source]
examples.multigrid.mg_test_general_dirichlet.beta(x, y)[source]
examples.multigrid.mg_test_general_dirichlet.f(x, y)[source]
examples.multigrid.mg_test_general_dirichlet.gamma_x(x, y)[source]
examples.multigrid.mg_test_general_dirichlet.gamma_y(x, y)[source]
examples.multigrid.mg_test_general_dirichlet.test_general_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the general MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_general_dirichlet.true(x, y)[source]

## examples.multigrid.mg_test_general_inhomogeneous module¶

Test the general MG solver with inhomogeneous Dirichlet

boundary conditions.

Here we solve:

```alpha phi + div{beta grad phi} + gamma . grad phi = f
```

with:

```alpha = 10.0
beta = x*y + 1  (note: x*y alone doesn't work)
gamma_x = 1
gamma_y = 1

f =  -(pi/2)*(x + 1)*sin(pi*y/2)*cos(pi*x/2)
- (pi/2)*(y + 1)*sin(pi*x/2)*cos(pi*y/2) +
(-pi**2*(x*y+1)/2 + 10)*cos(pi*x/2)*cos(pi*y/2)
```

This has the exact solution:

```phi = cos(pi*x/2)*cos(pi*y/2)
```

on [0,1] x [0,1], with Dirichlet boundary conditions:

```phi(x=0) = cos(pi*y/2)
phi(x=1) = 0
phi(y=0) = cos(pi*x/2)
phi(y=1) = 0
```

For the coefficients we do not have to impose the same BCs, since that may represent a different physical quantity. beta is the one that really matters since it must be brought to the edges. Here we take beta to have Neumann BCs. (Dirichlet BCs for beta will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_general_inhomogeneous.alpha(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.beta(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.f(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.gamma_x(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.gamma_y(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.test_general_poisson_inhomogeneous(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the general MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_general_inhomogeneous.true(x, y)[source]
examples.multigrid.mg_test_general_inhomogeneous.xl_func(y)[source]
examples.multigrid.mg_test_general_inhomogeneous.yl_func(x)[source]

## examples.multigrid.mg_test_simple module¶

an example of using the multigrid class to solve Laplace’s equation. Here, we solve:

```u_xx + u_yy = -2[(1-6x**2)y**2(1-y**2) + (1-6y**2)x**2(1-x**2)]
u = 0 on the boundary
```

this is the example from page 64 of the book A Multigrid Tutorial, 2nd Ed.

The analytic solution is u(x,y) = (x**2 - x**4)(y**4 - y**2)

examples.multigrid.mg_test_simple.f(x, y)[source]
examples.multigrid.mg_test_simple.test_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]
examples.multigrid.mg_test_simple.true(x, y)[source]

## examples.multigrid.mg_test_vc_constant module¶

Test the variable coefficient MG solver with a CONSTANT coefficient problem – the same one from the multigrid class test. This ensures we didn’t screw up the base functionality here.

We solve:

```u_xx + u_yy = -2[(1-6x**2)y**2(1-y**2) + (1-6y**2)x**2(1-x**2)]
u = 0 on the boundary
```

this is the example from page 64 of the book A Multigrid Tutorial, 2nd Ed.

The analytic solution is u(x,y) = (x**2 - x**4)(y**4 - y**2)

examples.multigrid.mg_test_vc_constant.alpha(x, y)[source]
examples.multigrid.mg_test_vc_constant.f(x, y)[source]
examples.multigrid.mg_test_vc_constant.test_vc_constant(N)[source]
examples.multigrid.mg_test_vc_constant.true(x, y)[source]

## examples.multigrid.mg_test_vc_dirichlet module¶

Test the variable-coefficient MG solver with Dirichlet boundary conditions.

Here we solve:

```div . ( alpha grad phi ) = f
```

with:

```alpha = 2.0 + cos(2.0*pi*x)*cos(2.0*pi*y)

f = -16.0*pi**2*(cos(2*pi*x)*cos(2*pi*y) + 1)*sin(2*pi*x)*sin(2*pi*y)
```

This has the exact solution:

```phi = sin(2.0*pi*x)*sin(2.0*pi*y)
```

on [0,1] x [0,1]

We use Dirichlet BCs on phi. For alpha, we do not have to impose the same BCs, since that may represent a different physical quantity. Here we take alpha to have Neumann BCs. (Dirichlet BCs for alpha will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_vc_dirichlet.alpha(x, y)[source]
examples.multigrid.mg_test_vc_dirichlet.f(x, y)[source]
examples.multigrid.mg_test_vc_dirichlet.test_vc_poisson_dirichlet(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the variable-coefficient MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_vc_dirichlet.true(x, y)[source]

## examples.multigrid.mg_test_vc_periodic module¶

Test the variable-coefficient MG solver with periodic data.

Here we solve:

```div . ( alpha grad phi ) = f
```

with:

```alpha = 2.0 + cos(2.0*pi*x)*cos(2.0*pi*y)

f = -16.0*pi**2*(cos(2*pi*x)*cos(2*pi*y) + 1)*sin(2*pi*x)*sin(2*pi*y)
```

This has the exact solution:

```phi = sin(2.0*pi*x)*sin(2.0*pi*y)
```

on [0,1] x [0,1]

We use Dirichlet BCs on phi. For alpha, we do not have to impose the same BCs, since that may represent a different physical quantity. Here we take alpha to have Neumann BCs. (Dirichlet BCs for alpha will force it to 0 on the boundary, which is not correct here)

examples.multigrid.mg_test_vc_periodic.alpha(x, y)[source]
examples.multigrid.mg_test_vc_periodic.f(x, y)[source]
examples.multigrid.mg_test_vc_periodic.test_vc_poisson_periodic(N, store_bench=False, comp_bench=False, make_plot=False, verbose=1, rtol=1e-12)[source]

test the variable-coefficient MG solver. The return value here is the error compared to the exact solution, UNLESS comp_bench=True, in which case the return value is the error compared to the stored benchmark

examples.multigrid.mg_test_vc_periodic.true(x, y)[source]

## examples.multigrid.mg_vis module¶

an example of using the multigrid class to solve Laplace’s equation. Here, we solve:

```u_xx + u_yy = -2[(1-6x**2)y**2(1-y**2) + (1-6y**2)x**2(1-x**2)]
u = 0 on the boundary
```

this is the example from page 64 of the book A Multigrid Tutorial, 2nd Ed.

The analytic solution is u(x,y) = (x**2 - x**4)(y**4 - y**2)

examples.multigrid.mg_vis.doit(nx, ny)[source]
examples.multigrid.mg_vis.f(x, y)[source]
examples.multigrid.mg_vis.true(x, y)[source]

## examples.multigrid.project_periodic module¶

test of a cell-centered, centered-difference approximate projection.

initialize the velocity field to be divergence free and then add to it the gradient of a scalar (whose normal component vanishes on the boundaries). The projection should recover the original divergence- free velocity field.

The test velocity field comes from Almgen, Bell, and Szymczak 1996.

This makes use of the multigrid solver with periodic boundary conditions.

One of the things that this test demonstrates is that the initial projection may not be able to completely remove the divergence free part, so subsequent projections may be necessary. In this example, we add a very strong gradient component.

The total number of projections to perform is given by nproj. Each projection uses the divergence of the velocity field from the previous iteration as its source term.

Note: the output file created stores the original field, the poluted field, and the recovered field.

examples.multigrid.project_periodic.doit(nx, ny)[source]

manage the entire projection

## examples.multigrid.prolong_restrict_demo module¶

examples.multigrid.prolong_restrict_demo.doit()[source]