Commit Graph

217 Commits

Author SHA1 Message Date
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
1b814fcb8a
src/main.f90: fix chdir to absolute output path 2023-11-24 11:59:09 +01:00
621c725948 Fix unsafe use of merge and missing igrad override 2023-10-20 15:19:56 +02:00
e3656e8bdd Fix debug/optimization flags 2023-10-20 13:42:06 +02:00
7d9f43e1af Remove tolerance when searching ray-wall intersections 2023-10-05 16:37:56 +02:00
cf8a37fec9 Fix label for Icd units in log output 2023-10-04 16:56:05 +02:00
526195ca94 Fix input files documentation 2023-09-22 15:55:29 +02:00
Michele Guerini Rocco
2fed2ab5ec
src/gray_core.f90: remove unused variables in rhs 2023-09-21 14:21:25 +02:00
Michele Guerini Rocco
f75cedbf2b
src/gray_params.f90: fix comment 2023-09-21 14:21:25 +02:00
Michele Guerini Rocco
8bc5ac0064
always use generic math functions
This replaces double precision-specific function in order to allow
building GRAY with other real kinds, for example single or quadruple
precision.
2023-09-21 14:21:24 +02:00
Michele Guerini Rocco
dcc832ce65
fix simple limiter in analytic equilibrium
In the case of analytic equilibrium without a limiter contour, the
simple limiter was built incorrectly due to an unnecessary conversion
from cm (the equilibrium data are already in metre).
2023-09-20 16:15:41 +02:00
Michele Guerini Rocco
61605d3478
fix density calculation in analytical mode
In the analytical mode the density does not have a tail, so tail%end is
not initialised. The density is exactly zero at ψ≥1.
2023-09-20 16:14:22 +02:00
Michele Guerini Rocco
f5a983f926
src/ini_parser.f90: open file for reading 2023-09-20 16:14:22 +02:00
Michele Guerini Rocco
59dfcb7799
src/gray_cli.f90: allow enabling or disabling all units 2023-09-20 16:14:21 +02:00
Michele Guerini Rocco
8425e5e286
src/gray_cli.f90: simplify argument handling 2023-09-20 16:14:21 +02:00
Michele Guerini Rocco
a27e56933c
src/main.f90: give priority to cli over beamdata.txt 2023-09-20 16:14:21 +02:00
Michele Guerini Rocco
62c3045a7f
src/ini_parser.f90: avoid procedure pointer
In this case a normal procedure dummy argument works as well.
2023-09-20 16:14:21 +02:00
Michele Guerini Rocco
69367ae981
src/gray_core.f90: print profiles up to ψ_bnd 2023-09-20 16:14:20 +02:00
Michele Guerini Rocco
707dca1ab8
src/coreprofiles.f90: add some debug output 2023-09-14 11:26:57 +02: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
9bcad028b1
improve error handling in the gray_main routine
- rename errocodes → gray_errors

- restructure the errors into a `error_spec` type

- make the list of errors easily extensible

- rewrite the `print_errn`, `print_errhcd` (now `print_err_raytracing`,
  `print_err_ecrh_cd`) subroutine to handle arbitrary errors

- add functions to easily manipulate errors
  (`raise_error`, `has_error`, `is_critical`)

- remove print statements from quadpack

- log all errors to stderr using the logger module
2023-05-11 17:47:13 +02:00
Michele Guerini Rocco
78d8bdbb33
src/coreprofiles.f90: exit on density fit errors 2023-05-11 17:47:05 +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
1261860f40
add some useful debug messages 2023-04-12 23:45:49 +02:00
Michele Guerini Rocco
cc889bb5a0
use the logger everywhere
This converts the last remaining warnings to use the logging system.
Also drops `catand` and replace it with the intrinsic `atan`, which
supports complex as well as real numbers.

Note: before 3eab989d the `catand` function was actually incorrent!
The definition of arctan(z) can be obtained starting from the identity

  d/dz arctan(z) = 1/(1 + z²) = ½ [1/(1 + iz) + 1/(1 - iz)],

integrating and using the definition log(z) = ∫₁^z dz/z,

  arctan(z) = -i/2 [log(1 + iz) - log(1 - iz)].

If log is the principal branch, log(z) = log|z| + i arg(z), then

  arctan(z) = -i/2 log(w) = 1/2 arg(w) -i/2 log|w|

where w = (1 + iz)/(1 - iz). Finally, the real part is

  Re arctan(z) = 1/2 atan2(2Re(z), 1 - |z|²).

The term -|z|² is missing from the `catand` definition of GRAY,
but is present in the original Fortran 77 code from [SLATEC]:
it has probably been lost in the translation.

[SLATEC]: https://people.math.sc.edu/Burkardt/f_src/slatec/slatec.f90
2023-04-12 23:45:49 +02:00
Michele Guerini Rocco
0a63a20e73
src/dispersion.f90: cleanup
- merge branch with a method to control the speed of iteration and
  improve the convergence of `warmdisp` (thanks Thomas)

- unify `diel_tens_fr` and `diel_tens_wr` into a single subroutine,
  `dielectric_tensor`

- stay as close as possible to the notation of Daniela Farina's paper

- make `sox` an integer

- mark more subroutines as pure

- add more comments
2023-04-12 23:45:49 +02:00
Michele Guerini Rocco
c7d0d8370c
src/gray_core.f90: make some {disp,plas}_deriv outputs optional
Some of the outputs of disp_deriv and plas_deriv are only needed
when updating the local plasma quantities (ywppla_upd) and not when
integrating the raytracing equations (rkstep).

This change save some unnecessary computations and variable definitions.

Also add some comments to disp_deriv
2023-04-12 23:44:38 +02:00
Michele Guerini Rocco
658389f586
src/multipass.f90: improve error wording 2023-04-12 23:44:38 +02:00
Michele Guerini Rocco
69308901ee
src/beams.f90: document read_beam{0,1} formats 2023-04-12 23:44:38 +02:00
Michele Guerini Rocco
038864a84f
src/gray_params.f90: remove unused variable 2023-04-12 23:44:37 +02:00
Michele Guerini Rocco
018fd8eea2
improve some comments 2023-04-12 23:44:37 +02:00
Michele Guerini Rocco
92f0cad3c1
depend: fix typo 2023-03-30 11:06:33 +02:00
Michele Guerini Rocco
f169db3a70
default.nix: update to Nixpkgs 21.11 2023-03-29 21:52:21 +02:00
e6d864e63b Fix the summation mode + temporary fix for file unit numbers
- Add missing array allocations
- Add parameter for varying number of columns in input files
- Change output unit numbers (dirty fix. Original units created an empty
  named file, but wrote in default named fort.* files)
2022-11-24 23:37:29 +01:00
411c4ece48 Fix problem with long filenames not fitting the header width 2022-11-24 23:34:09 +01:00
aa3b816ffe Update license 2022-11-18 19:47:50 +01:00
43de968b46 Add License files 2022-11-17 19:42:44 +01:00
10dc3ba3d0 Improve documentation build
- Documentation is not built anymore with the default `all` rule to
improve portability. It must be built explicitly with `make docs`.
- Font types are not specified to allow building on systems with a
restricted set of fonts.
- Syntax fixes in the documentation Markdown.
2022-11-17 19:31:58 +01:00
ddfc5db039 Add -fPIC flag to compile successfully on some systems 2022-11-14 18:23:20 +00:00
5ff1d8cd10 Replace str_index function with intrinsic index 2022-11-14 16:57:57 +00:00
91fa6d84e0 Add first draft of Python plotting script 2022-11-13 01:57:09 +01:00
6181b6096e Fix Makefile to remove literal '\n' printed in .d files in some systems. 2022-10-28 13:32:56 +02:00
3eab989d7b Fix bug in catand (complex arc tangent)
Function catand returned an incorrect value for its real part. To be
possibly replaced with the intrinsic function atan.
2022-10-04 19:05:43 +02:00
8c3e246f25 Add missing iprof parameter initialization in GRAY/JINTRAC interface
Cherry-pick from commit 6627cb14
2022-10-04 18:43:13 +02:00