Commit Graph

29 Commits

Author SHA1 Message Date
Michele Guerini Rocco
23a8caff37
remove references to COCOS 0,10
While technically accepted by GRAY, these indices do not carry a special
meaning, as wrongly implied by the documentation: they are equivalent
to 8, 18 and specifically don't change the meaning of sgnbi,sgni.
2024-02-09 11:16:20 +01:00
Michele Guerini Rocco
c99fc0b343
src/equilibrium.f90: set q=0 for ψ>1 in numeric case 2024-02-09 11:16:20 +01:00
Michele Guerini Rocco
85c84c9a48
src/equilibrium.f90: fix J_phi at ψ>1 in print_prof
Since 24edfdc4 print_prof tabulates the input profiles up to ψ_bnd,
however the torr_curr_psi may fail (producing FPEs) outside ψ=1.
Moreover, torr_curr_psi gives the current on the LFS on the specific
line z=z_maxis, while the flux surface average is usually more
interesting, say when comparing J_φ with Jcd.

This change fixes both issues.
2024-02-09 11:16:20 +01:00
Michele Guerini Rocco
7ed2f9a394
src/equilibrium.f90: fix floating point exception
If frhopol is called with a very small ρ_p, less than then step size of
ε_machine^⅓, the two sided finite step differentiation evaluates frhotor
at ρ_p < 0, producing a floating point exception.
In this case we use the single- sided definition.
2024-02-09 11:16:20 +01:00
Michele Guerini Rocco
32f44c5cba
src/splines.f90: add procedure to transform spline_2d
This adds a proper procedure to rescale and shift a 2D B-spline by
manipulating the coefficients.

Note: the previous code in set_equil_spline did work, but by
transforming the whole partial(i,j) triggered warnings about operations
on uninitialised memory.
2024-02-09 11:16:20 +01:00
Michele Guerini Rocco
7c5b443847
src/equilibrium.f90: use enums 2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
c5a4b180bc
src/gray_params.f90: replace magic numbers with enums
1. Introduces enumerations (and some booleans) intended to replace all
   the magic numbers used throughout the code to represent multiple
   choices.

2. Replace the gray_params.sh script a new one that automatically
   generates code for all the GRAY parameters by parsing
   gray_params.f90.

3. Also generate extra code to accept the enum identifiers as valid
   values in the configuration files and command line arguments.

4. Set sensible default values for parameters that are rarely changes.
2024-02-09 11:16:18 +01:00
Michele Guerini Rocco
73bd010458
remove unnecessary implicit statements
Only a single `implicit none` at the start of each module is required.
2024-02-09 11:16:18 +01:00
86ff5ecb06
improve inside and move it to utils.f90
This slightly improves the performance of inside.
For a ~100 points contour the instructions cost is reduced by ~5%.
2024-02-09 11:16:18 +01:00
Michele Guerini Rocco
686f63b01a
fix some gfortran warnings 2024-02-09 11:16:17 +01:00
Michele Guerini Rocco
26976a31dd
src/equilibrium.f90: give the poloidal flux a definition domain
This change limits the evaluation of the poloidal flux spline ψ(R,z) to
a particular domain where ψ(R,z) is strictly monotonic. This choice
ensures that ψ_n < 1 only inside the plasma boundary; so plasma
parameters like q(ψ), Te(ψ), etc. can be mapped to the physical space
without ambiguities.

Before this change, anywhere ψ_n happened to decrease below 1 (either as
a result of a physical current or simply from the spline extrapolation),
Gray would effectively create a spurious plasma region.
This behavior is seriously problematic because it completely invalidates
the simulation: it can alter the ray directions, the power disribution
and total power if the beam enters one such region.

The domain is computed by applying a transformation to the contour of
the plasma boundary: for each contour point we cast a ray from the
magnetic axis to that point and extend the ray until the restriction of
ψ(R,z) on the ray starts decreasing or reach a maximum scaling factor.
The endpoint of the ray is then taken as the new point.
If ψ(R,z) is globally monotonic, the transformation is a homotety wrt
the magnetic axis, so the domain will be an enlarged boundary; otherwise
the shape will be more irregular (an intersection of the enlarged
boundary and several level curves of ψ).

Finally, each `pol_flux(r, z)` call is now guarded behind a check
`inside(psi_domain, r, z)`. For points outside the domain the subroutine
returns, as usual, -1 for ψ and 0 for derivatives.
2024-02-09 11:16:17 +01:00
Michele Guerini Rocco
95d398d503
src/equilibrium.f90: use the exact toroidal flux
To improve the analytical model correctness this changes the formula
of the toroidal flux to lift the large aspect ratio approximation (a << R₀).
In fact, Φ(r) = B₀πr² is technically inconsistent with the field varying
as B₀R₀/R. The exact expression is:

  Φ(r) = B₀πr² 2/[1 + √(1 - r²/R₀²)],

which is approximately equal to the former for r << R₀.

Note that this change introduces a divergence in the poloidal field at
r=R₀ (since ∂Φ/∂r → +∞), so the domain of the equilibrium has been
restricted to r<R₀, as expected.
This should not be a concern because the field outside the plasma
boundary is never directly, particularly not by the integrator.
2024-01-24 14:37:20 +01:00
Michele Guerini Rocco
9a301f5799
src/equilibrium.f90: re-introduce ρ_t(R,z) mapping
Directly mapping r(R,z) to ρ_p greatly simplifies the implementation of
pol_flux, but also produces a weird current profile and resonance curve.

This keeps the previous changes to the model but reverts the mapping to
the previous one: r → ρ_t.
2024-01-24 14:37:20 +01:00
Michele Guerini Rocco
24edfdc43a
src/gray_core.f90: print_* do not depend on q_spline
This changes all the print_* subroutines to not depend on q_spline,
either for the ψ data points or just their number.
In fact, in the new analytical model q_spline doesn't exist anymore.

Pro: This allows to mark q_spline as private.
2024-01-24 14:37:20 +01:00
Michele Guerini Rocco
8d86d70e91
src/equilibrium.f90: pol_curr return derivative wrt ψ_n 2023-12-12 23:37:56 +01:00
Michele Guerini Rocco
31a7b95fe1
combine equian and equinum_* subroutines
This change combines `equian` and `equinum_psi` into a new `pol_flux`
subroutine that computes ψ(R,z) and derivatives for either numerical or
analytical equilibria.
Similarly, `equian` (the fpol and dfpol outputs) and `equinum_fpol` are
combined intro `pol_curr` that computes F(ψ) and its derivative.

Callers of these subroutines do not select a specific version based on
the value of `iequil` anymore.
2023-12-12 23:37:56 +01:00
Michele Guerini Rocco
98dda6d6fa
src/equilibrium.f90: improve handling of the ψ normalisation
- Hide the implementation of the re-normalisation of the ψ(R,z) spline
  by adjusting the spline coefficients, instead of shifting and
  rescaling after each evaluation.

- Correct the value of `psia` = ψ(X point) - ψ(O point) after the ψ(R,z)
  spline has been re-normalised.

  This fixes another instance of decoupling between the values of X and
  ∇X that introduce a systematic error in the numerical integration of
  the raytracing equations.
  Here the issue is caused by the different normalisations used,
  specifically X=X(ψ_n') and ∇X=∇X(ψ_n), where ψ_n' is the re-normalised
  spline of the normalised flux and ψ_n the spline of the normalised
  flux.

  See d0a5a9f for more details on problems resulting from this error.

- Change `equian` and `equinum_psi` to return the normalised values
  for both the flux and its derivatives, to avoid confusions.
  Callers that needed the unnormalised derivatives now multiply
  explicitly by `psia`.
2023-12-12 23:37:56 +01:00
Michele Guerini Rocco
127d574be7
rework the analytical model
This change modifies the analytical equilibrium in order to simplify the
computation of the poloidal flux normalization and the derivatives.

In the power law parametrisation of the safety factor, ρ_t is replaced
with ρ_p and, similarly, the normalised poloidal radius is now
identified with ρ_p, instead of ρ_t.
With the same parameters (q₀,q₁,α...), this choice slightly changes the
plasma current distribution, but enables us to obtain a closed form for
ψ_a = ψ(r=a) and the relation ρ_t(ρ_p). In fact, both expressions are
now obtained by integrating the q(ρ_p), instead of 1/q(ρ_t), which has
no elementary antiderivative.

As the normalisation is now computed exactly, the values of the
normalised flux ψ_n = ψ/ψ_a and the gradient ∇ψ (entering the raytracing
equations in X and ∇X, respectively) are computed to the same precision.
Previously, ψ_n was computed to a lower precision due to the use of a
simple trapezoid integration of 1/q(ρ_p) for ψ_a, while ∇ψ was computed
up to machine precision using an exact formula.

This error effectively caused a very slight decoupling between X=ω_p²/ω²
and ∇X that introduced a systematic error in the numerical solution of
the raytracing equations.
The error manifests itself as a bias with a weak dependency on X in the
values taken by the dispersion function Λ(r̅, n̅) on the phase-space
points generated by the integrator. More specifically,

  lim h→0 Λ(r̅_i, n̅_i) = -kX(r̅_i)

where h is the integrator step size;
      r̅_i is the position at the i-th step;
      k ≈ -3.258⋅10⁻⁵ and depends only on the number of points used to
        perform the trapedoid integral for ψ_a (as ~ 1/n²).

After this change Λ behaves consistently with being a conserved quantity
(zero) up to the cumulative integration error of the 4° order
Runge-Kutta method. In fact we now have that:

  Λ(r̅_i, n̅_i) ∝ - h⁴ ‖∂⁴X(r̅_i)/∂r̅⁴‖

It must be said that within this model the relation ρ_p(ρ_t) can't be
computed analytically (inverting ρ_t(ρ_p) produces a trascendental
equation of the form b = x + c x^α). However, this relation is not
necessary for raytracing and is easily solved, up to machine
precision, using minpack.

In addition, this change also makes the model consistetly use the
cocos=3 and fully implements the ability to force the signs of I_p, B_φ
(via equilibrium.sgni,sgnb) and rescaling the field (via
equilibrium.factb).
2023-12-12 23:37:43 +01:00
Michele Guerini Rocco
7ed76959d4
call exit on errors only in main 2023-09-14 11:26:56 +02:00
Michele Guerini Rocco
daf3d500af
use linear interpolation for monotonic data
The ρ_p/ρ_t mapping is 1:1, so the interpolation must always preserve
monotonicity, of which cubic splines generally make no guarantee.

Note: Linear interpolation does not provide even C¹ continuity, but
these data is not directly used in the numerical integration, so it
should be fine. Ideally this should be replaced with cubic splines
computed with the Fritsch–Carlson algorithm.
2023-09-14 11:26:56 +02:00
Michele Guerini Rocco
b1accf0ae3
simplify handling of splines
This adds a new `splines` module which implements a high-level interface
for creating and evaluating splines and rewrite almost all modules to
use it. Also, notably:

1. both `simplespline` and DIERCKX splines can now used with a uniform
   interface

2. most complexity due to handling working space arrays is gone

3. memory management has been significantly simplified too
2023-04-12 23:45:49 +02:00
Michele Guerini Rocco
63e2bf0b04
convert remaining subroutines to derived types
- converts analytical profiles/equilibrium subroutines to derived types
- use less undecipherable and consistent names

The subroutine names have changed as follows:

    set_prfspl → set_profiles_spline
    set_prfan  → set_profiles_an
  unset_prfspl → unset_profiles_spline
  unset_prfan  → unset_profiles_an
  set_equian   → set_equil_an
  set_eqspl    → set_equil_spline
  unset_equian → unset_equil_an
  unset_eqspl  → unset_equil_spline
  unset_rhospl → unset_rho_spline
2022-05-22 01:18:08 +02:00
Michele Guerini Rocco
add59dbdda
src: use the logging system everywhere 2022-05-11 01:15:04 +02:00
Michele Guerini Rocco
ef1617713f
src: simplify handling of free units
This changes the behavior of `get_free_unit` to simplify the handling of
optional unit numbers.
2022-05-11 01:15:03 +02:00
Michele Guerini Rocco
948a512254
src: use derived type arguments (work in progress)
This change structures the arguments of most functions, in particular
gray_main, into well-defined categories using derived types.

All types are defined in the gray_params.f90 (location subject to
change) and are organised as follows:

  gray_parameters (statically allocated data)
  ├── antenna_parameters
  ├── ecrh_cd_parameters
  ├── equilibrium_parameters
  ├── misc_parameters
  ├── output_parameters
  ├── profiles_parameters
  └── raytracing_parameters

  gray_data - inputs of gray_main (dynamically-allocated arrays)
  ├── equilibrium_data
  └── profiles_data

  gray_results - outputs of gray_main (dynamically-allocated arrays)
2022-05-11 01:15:01 +02:00
Michele Guerini Rocco
861a723ebc
src/equilibrium.f90: fix mixed tab/spaces 2022-05-11 01:14:59 +02:00
Daniele Micheletti
27f1793f14 trunk: added multiple passes calculation; added multipass module; graycore/gray_main: subroutine rewritten; equilibrium/read_equil_an: limiter coordinates read from equilibrium file for ipass>1; err_codes/check_err: istop=1 only for alpha<0; pec/pec_tab: fixed index assignements in loops; polarization/pol_limit: fixed ext,eyt normalization 2019-03-26 14:21:22 +00:00
46e36a5792 re-added missing output files, file units specified in dedicated module, added option iequil=3 for partially filled psi(R,z) grid on input: required for integration in JETTO 2015-11-23 17:55:27 +00:00
ce10204479 nocommon branch merged back into trunk 2015-11-18 16:34:33 +00:00