Skip to content
Snippets Groups Projects
cavities.py 3.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bogdan Enache's avatar
    Bogdan Enache committed
    """Minimal example of a grudge driver."""
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    __copyright__ = "Copyright (C) 2015 Andreas Kloeckner"
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    __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:
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    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.
    """
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    import numpy as np
    import pyopencl as cl
    
    
    from meshmode.array_context import PyOpenCLArrayContext
    
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    from grudge.shortcuts import set_up_rk4
    
    Matt Wala's avatar
    Matt Wala committed
    from grudge import sym, bind, DGDiscretizationWithBoundaries
    
    from grudge.models.em import get_rectangular_cavity_mode
    
    Matt Wala's avatar
    Matt Wala committed
    STEPS = 60
    
    
    
    Bogdan Enache's avatar
    Bogdan Enache committed
    def main(dims, write_output=True, order=4):
        cl_ctx = cl.create_some_context()
        queue = cl.CommandQueue(cl_ctx)
    
        actx = PyOpenCLArrayContext(queue)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        from meshmode.mesh.generation import generate_regular_rect_mesh
        mesh = generate_regular_rect_mesh(
                a=(0.0,)*dims,
                b=(1.0,)*dims,
                n=(5,)*dims)
    
        discr = DGDiscretizationWithBoundaries(actx, mesh, order=order)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        if 0:
            epsilon0 = 8.8541878176e-12  # C**2 / (N m**2)
            mu0 = 4*np.pi*1e-7  # N/A**2.
            epsilon = 1*epsilon0
            mu = 1*mu0
        else:
            epsilon = 1
            mu = 1
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        from grudge.models.em import MaxwellOperator
        op = MaxwellOperator(epsilon, mu, flux_type=0.5, dimensions=dims)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        if dims == 3:
    
            sym_mode = get_rectangular_cavity_mode(1, (1, 2, 2))
    
            fields = bind(discr, sym_mode)(actx, t=0, epsilon=epsilon, mu=mu)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        else:
    
            sym_mode = get_rectangular_cavity_mode(1, (2, 3))
    
            fields = bind(discr, sym_mode)(actx, t=0)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        # FIXME
        #dt = op.estimate_rk4_timestep(discr, fields=fields)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        op.check_bc_coverage(mesh)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        # print(sym.pretty(op.sym_operator()))
        bound_op = bind(discr, op.sym_operator())
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        def rhs(t, w):
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        if mesh.dim == 2:
            dt = 0.004
        elif mesh.dim == 3:
            dt = 0.002
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        dt_stepper = set_up_rk4("w", dt, fields, rhs)
    
    Matt Wala's avatar
    Matt Wala committed
        final_t = dt * STEPS
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        nsteps = int(final_t/dt)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        print("dt=%g nsteps=%d" % (dt, nsteps))
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        from grudge.shortcuts import make_visualizer
        vis = make_visualizer(discr, vis_order=order)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        step = 0
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        norm = bind(discr, sym.norm(2, sym.var("u")))
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        from time import time
        t_last_step = time()
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        e, h = op.split_eh(fields)
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        if 1:
    
    Matt Wala's avatar
    Matt Wala committed
            vis.write_vtk_file("fld-cavities-%04d.vtu" % step,
    
    Bogdan Enache's avatar
    Bogdan Enache committed
                    [
                        ("e", e),
                        ("h", h),
                        ])
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        for event in dt_stepper.run(t_end=final_t):
            if isinstance(event, dt_stepper.StateComputed):
                assert event.component_id == "w"
    
    Bogdan Enache's avatar
    Bogdan Enache committed
                step += 1
    
                print(step, event.t, norm(u=e[0]), norm(u=e[1]),
                        norm(u=h[0]), norm(u=h[1]),
    
    Bogdan Enache's avatar
    Bogdan Enache committed
                        time()-t_last_step)
                if step % 10 == 0:
                    e, h = op.split_eh(event.state_component)
    
    Matt Wala's avatar
    Matt Wala committed
                    vis.write_vtk_file("fld-cavities-%04d.vtu" % step,
    
    Bogdan Enache's avatar
    Bogdan Enache committed
                            [
                                ("e", e),
                                ("h", h),
                                ])
                t_last_step = time()
    
    
    
    if __name__ == "__main__":
    
    Bogdan Enache's avatar
    Bogdan Enache committed
        main(3)