Newer
Older
Bogdan Enache
committed
"""Minimal example of a grudge driver."""
__copyright__ = """
Copyright (C) 2015 Andreas Kloeckner
Copyright (C) 2021 University of Illinois Board of Trustees
"""
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy as np
Bogdan Enache
committed
import pyopencl as cl
from arraycontext.impl.pyopencl import PyOpenCLArrayContext
from arraycontext.container.traversal import thaw
from grudge.shortcuts import set_up_rk4
from grudge import DiscretizationCollection
from pytools.obj_array import flat_obj_array
import grudge.op as op
def main(write_output=False, order=4):
Bogdan Enache
committed
cl_ctx = cl.create_some_context()
queue = cl.CommandQueue(cl_ctx)
actx = PyOpenCLArrayContext(
queue,
allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))
)
Bogdan Enache
committed
from meshmode.mesh.generation import generate_regular_rect_mesh
mesh = generate_regular_rect_mesh(
a=(-0.5,)*dims,
b=(0.5,)*dims,
nelements_per_axis=(20,)*dims)
dcoll = DiscretizationCollection(actx, mesh, order=order)
def source_f(actx, dcoll, t=0):
source_center = np.array([0.1, 0.22, 0.33])[:dcoll.dim]
source_width = 0.05
source_omega = 3
source_center_dist = flat_obj_array(
[nodes[i] - source_center[i] for i in range(dcoll.dim)]
)
return (
np.sin(source_omega*t)
* actx.np.exp(
-np.dot(source_center_dist, source_center_dist)
/ source_width**2
)
)
ones = dcoll.zeros(actx) + 1
c = actx.np.where(np.dot(x, x) < 0.15,
np.float32(0.1)*ones,
np.float32(0.2)*ones)
Bogdan Enache
committed
from grudge.models.wave import VariableCoefficientWeakWaveOperator
from meshmode.mesh import BTAG_ALL, BTAG_NONE
wave_op = VariableCoefficientWeakWaveOperator(
dcoll,
c,
source_f=source_f,
dirichlet_tag=BTAG_NONE,
neumann_tag=BTAG_NONE,
radiation_tag=BTAG_ALL,
flux_type="upwind"
)
Bogdan Enache
committed
fields = flat_obj_array(
dcoll.zeros(actx),
[dcoll.zeros(actx) for i in range(dcoll.dim)]
)
Bogdan Enache
committed
Bogdan Enache
committed
def rhs(t, w):
Bogdan Enache
committed
if mesh.dim == 2:
Bogdan Enache
committed
elif mesh.dim == 3:
Bogdan Enache
committed
dt_stepper = set_up_rk4("w", dt, fields, rhs)
Bogdan Enache
committed
nsteps = int(final_t/dt)
print("dt=%g nsteps=%d" % (dt, nsteps))
from grudge.shortcuts import make_visualizer
Bogdan Enache
committed
step = 0
def norm(u):
return op.norm(dcoll, u, 2)
Bogdan Enache
committed
from time import time
t_last_step = time()
if write_output:
u = fields[0]
v = fields[1:]
vis.write_vtk_file(
"fld-var-propogation-speed-%04d.vtu" % step,
[
("u", u),
("v", v),
("c", c),
]
)
Bogdan Enache
committed
for event in dt_stepper.run(t_end=final_t):
if isinstance(event, dt_stepper.StateComputed):
assert event.component_id == "w"
step += 1
if step % 10 == 0:
print(f"step: {step} t: {time()-t_last_step} "
f"L2: {norm(u=event.state_component[0])}")
if write_output:
vis.write_vtk_file(
"fld-var-propogation-speed-%04d.vtu" % step,
Bogdan Enache
committed
[
("u", event.state_component[0]),
("v", event.state_component[1:]),
Bogdan Enache
committed
t_last_step = time()
assert norm(u=event.state_component[0]) < 1
Bogdan Enache
committed
if __name__ == "__main__":
main()