Analyze magnetic field

From pjt222/agent-almanac · View interactive version

Analyze magnetic field is an AI agent skill from the GitHub repository pjt222/agent-almanac. Calculate and visualize magnetic fields produced by current distributions using the Biot-Savart law, Ampere's law, and magnetic dipole approximations. Use when computing B-fields from arbitrary current geometries, exploiting symmetry with Ampere's law, analyzing superposition of multiple sources, or

How to use this skill

Skill file

Analyze Magnetic Field

Calculate the magnetic field produced by a given current distribution by characterizing the source geometry, selecting the appropriate law (Biot-Savart for arbitrary geometries, Ampere's law for high-symmetry configurations), evaluating field integrals, checking limiting cases, incorporating magnetic material effects where relevant, and visualizing the resulting field-line topology.

When to Use

Inputs

Procedure

Step 1: Characterize Current Distribution and Geometry

Fully specify the source before selecting a method:

  1. Current path: Describe the geometry of every current-carrying element. For line currents, specify the path as a parametric curve r'(t). For surface currents, specify the surface current density K (A/m). For volume currents, specify J (A/m^2).
  2. Coordinate system: Choose coordinates aligned with the dominant symmetry. Cylindrical (rho, phi, z) for wires and solenoids. Spherical (r, theta, phi) for dipoles and loops at large distances. Cartesian for planar sheets.
  3. Symmetry analysis: Identify translational, rotational, and reflection symmetries. A symmetry of the source is a symmetry of the field. Document which components of B are nonzero by symmetry and which vanish.
  4. Current continuity: Verify that the current distribution satisfies div(J) = 0 (steady state) or div(J) = -d(rho)/dt (time-varying). Inconsistent current distributions produce unphysical fields.
## Source Characterization
- **Current type**: [line I / surface K / volume J]
- **Geometry**: [parametric description]
- **Coordinate system**: [and justification]
- **Symmetries**: [translational / rotational / reflection]
- **Nonzero B-components by symmetry**: [list]
- **Current continuity**: [verified / issue noted]

Got: A complete geometric description of the current distribution with coordinate system chosen, symmetries cataloged, and current continuity verified.

If fail: If the geometry is too complex for a closed-form parametric description, discretize into short straight segments (numerical Biot-Savart). If current continuity is violated, add displacement current or return charge accumulation terms before proceeding.

Step 2: Select Appropriate Law

Choose the method that matches the problem's symmetry and complexity:

  1. Ampere's law (high symmetry): Use when the current distribution has sufficient symmetry that B can be pulled out of the line integral. Applicable cases:
  1. Biot-Savart law (general): Use for arbitrary geometries where Ampere's law cannot simplify:
  1. Magnetic dipole approximation (far field): Use when the observation point is far from the source (r >> source dimension d):
  1. Superposition: For multiple sources, compute B from each independently and sum vectorially. Linearity of Maxwell's equations guarantees this is exact.
## Method Selection
- **Primary method**: [Ampere / Biot-Savart / dipole]
- **Justification**: [symmetry argument or distance criterion]
- **Expected complexity**: [closed-form / single integral / numerical]
- **Fallback method**: [if primary fails or for cross-validation]

Got: A justified choice of method with a clear statement of why the chosen law is appropriate for the problem's symmetry level.

If fail: If Ampere's law is chosen but the symmetry is insufficient (B cannot be extracted from the integral), fall back to Biot-Savart. If the source geometry is too complex for analytic Biot-Savart, discretize numerically.

Step 3: Set Up and Evaluate Field Integrals

Execute the calculation using the method selected in Step 2:

  1. Ampere's law path: For each Amperian loop:
  1. Biot-Savart integration: For each field point r:
  1. Dipole calculation:
  1. Superposition assembly: Sum contributions from all sources at each observation point. Track components separately to preserve cancellation accuracy.
## Field Calculation
- **Integral setup**: [explicit expression]
- **Evaluation method**: [analytic / numeric with N segments]
- **Result**: B(r) = [expression with units]
- **Convergence check** (if numerical): [N vs. 2N comparison]

Got: An explicit expression for B(r) at the observation points, with correct units (Tesla or Gauss) and a convergence check for numerical results.

If fail: If the integral diverges, check for a missing regularization (e.g., the field on the wire itself diverges for an infinitely thin wire -- use finite wire radius). If numerical results oscillate with N, the integrand has a near-singularity that requires adaptive quadrature or analytical subtraction of the singular part.

Step 4: Check Limiting Cases

Verify the result against known physics before trusting it:

  1. Far-field dipole limit: At large r, any localized current distribution should produce a field that matches the magnetic dipole formula. Compute B from your result in the limit r -> infinity and compare with (mu_0 / 4 pi) * [3(m . r_hat) r_hat - m] / r^3.
  1. Near-field infinite-wire limit: Close to a long straight segment of the conductor (distance rho << length L), the field should approach B = mu_0 I / (2 pi rho). Check this for the relevant portion of your geometry.
  1. On-axis special cases: For loops and solenoids, the on-axis field has simple closed forms:
  1. Symmetry consistency: Verify that components predicted to vanish by symmetry (Step 1) are indeed zero in the computed result. A nonzero forbidden component indicates an error.
  1. Dimensional analysis: Verify that B has units of Tesla. Every term should carry mu_0 * [current] / [length] or equivalent.
## Limiting Case Verification
| Case | Condition | Expected | Computed | Match |
|------|-----------|----------|----------|-------|
| Far-field dipole | r >> d | mu_0 m / (4 pi r^3) scaling | [result] | [Yes/No] |
| Near-field wire | rho << L | mu_0 I / (2 pi rho) | [result] | [Yes/No] |
| On-axis formula | [geometry] | [known result] | [result] | [Yes/No] |
| Symmetry zeros | [component] | 0 | [result] | [Yes/No] |
| Units | -- | Tesla | [check] | [Yes/No] |

Got: All limiting cases match. The field has the correct units, symmetry, and asymptotic behavior.

If fail: A failed limit indicates an error in the integral setup or evaluation. The most common causes are: wrong sign in the cross product, missing factor of 2 or pi, incorrect limits of integration, or a coordinate system mismatch between source and field point parameterizations.

Step 5: Incorporate Magnetic Materials and Visualize

Extend the analysis to include material effects and produce field visualizations:

  1. Linear magnetic materials: Replace mu_0 with mu = mu_r * mu_0 inside the material. Apply boundary conditions at material interfaces:
  1. Nonlinear materials (B-H curves): For ferromagnetic cores:
  1. Demagnetization effects: For finite-geometry magnetic materials (e.g., short rods, spheres), the internal field is reduced by the demagnetization factor N_d: H_internal = H_applied - N_d * M.
  1. Field visualization:
  1. Physical intuition check: Confirm that the field pattern makes qualitative sense. The field should be strongest near the current source, should circulate around currents (right-hand rule), and should decay with distance.
## Material Effects and Visualization
- **Material model**: [vacuum / linear mu_r / nonlinear B-H / hysteretic]
- **Boundary conditions applied**: [list interfaces]
- **Visualization**: [field lines / magnitude contour / both]
- **Div B = 0 check**: [field lines close / verified numerically]

Got: A complete field solution including material effects where relevant, with a visualization that shows closed field lines consistent with div B = 0 and qualitative behavior matching physical intuition.

If fail: If field lines do not close, the calculation has a divergence error -- recheck the integral or numerical method. If the material introduces unexpected field amplification, verify that mu_r is applied only inside the material volume and that boundary conditions are correctly enforced at every interface.

Validation

Pitfalls

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.