GithubHelp home page GithubHelp logo

skphy / fundamental_constants Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vmagnin/fundamental_constants

0.0 0.0 0.0 94 KB

Fortran modules with the CODATA fundamental physical constants, generated by a Python script.

License: MIT License

Shell 0.18% Python 3.87% Fortran 95.94%

fundamental_constants's Introduction

CODATA fundamental physical constants

Fortran modules with the CODATA fundamental physical constants, generated by a Python script from the NIST website.

The nist.py Python script downloads the CODATA recommended values (latest by default), available as an ASCII file on the NIST website:

https://physics.nist.gov/cuu/Constants/Table/allascii.txt

and generates a Fortran module CODATA_constants in the file src/CODATA_constants.f90. The 2018 CODATA set contains 354 constants. The next set should be published in 2022.

It also generates a Fortran program test/test.f90 which uses that module to generate a text file reproducing the two first columns, with (nearly) the same formatting as in the CODATA NIST file.

Ready to use validated Fortran modules

In the src/ directory, you will find for each recent CODATA sets:

  • the corresponding validated CODATA_????_constants.f90 module,
  • the fortran_generated_????.txt file generated using this module. A graphical diff tool was used to verify that the numbers printed in that file are identical to numbers in the NIST file, which validates the Fortran module,
  • the CODATA_latest_constants.f90 module, if you are only interested by the latest set.

If you use the Fortran Package Manager fpm, you can add in the "dependencies" section of your fpm.toml file:

fundamental_constants = { git="https://github.com/vmagnin/fundamental_constants.git" }

or you can simply copy/paste the .f90 files you need in your project.

Using the Fortran modules

The constant names in the modules are quite long:

module CODATA_2018_constants
  use, intrinsic :: iso_fortran_env, only: wp=>real64
  implicit none

  real(wp), parameter :: alpha_particle_electron_mass_ratio = 7294.29954142_wp ! 0.000 000 24  
  real(wp), parameter :: alpha_particle_mass = 6.6446573357e-27_wp ! 0.000 000 0020 e-27  kg
  real(wp), parameter :: alpha_particle_mass_energy_equivalent = 5.9719201914e-10_wp ! 0.000 000 0018 e-10  J
...

You should rename them upon import like this:

use CODATA_latest_constants, only: c=>speed_of_light_in_vacuum

You can create your own short module with a use CODATA_latest_constants statement and put into it your renamed favourite constants, accordingly to what you generally need in your scientific field.

Note also that the names of some constants may vary each time CODATA releases new recommended values.

Why several modules?

Most of the time you will use the latest values. But if you need to reproduce exactly the results obtained several years ago, you may need to use the corresponding CODATA set. It can be also useful if your project contains tests that expect a very precise numerical result.

Generating a new Fortran module

Default options

From the project root directory, type:

$ ./nist.py
Generating the src/CODATA_constants.f90 file using the recommended 2018 values:
https://physics.nist.gov/cuu/Constants/Table/allascii.txt
354 constants written
34423 bytes

Options of the Python script

$ ./nist.py --help
usage: nist.py [-h] [-y {2010,2014,2018}] [-d] [-v]

Downloads CODATA fundamental physical constants from NIST website and generates a Fortran module.

optional arguments:
  -h, --help           show this help message and exit
  -y {2010,2014,2018}  CODATA values: 2010, 2014 or 2018
  -d                   Delete calculated values (...)
  -v                   Version

Sources : <https://github.com/vmagnin/fundamental_constants>

By default latest values are downloaded. But you can force the year with the -y option.

By default, calculated values are kept. But you can delete them with the -d option if you prefer to calculate them in your program, in order to have more decimals.

Validating the values in the Fortran module

If you have the fpm Fortran Package Manager, build and launch the test:

$ fpm test
[...]
 Do you know this constant with so many zeros?
0100000110110001110111100111100001001010000000000000000000000000
 Oh sorry, you\'re that kind of biped with 10 digits?
   299792458.00000000     
 Some other constants:
   6.0221407599999999E+023
   1.3806490000000001E-023
   1.6021766339999999E-019
 4.500 3878060E-10        
 2808.92113298            
 1234.123 456 78          
 Writing the fortran_generated_????.txt test file

A graphical diff tool, like meld, can be used to compare the fortran_generated_????.txt file with the NIST file, in order to validate the Fortran module. Most constants are written exactly as in the NIST file. A few constants are written differently because the NIST file don't use the classical scientific format: for example, the molar mass of carbon-12 is written 1.199 999 999 58 e-2 in our file but 11.99 999 999 58 e-3 in the NIST file. But it is quick to verify that those values are correct. Some of the values in the NIST file can be calculated from other constants and are terminated by "..." in the NIST file: note that their values in the Fortran module are not calculated, but have the same number of digits as in the NIST file.

You can use alternatively the build.sh script to build the build/runTests executable.

References

fundamental_constants's People

Contributors

vmagnin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.