You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add examples/compare_ncsx_meiss_vmec_ck.py to compare guiding-center orbits in the NCSX free-boundary equilibrium using Cash–Karp GC in VMEC vs Meiss (coils) coordinates, based on the STELLOPT NCSX wout and coils files.
PR Type
Enhancement
Description
Add comprehensive comparison script for NCSX guiding-center orbits
Implements Cash–Karp RK5(4) integration in VMEC vs Meiss coordinates
Automates NCSX equilibrium file download and coils format conversion
Generates visual comparison plots of orbit trajectories (s, theta, phi)
Diagram Walkthrough
flowchart LR
A["NCSX Files<br/>wout + coils"] -->|Download & Convert| B["Local Test Data"]
B -->|Link| C["Work Directory"]
C -->|Config 1| D["SIMPLE VMEC<br/>Cash–Karp"]
C -->|Config 2| E["SIMPLE Meiss<br/>Cash–Karp"]
D -->|orbits.nc| F["Read & Plot<br/>Comparison"]
E -->|orbits.nc| F
F -->|Output| G["Comparison Plot<br/>s, theta, phi vs time"]
Loading
File Walkthrough
Relevant files
Enhancement
compare_ncsx_meiss_vmec_ck.py
NCSX Cash–Karp orbit comparison and visualization script
examples/compare_ncsx_meiss_vmec_ck.py
New 279-line script comparing NCSX guiding-center orbits using Cash–Karp RK5(4) integration
Implements automatic download of NCSX VMEC equilibrium and coils files from STELLOPT
Converts STELLOPT coils format to SIMPLE's coils.simple format with filament coordinates
Runs SIMPLE twice with identical parameters except field type (VMEC vs Meiss canonical coordinates)
Reads orbit trajectories from netCDF output and generates multi-panel comparison plots
Below is a summary of compliance checks for this PR:
Security Compliance
⚪
Insecure external downloads
Description: Unauthenticated HTTP downloads of external files without integrity or certificate pinning (via urlretrieve) allow MITM/tampering of the NCSX wout and coils inputs, which are then executed by the simulator and could lead to compromised results or code execution if the toolchain parses malformed files. compare_ncsx_meiss_vmec_ck.py [62-69]
Description: Creation of filesystem symlinks in a shared work directory without validation can be abused if an attacker pre-places a symlink to an arbitrary path, causing the script to overwrite or read unintended files when linking field inputs. compare_ncsx_meiss_vmec_ck.py [119-119]
Referred Code
os.symlink(src, dst)
Unsandboxed external execution
Description: Running an external binary (simple.x) with user-controlled configuration from the network-fetched files without sandboxing or path validation risks arbitrary code execution if the binary is trojaned or if crafted inputs trigger exploitable parser bugs. compare_ncsx_meiss_vmec_ck.py [121-134]
Referred Code
print(f"Running SIMPLE [{tag}] in {run_dir}")
res=subprocess.run(
[simple_exe, cfg_path],
cwd=run_dir,
capture_output=True,
text=True,
timeout=1800,
)
ifres.returncode!=0:
print(f"SIMPLE run '{tag}' failed")
print("STDOUT:", res.stdout[-2000:] iflen(res.stdout) >2000elseres.stdout)
print("STDERR:", res.stderr[-2000:] iflen(res.stderr) >2000elseres.stderr)
raiseRuntimeError(f"SIMPLE run '{tag}' failed with exit code {res.returncode}")
TOCTOU file deletion
Description: Unconditionally removing existing paths before creating symlinks (os.remove(dst) on an attacker-controlled or unexpected path) can delete files outside the intended directory if dst is a malicious symlink (TOCTOU). compare_ncsx_meiss_vmec_ck.py [171-175]
Objective: To prevent the leakage of sensitive system information through error messages while providing sufficient detail for internal debugging.
Status: Verbose subprocess logs: On failure the script prints captured stdout/stderr from the external SIMPLE run to the console, which may expose internal paths or sensitive environment details depending on SIMPLE output.
Referred Code
ifres.returncode!=0:
print(f"SIMPLE run '{tag}' failed")
print("STDOUT:", res.stdout[-2000:] iflen(res.stdout) >2000elseres.stdout)
print("STDERR:", res.stderr[-2000:] iflen(res.stderr) >2000elseres.stderr)
raiseRuntimeError(f"SIMPLE run '{tag}' failed with exit code {res.returncode}")
Objective: To ensure logs are useful for debugging and auditing without exposing sensitive information like PII, PHI, or cardholder data.
Status: Unstructured prints: The script uses plain print statements for operational events and may echo unstructured external process output, lacking structured logging and controls to avoid sensitive data exposure.
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent vulnerabilities
Status: External input trust: The script downloads and parses external files and symlinks them without validation (e.g., checksums, schema/variable presence), and executes an external binary based on repository path without verification.
To improve reliability, bundle the required NCSX data files within the repository instead of downloading them from external URLs. This change makes the example self-contained and prevents failures from broken links.
defensure_ncsx_files(test_data_dir: str):
# Data files are now part of the repository, no download needed.# The function would just verify existence and perform the conversion.os.makedirs(test_data_dir, exist_ok=True)
wout_ncsx=os.path.join(test_data_dir, "wout_ncsx.nc")
coils_stellopt=os.path.join(test_data_dir, "coils.c09r00")
coils_simple=os.path.join(test_data_dir, "coils.c09r00.simple")
ifnotos.path.exists(coils_simple):
# Convert the bundled STELLOPT coils fileprint("Converting NCSX coils to SIMPLE format...")
# ... conversion logic ...returnwout_ncsx, coils_simple
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies a significant reliability issue with downloading files from external URLs, which could break the example in the future, and proposes a robust solution to make it self-contained.
Medium
Possible issue
Prevent crashes when creating symlinks
Improve symlink creation by handling cases where the destination path is an existing directory. Use os.path.lexists() and shutil.rmtree() to prevent potential OSError exceptions.
for src, dst in ((wout_ncsx, wout_link), (coils_simple, coils_link)):
- if os.path.islink(dst) or os.path.exists(dst):- os.remove(dst)+ if os.path.lexists(dst):+ if os.path.isdir(dst) and not os.path.islink(dst):+ import shutil+ shutil.rmtree(dst)+ else:+ os.remove(dst)
os.symlink(src, dst)
Apply / Chat
Suggestion importance[1-10]: 6
__
Why: The suggestion correctly identifies that os.remove() will fail on a directory and provides a robust solution using os.path.lexists() and shutil.rmtree() to handle this edge case, improving the script's reliability.
Low
More
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Add examples/compare_ncsx_meiss_vmec_ck.py to compare guiding-center orbits in the NCSX free-boundary equilibrium using Cash–Karp GC in VMEC vs Meiss (coils) coordinates, based on the STELLOPT NCSX wout and coils files.
PR Type
Enhancement
Description
Add comprehensive comparison script for NCSX guiding-center orbits
Implements Cash–Karp RK5(4) integration in VMEC vs Meiss coordinates
Automates NCSX equilibrium file download and coils format conversion
Generates visual comparison plots of orbit trajectories (s, theta, phi)
Diagram Walkthrough
File Walkthrough
compare_ncsx_meiss_vmec_ck.py
NCSX Cash–Karp orbit comparison and visualization scriptexamples/compare_ncsx_meiss_vmec_ck.py
Cash–Karp RK5(4) integration
from STELLOPT
filament coordinates
Meiss canonical coordinates)
comparison plots