Giter VIP home page Giter VIP logo

maxwell-simulation's Introduction

Electromagnetic wave simulation

Live version at cemsim.com

An interactive web-based simulator of electromagnetic waves. Simulation done using the finite-difference time-domain method.

Found any bugs, have suggestions or anything else? Open an issue or contact me.

Technology used among others: React, TypeScript, regl.

Getting started

To launch this web-app yourself, first install Node if you haven't. Next clone or download this repository, type npm install and then npm start. The project was created using Create React App so all its commands are also available.

Project structure overview

How the simulation works / an intro to simulating Maxwell's equations

The electromagnetic field is described by Maxwell's equations. In their most common form they state that there exists an electric and a magnetic field. The equations describe how those two fields evolve over time and affect each other. Their macroscopic version also takes into account materials which are specified by their relative permittivity and relative permeability.

Maxwell's equations exist in the continuous form as integral or differential equations and we will use their differential form. For simulation we have to discretize. We start with the equations in differential form and replace differentiation by finite differences, for example d/dt X(t) becomes (X_t+1 - X_t) / dt. We can then solve such an equation for X_t+1 to obtain an update equation which we can use to update our fields. We also have to discretize space itself which we do by specifying a discrete grid. At each grid cell location we store the values of the two fields and the material parameters. This type of simulation is called a finite-difference time-domain (FDTD) method. As we only display two dimensions in this simulation we can further simplify these equations by taking all spatial derivatives in the Z direction to be zero. This will get rid of some terms when calculating the curl.

With this information we can derive all the equations we need for simulation. We can use a Yee grid to automatically satisfy Gauss's law and Gauss's law for magnetism (see here (pdf warning)). Here we make the magnetic field grid exist at a 0.5 * cell size offset from the electric field grid. Now we can discretize the Fadaray's and Ampère's law to obtain the update equations for the magnetic and electric fields respectively.

Below we derive the update equations for Faraday's law which will give us the magnetic field given the electric field and the previous magnetic field.

Notation

X: position 3-vector
x, y, z: individual position coordinates
\hat{X/Y/Z}: unit 3-vector in X/Y/Z direction
t: time
E: electric field
B: magnetic field
C_x/y/z: Spatial grid cell size
𝜏: Time step size

1. Faraday's law

2. Write out curl and individual vector components

3. Set Z-derivatives (d/dz) to zero

4. Discretize curl and time-derivative

5. Solve for B_t+1(X)

We can do exactly the same procedure for Ampère's law which yields the update equations for the electric field given the magnetic field and the previous electric field.

A great lecture series on YouTube about this and more was created by CEM Lectures and can be found here.

Finally in this simulation we also take into account conductivity (ie. loss). The modification to the equations for this can be found here (pdf warning) at the beginning of chapter 5 (page 53 equations 5.1a and 5.1b).

The code for all of this is implemented in WebGL shaders for quick parallel computation and can be found here.

Acknowledgements

maxwell-simulation's People

Contributors

robinka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

maxwell-simulation's Issues

Add more examples

Some from the reddit thread:

  • Resonator
  • Bragg reflector
  • Superconductors (using µ=0, eps=inf? might not work at all)
  • Grating mirror

Add more graphics settings

  • Bloom intensity
  • Different visualizers for the field (eg. toggle magnetic energy, electric energy, show vector fields, ...)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.