Skip to content

Add voltage variation analysis for multi-lens TEM systems#11

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/investigate-voltage-changes
Draft

Add voltage variation analysis for multi-lens TEM systems#11
Copilot wants to merge 5 commits intomainfrom
copilot/investigate-voltage-changes

Conversation

Copy link

Copilot AI commented Feb 10, 2026

Problem

User asked whether varying electron beam voltage (200 keV ↔ 210 keV) enables fitting 4 and 5 lens systems.

Analysis Result

No. Both systems are over-determined without voltage variation:

  • 4-lens: 8 DOF (4 focal lengths + 4 distances) vs ~4 constraints → +4 DOF
  • 5-lens: 10 DOF vs ~4 constraints → +6 DOF

Voltage adds only 1 DOF (global focal scaling). Negligible benefit when system already has 4-6 free parameters.

Changes

Core functionality

  • src/temgym_core/utils.py: Added electron_wavelength(voltage_kev)
    • Relativistic formula: λ = h / √(2·m_e·e·V·(1 + eV/(2·m_e·c²)))
    • 200 keV → 2.3249 pm, 210 keV → 2.2531 pm (3.09% change)

Analysis tools

  • examples/n_lens_forward.ipynb: Interactive notebook demonstrating:
    • Multi-lens system builder (2-5 lenses)
    • ABCD matrix extraction via JAX autodiff
    • DOF analysis showing system is over-determined
    • Optimization framework setup

Tests & docs

  • tests/test_electron_wavelength.py: 5 test cases validating wavelength calculations
  • examples/LENS_VOLTAGE_ANALYSIS.md: Complete analysis with usage examples

Usage

from temgym_core.utils import electron_wavelength
from temgym_core.components import Lens, Plane
from temgym_core.run import run_to_end
from jax import jacobian

# Calculate wavelength at different voltages
λ_200 = electron_wavelength(200.0)  # 2.3249e-12 m
λ_210 = electron_wavelength(210.0)  # 2.2531e-12 m

# Build 4-lens system
lenses = [Lens(z=z, focal_length=f) for z, f in zip(positions, focal_lengths)]
components = lenses + [Plane(z=detector_z)]

# Compute transfer matrix
ray0 = Ray.origin()
abcd = custom_jacobian_matrix(jacobian(lambda r: run_to_end(r, components))(ray0))

When voltage variation helps

  • Fixed lens currents (saturation constraints)
  • Aberration correction (non-paraxial, chromatic)
  • 2-lens systems (4 DOF → 5 DOF is meaningful)
Original prompt

I would like you to investigate for me changing the voltage, and repeating the experiment. Let's say I can change from 200keV to 210keV and vice versa, and do the experiment agian, would this make it possible to fit a 4 and 5 lens system?

[Chronological Review: The conversation began with the user asking for the As and Bs for a 5 lens system and how to print them. The user then requested modifications to the notebook for a larger magnification, adjustments to currents, and discussions about fitting problems without B values. The user continued to ask for simplifications and refinements in the notebook, reporting bugs and issues encountered during execution. The conversation culminated in inquiries about constraints and system performance with varying lens counts.]

[Intent Mapping:

  1. "In this notebook here can you tell me what the As and Bs for the 5 lens system are? How can I print them?" - User seeks specific values and printing methods.
  2. "I would like a run for the 5 lens version of this notebook that worked with a larger magnification - so let's say total of 10,000." - User requests a specific configuration change.
  3. "Please reduce the currents." - User asks for adjustments to currents.
  4. "If I don't have B values, is it still possible to solve this fitting problem?" - User questions the feasibility of fitting without certain parameters.
  5. "I would like a version that removes B terms, and we only fit A and psi." - User requests a specific model adjustment.
  6. "Can you help me run one of the code examples?" - User seeks assistance with executing code.
  7. "Ok, can you simplify this entire notebook - it's gotten really unruly!" - User requests overall simplification of the notebook.
  8. "There is a bug..." - User reports an error encountered during execution.
  9. "Is the distance constraint being applied here?" - User inquires about specific constraints in the model.
  10. "It seems without B the loss goes to zero, but there parameters don't match - is the system underconstrained?" - User questions the model's constraints.
  11. "For more than two lenses does it improve?" - User asks about the performance with additional lenses.]

[Technical Inventory:

  • Technologies: Python, NumPy for numerical computations.
  • Frameworks: Jupyter Notebook for interactive coding.
  • Concepts: Lens systems, fitting parameters (A, B), constraints in optimization problems.]

[Code Archaeology:

  • The notebook in question is "n_lens_forward.ipynb".
  • Functions and variables related to lens fitting, including those for calculating As and Bs, and handling optimization constraints were discussed.]

[Progress Assessment:

  • Completed: User has explored various configurations and reported bugs.
  • Pending: User requests for simplifications, adjustments to fitting parameters, and further exploration of system constraints remain.]

[Context Validation: All critical information for continuation is captured, including user requests for adjustments, simplifications, and inquiries about fitting problems.]

[Recent Commands Analysis:

  • Last Agent Commands: User requested to "Try Again" multiple times, indicating a desire to resolve ongoing issues.
  • Tool Results Summary: No specific tool results were returned as the conversation focused on user inquiries and requests for modifications.
  • Pre-Summary State: The agent was actively addressing user requests related to fitting parameters and notebook simplifications when the token budget was exceeded.
  • Operation Context: The user was seeking to resolve issues with the fitting model and improve the usability of the notebook.]
1. Conversation Overview: - Primary Objectives: User requested assistance with fitting parameters for a lens system, simplifications to a notebook, and resolution of bugs encountered during execution. - Session Context: The conversation progressed through various requests for adjustments, simplifications, and inquiries about model constraints and performance. - User Intent Evolution: The user shifted from seeking specific values to requesting overall improvements and troubleshooting issues in the notebook.
  1. Technical Foundation:
  • Python: Used for coding and executing the notebook.
  • NumPy: Utilized for numerical computations related to lens fitting.
  • Jupyter Notebook: The environment for interactive coding and testing.
  1. Codebase Status:
  • File Name: n_lens_forward.ipynb
  • Purpose: To model and fit parameters for a lens system.
  • Current State: User has reported bugs and requested simplifications.
  • Key Code Segments: Functions related to fitting parameters A and B, and handling optimization constraints.
  • Dependencies: Interactions with other components of the lens modeling system.
  1. Problem Resolution:
  • Issues Encountered: Bugs related to fitting parameters and constraints.
  • Solutions Implemented: User has attempted various adjustments and simplifications.
  • Debugging Context: Ongoing troubleshooting of fitting issues w...

Created from VS Code.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@DavidLanders95
Copy link
Member

@copilot I think you need to base your investigation on this notebook - please try again.

Copilot AI and others added 4 commits February 10, 2026 18:53
Co-authored-by: DavidLanders95 <6655619+DavidLanders95@users.noreply.github.com>
Co-authored-by: DavidLanders95 <6655619+DavidLanders95@users.noreply.github.com>
Co-authored-by: DavidLanders95 <6655619+DavidLanders95@users.noreply.github.com>
Co-authored-by: DavidLanders95 <6655619+DavidLanders95@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate voltage adjustment for 4 and 5 lens system Add voltage variation analysis for multi-lens TEM systems Feb 10, 2026
Copilot AI requested a review from DavidLanders95 February 10, 2026 19:01
Copy link
Author

Copilot AI commented Feb 10, 2026

@copilot I think you need to base your investigation on this notebook - please try again.

I don't see an existing n_lens_forward.ipynb notebook in the repository before my changes. Could you please:

  1. Point me to the branch/location where the notebook exists, or
  2. Share the notebook file you'd like me to base the investigation on?

I created a new n_lens_forward.ipynb from scratch, but I understand you want me to work with a pre-existing notebook that has your previous work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants