I am trying to create a program that simulates a magnetic field distribution around the end of a coil in java. To do this I have decided to use the Boit-Savart law around a circular loop though I'm struggling to get it to work as I still haven't learnt this stuff yet (I am in high school). The goal is to capture the field distribution in this orientation from the coil: field distribution from coil, rectangle is what needs to be shown
To do this I have decided to create a Point class that stores a point's (x,y) location and strength vectors bx, by at that point. Then the program creates an ArrayList for every point P(x,y) within the screen's dimensions and calculates the x and y vectors at every point. I currently have this:
public class Point {
public double x;
public double y;
public double b;
public double bx;
public double by;
public Point(double x, double y) {
this.x = x;
this.y = y;
this.b = 0;
this.bx = 0;
this.by = 0;
}
}
import java.util.ArrayList;
public class Simulation {
ArrayList<Point> points;
static double min = Double.MAX_VALUE;
static double max = Double.MIN_VALUE;
public Simulation() {
this.points = new ArrayList<>();
}
public void pointArray() {
for (int x = 0; x <= 800; x++) { // 800 width
for (int y = 0; y <= 800; y++) { // 800 height
Point p = new Point(x, y);
calculateVectors(p);
// update min/max
if (p.b > max) {
max = p.b;
} else if (p.b < min) {
min = p.b;
}
points.add(p);
}
}
}
public ArrayList<Point> getPoints() {
pointArray();
return points;
}
public static void calculateVectors(Point p) {
double loopRadius = 100; // Radius of the loop in physical z-direction
double current = 50; // Current through the loop
double mu0 = 4 * Math.PI * 1E-7; // Permeability of free space
double dTheta = 2 * Math.PI / 100; // Discretization step in radians
double Bz = 0.0; // Bx from calculation, but physically Bz
double By = 0.0; // Stays as By in both calculation and physical space
for (int i = 0; i < 100; i++) {
double theta = i * dTheta;
// Loop coordinates in the calculation xy-plane (interpreted as xz in physical space)
double segmentX = loopRadius * Math.cos(theta); // Physically z-coordinate
double segmentY = loopRadius * Math.sin(theta); // Stays as y-coordinate
// Displacement vector from this segment to the observation point
double rx = (p.x - segmentX) * 0.01; // p.z is used for physical z, mapped from calculated x
double ry = (p.y - segmentY) * 0.01; // y-coordinate remains unchanged
double r = Math.sqrt(rx * rx + ry * ry);
if (r > 1E-6) { // Avoid division by zero
// dl vector tangent to the loop, in the calculation plane
double dlx = -Math.sin(theta) * loopRadius * dTheta; // Physically dlz
double dly = Math.cos(theta) * loopRadius * dTheta; // Stays as dly
// Magnetic field contribution from this segment
double dBz = (mu0 * current / (4 * Math.PI)) * (dly * rx - dlx * ry) / (r * r * r); // Calculated as dBx, interpreted as dBz
Bz += dBz * (rx / r); // Physically Bz contribution
By += dBz * (ry / r); // By contribution
}
}
// Assign the calculated field components to the Point object
p.bx = Bz; // Calculated Bx interpreted as Bz
p.by = By;
// Calculate the magnitude of the B field vector in the zy-plane
p.b = Math.sqrt(Bz * Bz + By * By);
}
}
The issue with what chatgpt gave me is that the coil is oriented towards the screen when it should be pointing sideways (like in the drawing above). How can I fix this?