Skip to content

Commit 718ac3a

Browse files
authored
Merge pull request #800 from MilanSkocic/master
Addition of Codata constants
2 parents 3d283a7 + b824cab commit 718ac3a

12 files changed

+2282
-10
lines changed

doc/specs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This is an index/directory of the specifications (specs) for each new module/fea
1414
- [ansi](./stdlib_ansi.html) - Terminal color and style escape sequences
1515
- [array](./stdlib_array.html) - Procedures for index manipulation and array handling
1616
- [ascii](./stdlib_ascii.html) - Procedures for handling ASCII characters
17+
- [constants](./stdlib_constants.html) - Constants
1718
- [bitsets](./stdlib_bitsets.html) - Bitset data types and procedures
1819
- [error](./stdlib_error.html) - Catching and handling errors
1920
- [hash](./stdlib_hash_procedures.html) - Hashing integer

doc/specs/stdlib_constants.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: constants
3+
---
4+
5+
[TOC]
6+
7+
## Introduction
8+
9+
10+
The [[stdlib_constants]] module provides mathematical constants and the most common physical constants.
11+
12+
**Warning**: The names of the most common physical constants are kept short as they are inside a dedicated module.
13+
Nonetheless, in case of overlapping names, they can always be renamed as following:
14+
15+
```fortran
16+
use stdlib_constants, only: clight => c
17+
```
18+
19+
## Codata
20+
21+
The [[stdlib_codata(module)]] module defines all codata (physical) constants as derived
22+
type. The module is automatically generated with a simple
23+
[parser written in Python](https://github.com/MilanSkocic/codata/)
24+
The latest codata constants were released in 2022 by the [NIST](http://physics.nist.gov/constants)
25+
All values for the codata constants are provided as double precision reals.
26+
The names are quite long and can be aliased with shorter names.
27+
28+
The derived type [[stdlib_codata_type(module):codata_constant_type(type)]] defines:
29+
30+
* 4 members:
31+
32+
* `name` (string)
33+
* `value` (double precision real)
34+
* `uncertainty` (double precision real)
35+
* `unit` (string)
36+
37+
* 2 type-bound procedures:
38+
39+
* `print`: to print the values of the constant members;
40+
* `to_real`: to get the value or the uncertainty to the desired precision.
41+
42+
A module level interface [[stdlib_codata_type(module):to_real(interface)]] is
43+
available for getting the constant value or uncertainty of a constant.
44+
45+
## `to_real` - Get the constant value or its uncertainty.
46+
47+
### Status
48+
49+
Experimental
50+
51+
### Description
52+
53+
Convert a [[stdlib_codata_type(module):codata_constant_type(type)]] to a `real` (at least `sp`, or `dp`) scalar.
54+
**Warning**: Some constants cannot be converted to single precision `sp` reals due to the value of the exponents.
55+
56+
### Syntax
57+
58+
`r = ` [[stdlib_codata_type(module):to_real(interface)]] `(c, mold [, uncertainty])`
59+
60+
### Arguments
61+
62+
`c`: argument has `intent(in) ` and shall be of type [[stdlib_codata_type(module):codata_constant_type(type)]].
63+
64+
`mold`: argument has `intent(in)` and shall be of `real` type.
65+
**Note**: The type of the `mold` argument defines the type of the result.
66+
67+
`uncertainty` (optional): argument has `intent(in)` and shall be of `logical` type.
68+
It specifies if the uncertainty needs to be returned instead of the value. Default to `.false.`.
69+
70+
### Return value
71+
72+
Returns a scalar of `real` type which is either the value or the uncertainty of a codata constant.
73+
74+
## Example
75+
76+
```fortran
77+
{!example/constants/example_constants.f90!}
78+
```

example/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endmacro(ADD_EXAMPLE)
99
add_subdirectory(array)
1010
add_subdirectory(ascii)
1111
add_subdirectory(bitsets)
12+
add_subdirectory(constants)
1213
add_subdirectory(error)
1314
add_subdirectory(hashmaps)
1415
add_subdirectory(hash_procedures)

example/constants/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ADD_EXAMPLE(constants)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
program example_constants
2+
use stdlib_constants, only: c, pi=>PI_dp
3+
use stdlib_codata, only: alpha=>ALPHA_PARTICLE_ELECTRON_MASS_RATIO
4+
use stdlib_codata_type, only : to_real
5+
use stdlib_kinds, only: dp, sp
6+
7+
! Use most common physical constants defined as double precision reals
8+
print *, "speed of light in vacuum= ", c
9+
10+
! Use of mathematical constants such as PI
11+
print *, "PI as double precision real= ", pi
12+
13+
! Use codata_constant type for evaluating the value to the desired precision
14+
print *, "Value of alpha... evaluated to double precision=", alpha%to_real(1.0_dp)
15+
print *, "Uncertainty of alpha... evaluated to double precision=", alpha%to_real(1.0_sp, .true.)
16+
print *, "Value of alpha... evaluated to single precision=", alpha%to_real(1.0_sp)
17+
18+
! Convert a codata constant to a real
19+
print *, "Value of the alpha... evaluated to double precision=", to_real(alpha, 1.0_dp)
20+
21+
22+
! Print out codata constant attributes: name, value, uncertainty and unit
23+
call alpha%print()
24+
25+
end program example_constants

src/CMakeLists.txt

+13-10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ set(fppFiles
66
stdlib_bitsets.fypp
77
stdlib_bitsets_64.fypp
88
stdlib_bitsets_large.fypp
9-
stdlib_hash_32bit.fypp
9+
stdlib_codata_type.fypp
10+
stdlib_constants.fypp
11+
stdlib_hash_32bit.fypp
1012
stdlib_hash_32bit_fnv.fypp
11-
stdlib_hash_32bit_nm.fypp
12-
stdlib_hash_32bit_water.fypp
13-
stdlib_hash_64bit.fypp
14-
stdlib_hash_64bit_fnv.fypp
15-
stdlib_hash_64bit_pengy.fypp
16-
stdlib_hash_64bit_spookyv2.fypp
13+
stdlib_hash_32bit_nm.fypp
14+
stdlib_hash_32bit_water.fypp
15+
stdlib_hash_64bit.fypp
16+
stdlib_hash_64bit_fnv.fypp
17+
stdlib_hash_64bit_pengy.fypp
18+
stdlib_hash_64bit_spookyv2.fypp
1719
stdlib_io.fypp
1820
stdlib_io_npy.fypp
1921
stdlib_io_npy_load.fypp
@@ -25,9 +27,9 @@ set(fppFiles
2527
stdlib_linalg_outer_product.fypp
2628
stdlib_linalg_kronecker.fypp
2729
stdlib_linalg_cross_product.fypp
28-
stdlib_linalg_solve.fypp
30+
stdlib_linalg_solve.fypp
2931
stdlib_linalg_determinant.fypp
30-
stdlib_linalg_state.fypp
32+
stdlib_linalg_state.fypp
3133
stdlib_optval.fypp
3234
stdlib_selection.fypp
3335
stdlib_sorting.fypp
@@ -68,7 +70,7 @@ set(fppFiles
6870
stdlib_version.fypp
6971
)
7072

71-
# Preprocessed files to contain preprocessor directives -> .F90
73+
# Preprocessed files to contain preprocessor directives -> .F90
7274
set(cppFiles
7375
stdlib_linalg_constants.fypp
7476
stdlib_linalg_blas.fypp
@@ -97,6 +99,7 @@ set(SRC
9799
stdlib_ansi_operator.f90
98100
stdlib_ansi_to_string.f90
99101
stdlib_array.f90
102+
stdlib_codata.f90
100103
stdlib_error.f90
101104
stdlib_hashmap_wrappers.f90
102105
stdlib_hashmaps.f90

0 commit comments

Comments
 (0)