Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 61 additions & 60 deletions src/Vacuum/Vacuum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Initialize DCON parameters for vacuum field calculations.

# Arguments

- `mtheta`: Number of theta grid points (Integer)
- `lmin`: Minimum poloidal mode number (Integer)
- `lmax`: Maximum poloidal mode number (Integer)
- `nnin`: Toroidal mode number (Integer)
- `qa1in`: Safety factor parameter (Float64)
- `xin`: Vector of radial coordinates at plasma boundary (Vector{Float64})
- `zin`: Vector of vertical coordinates at plasma boundary (Vector{Float64})
- `deltain`: Vector of displacement values (Vector{Float64})
- `mtheta`: Number of theta grid points (Integer)
- `lmin`: Minimum poloidal mode number (Integer)
- `lmax`: Maximum poloidal mode number (Integer)
- `nnin`: Toroidal mode number (Integer)
- `qa1in`: Safety factor parameter (Float64)
- `xin`: Vector of radial coordinates at plasma boundary (Vector{Float64})
- `zin`: Vector of vertical coordinates at plasma boundary (Vector{Float64})
- `deltain`: Vector of displacement values (Vector{Float64})

# Note

Expand Down Expand Up @@ -75,8 +75,8 @@ and resets the internal DCON state for future vacuum calculations.

# Notes

- Must be called after `set_dcon_params` if you want to reset the DCON memory.
- No arguments are required.
- Must be called after `set_dcon_params` if you want to reset the DCON memory.
- No arguments are required.

# Example

Expand All @@ -99,22 +99,22 @@ Compute the vacuum response matrix for magnetostatic perturbations.

# Arguments

- `wv`: Pre-allocated complex matrix (mpert × mpert) to store vacuum response (Array{ComplexF64,2})
- `mpert`: Number of perturbation modes (Integer)
- `mtheta`: Number of theta grid points for plasma (Integer)
- `mtheta_vacuum`: Number of theta grid points for vacuum region (Integer)
- `complex_flag`: Whether to use complex arithmetic (Bool)
- `kernelsignin`: Sign convention for vacuum kernels (Float64, typically -1.0)
- `wall_flag`: Whether to include an externally defined wall shape (Bool)
- `farwall_flag`: Whether to use far-wall approximation (Bool)
- `grrio`: Green's function data (Array{Float64,2})
- `xzptso`: Source point coordinates (Array{Float64,2})
- `op_ahgfile`: Optional communication file for when set_dcon_params is not called (String or Nothing)
- `wv`: Pre-allocated complex matrix (mpert × mpert) to store vacuum response (Array{ComplexF64,2})
- `mpert`: Number of perturbation modes (Integer)
- `mtheta`: Number of theta grid points for plasma (Integer)
- `mtheta_vacuum`: Number of theta grid points for vacuum region (Integer)
- `complex_flag`: Whether to use complex arithmetic (Bool)
- `kernelsignin`: Sign convention for vacuum kernels (Float64, typically -1.0)
- `wall_flag`: Whether to include an externally defined wall shape (Bool)
- `farwall_flag`: Whether to use far-wall approximation (Bool)
- `grrio`: Green's function data (Array{Float64,2})
- `xzptso`: Source point coordinates (Array{Float64,2})
- `op_ahgfile`: Optional communication file for when set_dcon_params is not called (String or Nothing)

# Returns

- Modifies `wv` in-place with the computed vacuum response matrix
- Returns the modified `wv` matrix
- Modifies `wv` in-place with the computed vacuum response matrix
- Returns the modified `wv` matrix

# Note

Expand Down Expand Up @@ -201,28 +201,28 @@ end
"""
compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSettings)

Compute the vacuum response matrix using provided vacuum inputs.
Compute the vacuum response matrix using provided vacuum inputs.

This is the pure Julia implementation that replaces the Fortran `mscvac` function.
This is the pure Julia implementation that replaces the Fortran `mscvac` function.
It returns the relevant arrays: `wv`, `grri`, and `xzpts`.

# Arguments

- `inputs::VacuumInput`: Struct containing vacuum calculation parameters including mode numbers,
grid resolution, toroidal mode number, and plasma boundary information.
- `wall_settings::WallShapeSettings`: Struct specifying the wall geometry configuration.
- `inputs::VacuumInput`: Struct containing vacuum calculation parameters including mode numbers,
grid resolution, toroidal mode number, and plasma boundary information.
- `wall_settings::WallShapeSettings`: Struct specifying the wall geometry configuration.

# Returns

- `wv`: Complex vacuum response matrix (mpert × mpert) relating plasma perturbations to vacuum response
- `grri`: Green's function response matrix (2*mtheta × 2*mpert) in Fourier space
- `xzpts`: Coordinate array (mtheta × 4) containing [R_plasma, Z_plasma, R_wall, Z_wall]
- `wv`: Complex vacuum response matrix (mpert × mpert) relating plasma perturbations to vacuum response
- `grri`: Green's function response matrix (2*mtheta × 2*mpert) in Fourier space
- `xzpts`: Coordinate array (mtheta × 4) containing [R_plasma, Z_plasma, R_wall, Z_wall]

# Notes

- This function initializes the plasma surface and wall geometry internally
- The vacuum response includes plasma-plasma and plasma-wall coupling effects
- For n=0 modes with closed walls, a regularization factor is added to prevent singularities
- This function initializes the plasma surface and wall geometry internally
- The vacuum response includes plasma-plasma and plasma-wall coupling effects
- For n=0 modes with closed walls, a regularization factor is added to prevent singularities
"""
function compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSettings)

Expand All @@ -236,7 +236,7 @@ function compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSe

# Plasma–Plasma block
j1, j2 = 1, 1
kernel!(grad_greenfunction_mat, greenfunction_temp, plasma_surf.x, plasma_surf.z, plasma_surf.x, plasma_surf.z, j1, j2, 1, n)
kernel!(grad_greenfunction_mat, greenfunction_temp, plasma_surf.x, plasma_surf.z, plasma_surf.x, plasma_surf.z, j1, j2, n)

# Fourier transform plasma-plasma block
fourier_transform!(grri, greenfunction_temp, plasma_surf.cslth, 0, 0)
Expand All @@ -245,15 +245,15 @@ function compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSe
!wall.nowall && begin
# Plasma–Wall block
j1, j2 = 1, 2
kernel!(grad_greenfunction_mat, greenfunction_temp, plasma_surf.x, plasma_surf.z, wall.x, wall.z, j1, j2, 0, n)
kernel!(grad_greenfunction_mat, greenfunction_temp, plasma_surf.x, plasma_surf.z, wall.x, wall.z, j1, j2, n)

# Wall–Wall block
j1, j2 = 2, 2
kernel!(grad_greenfunction_mat, greenfunction_temp, wall.x, wall.z, wall.x, wall.z, j1, j2, 0, n)
kernel!(grad_greenfunction_mat, greenfunction_temp, wall.x, wall.z, wall.x, wall.z, j1, j2, n)

# Wall–Plasma block
j1, j2 = 2, 1
kernel!(grad_greenfunction_mat, greenfunction_temp, wall.x, wall.z, plasma_surf.x, plasma_surf.z, j1, j2, 1, n)
kernel!(grad_greenfunction_mat, greenfunction_temp, wall.x, wall.z, plasma_surf.x, plasma_surf.z, j1, j2, n)

# Fourier transform wall blocks into grri
fourier_transform!(grri, greenfunction_temp, plasma_surf.cslth, mtheta, 0)
Expand All @@ -274,7 +274,7 @@ function compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSe
(kernelsign < 0) && begin
grad_greenfunction_mat .*= kernelsign
# Account for factor of 2 in diagonal terms in eq. 90 of Chance
for i in 1:2 * mtheta
for i in 1:(2*mtheta)
grad_greenfunction_mat[i, i] += 2.0
end
end
Expand Down Expand Up @@ -323,40 +323,41 @@ function compute_vacuum_response(inputs::VacuumInput, wall_settings::WallShapeSe
end

"""
compute_vacuum_field(inputs::VacuumInput, plasma_surf::PlasmaGeometry, wall::WallGeometry,
compute_vacuum_field(inputs::VacuumInput, plasma_surf::PlasmaGeometry, wall::WallGeometry,
Bn::Vector{<:Number}, R_grid::AbstractVector, Z_grid::AbstractVector)

Calculate the perturbed magnetic field in the vacuum region resulting from a normal
magnetic field perturbation (`Bn`) at the plasma surface. Replaces `mscfld` from Fortran.

This function orchestrates the vacuum field calculation by:
1. Calling `vaccal!` to compute the vacuum response kernel (`grri`)
2. Defining a grid of points (`R_grid`, `Z_grid`) where the field is to be calculated
3. Calling `_pickup_field` to compute the magnetic field components on that grid using the kernel
and the source perturbation `Bn`

1. Calling `vaccal!` to compute the vacuum response kernel (`grri`)
2. Defining a grid of points (`R_grid`, `Z_grid`) where the field is to be calculated
3. Calling `_pickup_field` to compute the magnetic field components on that grid using the kernel
and the source perturbation `Bn`

# Arguments

- `inputs::VacuumInput`: Struct containing vacuum calculation parameters (n, mpert, mtheta, etc.)
- `plasma_surf::PlasmaGeometry`: Struct with plasma surface geometry and basis functions
- `wall::WallGeometry`: Struct with wall geometry
- `Bn::Vector{<:Number}`: Complex vector of Fourier harmonics of the normal magnetic field
perturbation at the plasma surface, `B_n = B_n_real + i*B_n_imag`. Length must be `mpert`.
- `R_grid::AbstractVector`: Vector of R coordinates for the output field grid
- `Z_grid::AbstractVector`: Vector of Z coordinates for the output field grid
- `inputs::VacuumInput`: Struct containing vacuum calculation parameters (n, mpert, mtheta, etc.)
- `plasma_surf::PlasmaGeometry`: Struct with plasma surface geometry and basis functions
- `wall::WallGeometry`: Struct with wall geometry
- `Bn::Vector{<:Number}`: Complex vector of Fourier harmonics of the normal magnetic field
perturbation at the plasma surface, `B_n = B_n_real + i*B_n_imag`. Length must be `mpert`.
- `R_grid::AbstractVector`: Vector of R coordinates for the output field grid
- `Z_grid::AbstractVector`: Vector of Z coordinates for the output field grid

# Returns

- `B_R::Matrix{ComplexF64}`: The R-component of the magnetic field on the grid
- `B_Z::Matrix{ComplexF64}`: The Z-component of the magnetic field on the grid
- `B_phi::Matrix{ComplexF64}`: The toroidal component of the magnetic field on the grid
- `grid_info::Matrix{Int}`: Information about the grid points (1=inside plasma, 0=outside)
- `B_R::Matrix{ComplexF64}`: The R-component of the magnetic field on the grid
- `B_Z::Matrix{ComplexF64}`: The Z-component of the magnetic field on the grid
- `B_phi::Matrix{ComplexF64}`: The toroidal component of the magnetic field on the grid
- `grid_info::Matrix{Int}`: Information about the grid points (1=inside plasma, 0=outside)
"""
function compute_vacuum_field(inputs::VacuumInput, plasma_surf::PlasmaGeometry, wall::WallGeometry,
Bn::Vector{<:Number}, R_grid::AbstractVector, Z_grid::AbstractVector)
function compute_vacuum_field(inputs::VacuumInput, plasma_surf::PlasmaGeometry, wall::WallGeometry,
Bn::Vector{<:Number}, R_grid::AbstractVector, Z_grid::AbstractVector)

# 1. Call vaccal! to get the inverted Green's function matrix
# The Fortran version calls the whole chain (ent33 -> vaccal),
# The Fortran version calls the whole chain (ent33 -> vaccal),
# here we assume vaccal! provides what we need.
wv, grri = vaccal!(inputs, plasma_surf, wall)

Expand All @@ -367,12 +368,12 @@ function compute_vacuum_field(inputs::VacuumInput, plasma_surf::PlasmaGeometry,
# 2. Define grid and parameters for pickup routine
nx = length(R_grid)
nz = length(Z_grid)

# 3. Call the field pickup routine
B_R, B_Z, B_phi, grid_info = _pickup_field(
inputs, plasma_surf, grri, Bn_real, Bn_imag, R_grid, Z_grid
)

return B_R, B_Z, B_phi, grid_info
end
end
end
Loading