Commit Graph

212 Commits

Author SHA1 Message Date
Michele Guerini Rocco
bd6e1521b0
tests: check EC profiles 2024-11-04 12:00:20 +01:00
Michele Guerini Rocco
a4a39571ce
src/pec.f90: don't rely on P_inside, I_cd_inside
When combining multiple independent simulations with `gray -s`, the
statistics in sum-summary.txt are computed by summing the profiles
pointwise and calling `postproc_profiles` with their (supposedly common)
MHD equilibrium.

The subroutine assumes the normalisations of dP/dV⋅dV and J_φ⋅dA are
P_inside(ρ=1), I_cd_inside(ρ=1), however if the individual profiles have
been computed with different versions of Gray, built with different
compilers, or on different machines, the values may be very slightly off.
Further, due to the use of the relation Δρ² = ⟨ρ²⟩ - ⟨ρ⟩², the profile
widths are very sensible to the normalisation, producing wildly
incorrect values.

For example, due to the refactor of `magsurf_data`, a change of about
0.05% in dV resulted in Δρ² being off by 300%.
2024-11-04 12:00:20 +01:00
Michele Guerini Rocco
7477fffb43
move flux averages inside gray_equil 2024-11-04 12:00:20 +01:00
Michele Guerini Rocco
8c144c3892
make the sign of q consistent with cocos=3 2024-11-04 12:00:20 +01:00
Michele Guerini Rocco
fdf5ef72fe
src/magsurf_data.f90: cleanup 2024-11-04 12:00:20 +01:00
Michele Guerini Rocco
5c78af975a
add support for intel compilers 2024-11-04 12:00:19 +01:00
Michele Guerini Rocco
03443f1195
src/beams.f90: add option to not change iox in read_beam2
The file format parsed by read_beam2 also includes the polarisation,
unlike those of read_beam0 and read_beam1.
When running gray standalone, however, we expect the mode to be set by
`antenna.iox` in gray.ini, not by the beam file.
2024-11-04 12:00:17 +01:00
Michele Guerini Rocco
cdac0ca361
fixup 15fc891 2024-11-04 12:00:17 +01:00
Michele Guerini Rocco
24e0e6e472
src: remove unnecessary one, zero uses 2024-11-04 12:00:16 +01:00
Michele Guerini Rocco
d52e125d9c
src/gray_core: improve error reporting
- Avoid logging the same error over and over

- Make all the gray_errors actually warnings

- Replace `large_npl` error with `unstable_beam`, which is actually
  the root cause of the former

- Use the gray_main error as exit code
2024-11-04 12:00:16 +01:00
Michele Guerini Rocco
86d5b5a672
src/gray_core: refactor ic_gb 2024-11-03 09:19:22 +01:00
Michele Guerini Rocco
d5bbda1ea2
src/gray_tables: fix memory error with disabled table 2024-11-03 09:19:22 +01:00
Michele Guerini Rocco
10f783ca37
src/gray_errors.f90: avoid strange macro
Some compilers are not happy with this.
2024-11-03 09:19:22 +01:00
Michele Guerini Rocco
918d239b34
src/logger.f90: allow changing output unit and colors 2024-11-03 09:19:22 +01:00
Michele Guerini Rocco
d5c81268de
src/utils.f90: clean up
- Replace the `get_free_unit` subroutine with the built-in
  `newutin` option of the `open` statement.

- Replace `locatex` with just `locate` + an index offset.

- Replace `inside` with `contour%contains`.

- Merge `vmaxmin` and `vmaxmini` into a single subroutine
  with optional arguments.

- Remove unused `range2rect`, `bubble`.
2024-11-03 09:19:21 +01:00
Michele Guerini Rocco
751cca3bfc
mark some procedures as pure 2024-11-03 09:19:18 +01:00
Michele Guerini Rocco
166086d369
replace equilibrium module with an object
Similarly to eb648039 this change replaces the `equilibrium` module with
a new `gray_equil` module providing the same functionality without using
global variables.

  - `read_eqdsk`, `read_equil_an` are replaced by a single `load_equil`
    routine that handles all equilibrium kind (analytical, numerical,
    and vacuum).

  - `scale_equil` is merged into `load_equil`, which besides reading
    the equilibrium from file peforms the rescaling and interpolation based
    on the `gray_parameters` settings and the equilibrium kind.

    To operate on G-EQDSK data specifically, the `change_cocors` and
    `scale_eqdsk` are still available. The numeric equilibrium must then
    be initialised manually by calling equil%init().

  - `set_equil_spline`, `set_equil_an`, `unset_equil_spline`
     are completely removed as the module no longer has any internal state.

  - `fq` is replaced by `equil%safety`; `bfield` by `equil%b_field`;
    `frhotor`, `frhopol` by `equil%pol2tor` and `equil%pol2tor`;
    and the remaining subroutines by other methods of `abstract_equil`
    retaining the old name.

  - the `contours_psi` subroutine is replaced by `equil%flux_contour`,
    with a slightly changed invocation but same functionality.

  - the `gray_data` type is no longer required ans has been removed: all
    the core subroutines now access the input data only though either
    `abstract_equil`, `abstract_plasma` or the `limiter` contour.
2024-11-03 09:18:33 +01:00
Michele Guerini Rocco
ae80fb4945
src/splines.f90: add spline_2d%init_nonreg
This adds a proper subroutine to initialise a spline_2d given
non-regular data using surfit.
2024-10-07 16:19:33 +02:00
Michele Guerini Rocco
15a1f866b4
src/equilibrium: rewrite points_tgo, points_ox
This change adds a bit of documentation and simplifies the two
(internal) subroutines used to find the horizontal tangent points
and the magnetic O/X point.

Using a closure we can avoid explicitly passing parameters (psi0) to
hybrj1. Previously this required a custom `hybrj1mv` subroutine in
fitpack with an identical interface, except for our extra parameter.
2024-10-07 16:19:33 +02:00
Michele Guerini Rocco
c44176a505
src/splines.f90: use do cocurrent for transform 2024-10-07 16:19:33 +02:00
Michele Guerini Rocco
2c441668bb
replace coreprofiles module with an object
This change replaces the `coreprofiles` module with a new `gray_plasma`
module providing the same functionality without using global variables.

  - `read_profiles`, `read_profiles_an` are replaced by a single `load_plasma`
    routines that handles both profiles kind (numerical, analytical).

  - `scale_profiles` is merged into `load_plasma`, which besides reading
    the profiles from file peforms the rescaling and interpolation based
    on the `gray_parameters` settings.

  - `set_profiles_spline`, `set_profiles_an`, `unset_profiles_spline`
     are completely removed as the module no longer has any internal state.

  - `density`, `ftemp`, `fzeff` are replaced by the `abstract_plasma`
    type which provides the `dens`, `temp` and `zeff` methods for
    either `numeric_plasma` or `analytic_plasma` subtypes.
2024-10-07 16:19:33 +02:00
Michele Guerini Rocco
a4ab741341
src/dispersion.f90: remove global variables
The extv and ttv arrays can be computed at compile-time and simply
defined as parameters.
2024-10-07 16:19:32 +02:00
Michele Guerini Rocco
3a10b45595
src/limiter.f90: remove
1. Use the `contour` type for limiter and plasma boundary
   (rlim, zlim, rbnd, zbnd)

2. Replace `inside` with `contour%contains`

3. Replace `range2rect` with a `contour` interface

4. Remove the limiter module which just re-exports the limiter
   as a global; instead just pass the contour object around
2024-10-07 16:19:32 +02:00
Michele Guerini Rocco
a4d44933e2
stop re-exporting gray parameters as globals
This is a first step in removing all the global variables from gray.
2024-10-07 16:19:32 +02:00
Michele Guerini Rocco
ba8fc001e5
fix memory errors and use automatic deallocation 2024-10-07 16:19:30 +02:00
Michele Guerini Rocco
f9c313323a
abstract the outputs units
This change replaces the output files (Fortran units) with a derived
type called table, that hold the data in memory until further
processing. The data stored in a table can be dumped to a file, as
before, or processed in other ways, for example converted to other
derived type.
2024-10-07 16:19:30 +02:00
1bcc4f968b src/gray_params.f90: replace misused findloc function 2024-05-30 18:00:06 +02:00
2b8ec53d70 Improve compatibility with some compilers 2024-05-30 17:40:05 +02:00
Michele Guerini Rocco
baf53b932b
simplify memory management
This change replaces pointers with automatic arrays to greatly simplify
the memory management in the main subroutine:

  - All arrays are defined in a single location and with their final
    dimension explicitely shown.

  - The allocation/deallocation is performed automatically when
    entering/leaving the gray_main routine.
2024-04-29 10:08:16 +02:00
Michele Guerini Rocco
3115e9e9f8
move vendored libraries to their own directory 2024-04-29 10:08:16 +02:00
Michele Guerini Rocco
27141d803c
move minpack license to minpack source 2024-04-22 19:09:30 +02:00
Michele Guerini Rocco
a596b0dff2
add gray-convert tool
This is a command line tool to convert between different GRAY
configuration file formats.
2024-04-22 19:09:29 +02:00
Michele Guerini Rocco
0a87a3ef76
src/polarization.f90: rewrite
- Remove the Stokes parameters as an intermediate step in the
  conversion between Jones vectors and polarisation ellipses.

- Document every single step performed when converting between
  different parametrisations and how the polarisation at the
  plasma boundary is computed. This includes how everything
  was derived from first principles.

- Mark the subroutines as pure.

- Remove `set_pol` entirely.
2024-04-11 21:49:52 +02:00
Michele Guerini Rocco
f82f91bc8d
fix coupling for subsequent beams
In situations when multiple beams are traced, either when allowing
multiple plasma crossings (raytracing.ipass > 0) or the initial polarisation
is mixed (raytracing.ipol == .true.), the couplings of all but the first
beam (with least index_rt) were invalid.

The bug is due to the re-use of the psipol,chipol variables as the beams
are traced sequentially over the beam_loop. For the first beam being
traced the psipol,chipol are correctly initialised to the user-defined
value and the resulting coupling is correct.
However, in each subsequent beam the values were not set to those of the
parent beam (or to the user-defined value in the case of the first X
mode beam), but to those of the previous beams (current index_rt - 1).

This change repurposes the psipv,chipv arrays to store the polarisation
of the parent beams, including the initial user-defined value and makes
plasma_in always use these to compute the coupling.

In addition, in the case the polarisation is not immediately known (i.e.
if raytracing.ipol == .false.), this change postpones the computation of
the Jones vector (ext, eyt) from the launch point, if the magnetic
equilibrium is available, to when the ray actually crosses the
plasma boundary.
The original code, besides being strictly incorrect, can lead to
non-negligible alterations to the coupling. This change also mean:

1. most of the functionality of `set_pol` has been merged with
   `plasma_in`
2. the polarisation is undefined and the Jones vector is set to the
   placeholder value [1, 0] till `plasma_im` is called

Finally, `polarcold` is removed because it's unused.
2024-04-11 18:06:46 +02:00
Michele Guerini Rocco
38a8edd439
doc: reorganize example input files 2024-02-16 00:38:43 +01:00
Michele Guerini Rocco
6a91eaa3a8
src/gray_params.f90: handle missing mandatory parameters 2024-02-09 11:16:21 +01:00
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
19f6d7f2f0
src/dierckx.f90: detect overflow in fpgrre
When a call to regrid results in ier=-1 (data needs to be refitted with
interpolating spline) the computation of the sum of the residuals can
overflow. In this case we can exit immediately (avoiding raising a FPE)
ignoring the results. The latter need to be recomputed anyway.
2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
2d16617db8
src/gray_core.f90: fix possible division by zero
If the EQDSK grid extends all the way to R=0, evaluating B ~ 1/R on a
regular grid from rmnm to rmxm results in a division by zero.
2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
3bc1efc2a6
src/minpack.f90: ignore FPE in user-supplied subs
The minimisation algorithm may try to evaluate a function outside of its
domain triggering a floating point exception. However, this is not a
concern because it won't affect the final result, so they can be ignored
2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
3dcacf685c
src/dierckx.f90: fix use of uninitialised var 2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
7c5b443847
src/equilibrium.f90: use enums 2024-02-09 11:16:19 +01:00
Michele Guerini Rocco
fac0c6ded8
src/main.f90: use enums 2024-02-09 11:16:18 +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
fa89439994
src/ini_parser.f90: handle inline comments and whitespace
While the Fortran read statement will ignore whitespace
and ignored remaining tokens, these are problematic when
manually parsing the INI values.
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