Skip to content

Instantly share code, notes, and snippets.

@mathdoodle
Last active August 31, 2016 01:49
Show Gist options
  • Save mathdoodle/bcc952a683eebe9fe015de02f6001ea1 to your computer and use it in GitHub Desktop.
Save mathdoodle/bcc952a683eebe9fe015de02f6001ea1 to your computer and use it in GitHub Desktop.
Line Charge Redux

Line Charge Redux

How To

This will use integration to calculate the electric field at a point due to a line of charge. Show the integration element at each point.

export const gray = EIGHT.Color.gray
export const green = EIGHT.Color.green
export const red = EIGHT.Color.red
import World from './World';
export default class Grid {
private inner: EIGHT.Grid;
constructor(private world: World) {
this.inner = new EIGHT.Grid({
uSegments: 8,
vSegments: 8
})
world.add(this.inner)
}
get color() {
return this.inner.color;
}
set color(color: EIGHT.Color) {
this.inner.color = color;
}
}
<!doctype html>
<html>
<head>
<style>
/* STYLE-MARKER */
</style>
<script src="https://jspm.io/system.js"></script>
<!-- SCRIPTS-MARKER -->
</head>
<body>
<canvas id='canvas'></canvas>
<script>
// CODE-MARKER
</script>
<script>
System.import('./index.js')
</script>
</body>
</html>
import Grid from './Grid';
import {gray, green, red} from './colors'
import {i,j,k} from './units'
import{meter, second, kilogram, newton, coulomb} from './units';
//console.log(`A Newton is ${newton}`) //This is using backticks not single quote
import{ε0} from './physics';
import {π} from './math';
/**
* Position of point in space where we compute the field strength.
*/
const X = 2.5 * meter * i
/**
* Length of the wire
*/
const L = 10 * meter
/**
* Number of subdivisions of the wire.
*/
const N = 20
/**
* element length
*/
const ΔL = L / N
/**
* Position of element, as an integer index m = [0, 1, ... , N-1]
*/
let m = 0
/**
* radius of wire and element
*/
const radius = 0.05 * L
/**
* Scales distances and positions for graphical display
*/
const lengthscale = 1.2 * L
/**
* Scales electric fields for graphical display
*/
const fieldscale = 20 * newton / coulomb
/**
* Electric field
*/
let E = 0 * i * newton / coulomb
/**
* Electric field due to charge element
* TODO: This should be a local variable.
*/
// let ΔE = 0*i * newton/coulomb
/**
* Displacement vector from element to point in space
* TODO: This should be local variable inside loop.
*/
// let r = 0*i * meter
/**
* Total Charge on wire, in coulombs.
*/
const Q = 1E-8 * coulomb
const world = new EIGHT.WorldG3('canvas')
world.camera.eye.copy(k-j+i).scale(1)
world.camera.up.copy(k)
world.scaleFactor = lengthscale
const wire = new EIGHT.CylinderG3(world)
wire.scaleFactor = lengthscale
wire.color = gray
//wire.opacity = 0.4
wire.length = L
wire.radius = radius
wire.axis = k
wire.transparent = true
const element = new EIGHT.CylinderG3(world)
element.scaleFactor = lengthscale
element.color = red
element.length = ΔL
element.radius = wire.radius + .0001 * meter // improves graphics
element.axis = k
const grid = new Grid(world)
grid.color = gray
/**
* Graphical representation total field vector arrow.
*/
const Earrow = new EIGHT.ArrowG3(world);
Earrow.scaleFactor = fieldscale
Earrow.h = i * newton / coulomb
Earrow.X = X
let framecounter = 0;
/**
* Animates the scene.
*/
function animate(timestamp: number) {
world.clear()
framecounter++
if ((framecounter % 10 === 0) && m < N) {
element.X = wire.axis * (-L / 2 + ΔL / 2 + m * ΔL)
/**
* Displacement vector from element to point in space
*/
const r = X - element.X
/**
* Amount of charge on the element.
*/
const ΔQ = Q * ΔL / L
/**
* Electric field due to charge element.
*/
const ΔE = ΔQ * r.direction() / (4 * π * ε0 * (r | r))
/**
* electric field arrow due to element
*/
const dEarrow = new EIGHT.ArrowG3(world)
dEarrow.scaleFactor = fieldscale
dEarrow.color= green
dEarrow.X = X + (E / fieldscale) * world.scaleFactor
dEarrow.h = ΔE
Earrow.h = E
E = E + ΔE //summing the electric field
m++ //moving element up the wire
}
world.draw()
requestAnimationFrame(animate)
}
requestAnimationFrame(animate)
//
// Basis elements
//
export const zero = EIGHT.Geometric3.zero()
export const one = EIGHT.Geometric3.one()
export const e1 = EIGHT.Geometric3.e1()
export const e2 = EIGHT.Geometric3.e2()
export const e3 = EIGHT.Geometric3.e3()
/**
* The pseudoscalar for Euclidean 3D Geometric Space.
*/
const I = e1 ^ e2 ^ e3
//
// Universal functions
//
export const exp = EIGHT.exp
export const log = EIGHT.log
export const cos = EIGHT.cos
export const sin = EIGHT.sin
//
// Constants
//
export const π = Math.PI
/**
* A complete turn, 2 * π.
*/
export const τ = 2 * Math.PI
{
"description": "Line Charge Redux",
"dependencies": {
"davinci-eight": "2.245.0",
"davinci-units": "1.1.0"
},
"operatorOverloading": true,
"name": "copy-of-copy-of-copy-of-copy-of-eight-alpha-blending",
"version": "0.1.0",
"keywords": [
"Electric field",
"line charge",
"integration"
],
"author": "David Geo Holmes"
}
import {unitless, meter as m, second,kilogram, coulomb as C, newton as N} from './units'
export const ε0 = 8.854E-12 * C * C/ (m * m * N)
// console.log(`${ε0.toPrecision(4)}`)
body {
margin: 0;
overflow: hidden;
}
canvas { width: 500px; height: 500px }
#stats { position: absolute; top: 0; left: 0; }
export const unitless = UNITS.G3.one
export const i = UNITS.G3.e1
export const j = UNITS.G3.e2
export const k = UNITS.G3.e3
export const meter = UNITS.G3.meter
export const kilogram = UNITS.G3.kilogram
export const second = UNITS.G3.second
export const newton = kilogram * meter / (second * second)
export const coulomb = UNITS.G3.coulomb
UNITS.G3.BASIS_LABELS = UNITS.G3.BASIS_LABELS_HAMILTON
// I'd like to be able to do this... and have it be useful as a type.
export const Mass = kilogram.uom.dimensions;
export const Velocity = (meter / second).uom.dimensions;
export const Momentum = (kilogram * meter / second).uom.dimensions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment