Skip to content

Commit

Permalink
Rollback for C API in Fortran.
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanSkocic committed Dec 10, 2024
1 parent d8388b7 commit 5ec533d
Show file tree
Hide file tree
Showing 28 changed files with 26,538 additions and 78 deletions.
33 changes: 30 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,29 @@ else
btype=release
endif

PY=python
ifeq ($(PLATFORM), windows)
PY=py
endif
ifeq ($(PLATFORM), linux)
PY=python
AW=auditwheel repair --plat manylinux_2_31_x86_64 ./dist/*.whl
endif
ifeq ($(PLATFORM), darwin)
PY=python
endif


GEN_F=./scripts/gen_fortran.py
GEN_C=./scripts/gen_capi.py
GEN_HEADERS=./scripts/gen_headers.py
GEN_HEADER=./scripts/gen_header.py
GEN_STDLIB=./scripts/gen_stdlib.py

AST_SRC=$(wildcard ./data/*.toml)
F_SRC=$(patsubst ./data/%.toml, ./src/%.f90, $(AST_SRC))
C_SRC=$(patsubst ./data/%.toml, ./src/capi_%.f90, $(AST_SRC))
C_HEADERS=$(patsubst ./data/%.toml, ./include/%.h, $(AST_SRC))
C_HEADER=./include/$(NAME).h
SRC_FYPP=$(wildcard ./src/*.fypp)
SRC_FYPP_F90=$(patsubst ./src/%.fypp, ./src/%.f90, $(SRC_FYPP))
STDLIB=./stdlib/stdlib_codata.f90
Expand All @@ -36,11 +53,20 @@ $(LIBNAME): sources build copy_a shared

# ---------------------------------------------------------------------
# SOURCES
sources: $(SRC_FYPP_F90) $(F_SRC) $(STDLIB)
sources: $(SRC_FYPP_F90) $(F_SRC) $(C_SRC) $(C_HEADER) $(STDLIB)

./src/%.f90: ./data/%.toml
$(PY) $(GEN_F) $< $@

./src/capi_%.f90: ./data/%.toml
$(PY) $(GEN_C) $< $@

./include/%.h: ./data/%.toml
$(PY) $(GEN_HEADERS) $< $@

$(C_HEADER): $(C_HEADERS)
$(PY) $(GEN_HEADER) $^ -o $@

./src/%.f90: ./src/%.fypp
fypp -I ./include $< $@

Expand All @@ -59,6 +85,7 @@ test: build

example: build
fpm run --profile=$(btype) --example example_in_f
fpm run --profile=$(btype) --example example_in_c
# ---------------------------------------------------------------------


Expand Down Expand Up @@ -127,7 +154,7 @@ logo:
make -C media

clean:
rm -rf $(F_SRC) ./src/codata_version.f90 $(SRC_FYPP_F90) $(STDLIB)
rm -rf $(F_SRC) $(C_SRC) $(C_HEADERS) $(C_HEADER) ./src/codata_version.f90 $(SRC_FYPP_F90) $(STDLIB)
fpm clean --all
rm -rf API-doc/*
# ---------------------------------------------------------------------
66 changes: 52 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

`codata` is a Fortran library providing the latest codata constants (2022) and
older values (2018, 2014 and 2010).
The constants (values and uncertainties) are implemented as double precision reals
in derived types.
The sources are automatically generated from the raw codata values taken
from http://physics.nist.gov/constants.
The raw codata are taken from http://physics.nist.gov/constants.
The constants (values and uncertainties) are implemented as double precision reals.

The names are quite long and can be aliased with shorter names.

Expand All @@ -18,23 +16,16 @@ add the following to your `fpm.toml` file:
codata = { git="https://github.com/MilanSkocic/codata.git" }
```

**Important**:
**Notes**:

* The latest codata constants were integrated in the [stdlib](https://github.com/fortran-lang/stdlib/releases/tag/v0.7.0).
The constants are implemented as derived type which carries the name, the value, the uncertainty and the unit.
This library will be complementary to the constants defined in the stdlib by providing older values for the constants.
* The latest codata constants were integrated in the [stdlib](https://github.com/fortran-lang/stdlib/releases/tag/v0.7.0). The constants are implemented as derived type which carries the name, the value, the uncertainty and the unit. This library will be complementary to the constants defined in the stdlib by providing older values for the constants.

* If you only need sources for the codata constants that you can integrate directly in your sources you may be interested by https://github.com/vmagnin/fundamental_constants.

**Extra: C and Python**

`codata` is primarily a Fortran library but sources for C and Python are also
generated:
**Extra:**

* Pure python code is provided in the `py` folder. See `py/README.md`.
A python package is available [pypi](https://pypi.org/project/pycodata).
* Pure C code is provided in the `C` folder. See `C/README.md`
A Makefile is provided for building the C library.


# Dependencies
Expand Down Expand Up @@ -100,3 +91,50 @@ print '(A, F23.16)', "Mu_2010 = ", MOLAR_MASS_CONSTANT_2010%value
end program
```
```C
#include <stdio.h>
#include "codata.h"

int main(void){

printf("########## EXAMPLE IN C ##########n");

printf("%sn","########## VERSION ##########");
printf("version = %sn", version);

printf("%sn","########## CONSTANTS ##########");
printf("c = %fn", SPEED_OF_LIGHT_IN_VACUUM.value);

printf("%sn","########## UNCERTAINTY ##########");
printf("u(c) = %fn", SPEED_OF_LIGHT_IN_VACUUM.uncertainty);

printf("%sn","########## OLDER VALUES ##########");
printf("Mu_2022(latest) = %23.16fn", MOLAR_MASS_CONSTANT.value);
printf("Mu_2018 = %23.16fn", MOLAR_MASS_CONSTANT_2018.value);
printf("Mu_2014 = %23.16fn", MOLAR_MASS_CONSTANT_2014.value);
printf("Mu_2010 = %23.16fn", MOLAR_MASS_CONSTANT_2010.value);

return 0;
}
```
```Python
r"""Example in python."""
import sys
sys.path.insert(0, "../src/")
import pycodata
print("########## VERSION ##########")
print(f"version = {pycodata.__version__}")
print("########## constants ##########")
print(f"c =", pycodata.SPEED_OF_LIGHT_IN_VACUUM["value"])
print("########## UNCERTAINTY ##########")
print(f"u(c) = ", pycodata.SPEED_OF_LIGHT_IN_VACUUM["uncertainty"])
print("########## OLDER VALUES ##########")
print(f"Mu_2022 = ", pycodata.MOLAR_MASS_CONSTANT["value"])
print(f"Mu_2018 = ", pycodata.codata_constants_2018.MOLAR_MASS_CONSTANT_2018["value"])
print(f"Mu_2014 = ", pycodata.codata_constants_2014.MOLAR_MASS_CONSTANT_2014["value"])
print(f"Mu_2010 = ", pycodata.codata_constants_2010.MOLAR_MASS_CONSTANT_2010["value"])
```
54 changes: 24 additions & 30 deletions configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ function prepare_readme () {

local fpath_readme_tpl="$1"
local fpath_readme="$2"
local fpath_example="$3"
local lang="$4"
local fpath_example_in_f="$3"
local fpath_example_in_c="$4"
local fpath_example_in_py="$5"

echo "" > $fpath_readme
while read line
Expand All @@ -15,19 +16,32 @@ function prepare_readme () {
set +f
done < $fpath_readme_tpl

echo "\`\`\`$lang" >> $fpath_readme
echo "\`\`\`Fortran" >> $fpath_readme
while read line
do
echo $line >> $fpath_readme
done < $fpath_example
done < $fpath_example_in_f
echo "\`\`\`" >> $fpath_readme

echo "\`\`\`C" >> $fpath_readme
while read line
do
echo $line >> $fpath_readme
done < $fpath_example_in_c
echo "\`\`\`" >> $fpath_readme

echo "\`\`\`Python" >> $fpath_readme
while read line
do
echo $line >> $fpath_readme
done < $fpath_example_in_py
echo "\`\`\`" >> $fpath_readme
}

LIBNAME="libcodata"
NAME="codata"
PYNAME="py$NAME"
PY_SRC="./src/$PYNAME"
C_SRC="./C/src/"

# environment variables
FC=gfortran
Expand All @@ -37,8 +51,6 @@ INCLUDE_DIR="./include"
FPM_FFLAGS="-std=f2008 -pedantic -Wall -Wextra"
FPM_CFLAGS="-std=c11 -pedantic -Wall -Wextra"
FPM_LDFLAGS=""
CFLAGS_RELEASE="-O3 -funroll-loops -fmax-errors=1"
CFLAGS_DEBUG="-g -fmax-errors=1"
DEFAULT_INSTALL_DIR="$HOME/.local"
PLATFORM="linux"
EXT=".so"
Expand Down Expand Up @@ -68,8 +80,6 @@ export PLATFORM
export FPM_FFLAGS
export FPM_CFLAGS
export FPM_LDFLAGS
export CFLAGS_DEBUG
export CFLAGS_RELEASE
export DEFAULT_INSTALL_DIR
export BUILD_DIR
export INCLUDE_DIR
Expand All @@ -89,8 +99,6 @@ echo "##### FPM SETTINGS #####"
echo "* FPM_FFLAGS=" $FPM_FFLAGS
echo "* FPM_CFLAGS=" $FPM_CFLAGS
echo "* FPM_LDFLAGS=" $FPM_LDFLAGS
echo "* CFLAGS_DEBUG=" $CFLAGS_DEBUG
echo "* CFLAGS_RELEASE=" $CFLAGS_RELEASE

echo "##### INSTALLATION SETTINGS #####"
echo "* DEFAULT INSTALL DIR=" $DEFAULT_INSTALL_DIR
Expand All @@ -115,26 +123,12 @@ cp -f VERSION ./py/VERSION
cp -f LICENSE ./py/LICENSE
echo "OK"

echo -n "Generating README for fortran..."
fpath_example="./example/example.f90"
echo -n "Generating examples in README..."
fpath_readme_tpl="./README_TEMPLATE.txt"
fpath_readme="./README.md"
lang="Fortran"
prepare_readme $fpath_readme_tpl $fpath_readme $fpath_example $lang
fpath_example_in_f="./example/example.f90"
fpath_example_in_c="./example/example.c"
fpath_example_in_py="./example/example.py"
prepare_readme $fpath_readme_tpl $fpath_readme $fpath_example_in_f $fpath_example_in_c $fpath_example_in_py
echo "OK"

echo -n "Generating README for C..."
fpath_example="./C/example/example.c"
fpath_readme_tpl="./C/README_TEMPLATE.txt"
fpath_readme="./C/README.md"
lang="C"
prepare_readme $fpath_readme_tpl $fpath_readme $fpath_example $lang
echo "OK"

echo -n "Generating README for python..."
fpath_example="./py/example/example.py"
fpath_readme_tpl="./py/README_TEMPLATE.txt"
fpath_readme="./py/README.md"
lang="python"
prepare_readme $fpath_readme_tpl $fpath_readme $fpath_example $lang
echo "OK"
24 changes: 24 additions & 0 deletions example/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include "codata.h"

int main(void){

printf("########## EXAMPLE IN C ##########\n");

printf("%s\n","########## VERSION ##########");
printf("version = %s\n", codata_get_version());

printf("%s\n","########## CONSTANTS ##########");
printf("c = %f\n", SPEED_OF_LIGHT_IN_VACUUM.value);

printf("%s\n","########## UNCERTAINTY ##########");
printf("u(c) = %f\n", SPEED_OF_LIGHT_IN_VACUUM.uncertainty);

printf("%s\n","########## OLDER VALUES ##########");
printf("Mu_2022(latest) = %23.16f\n", MOLAR_MASS_CONSTANT.value);
printf("Mu_2018 = %23.16f\n", MOLAR_MASS_CONSTANT_2018.value);
printf("Mu_2014 = %23.16f\n", MOLAR_MASS_CONSTANT_2014.value);
printf("Mu_2010 = %23.16f\n", MOLAR_MASS_CONSTANT_2010.value);

return 0;
}
2 changes: 1 addition & 1 deletion example/example.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ program example_in_f
print '(A)', '########## EXAMPLE IN FORTRAN ##########'

print '(A)', '########## VERSION ##########'
print *, "version = ", version
print *, "version = ", get_version()

print '(A)', '########## CONSTANTS ##########'
print *, "c = ", SPEED_OF_LIGHT_IN_VACUUM%value
Expand Down
19 changes: 19 additions & 0 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
r"""Example in python."""
import sys
sys.path.insert(0, "../src/")
import pycodata

print("########## VERSION ##########")
print(f"version = {pycodata.__version__}")

print("########## constants ##########")
print(f"c =", pycodata.SPEED_OF_LIGHT_IN_VACUUM["value"])

print("########## UNCERTAINTY ##########")
print(f"u(c) = ", pycodata.SPEED_OF_LIGHT_IN_VACUUM["uncertainty"])

print("########## OLDER VALUES ##########")
print(f"Mu_2022 = ", pycodata.MOLAR_MASS_CONSTANT["value"])
print(f"Mu_2018 = ", pycodata.codata_constants_2018.MOLAR_MASS_CONSTANT_2018["value"])
print(f"Mu_2014 = ", pycodata.codata_constants_2014.MOLAR_MASS_CONSTANT_2014["value"])
print(f"Mu_2010 = ", pycodata.codata_constants_2010.MOLAR_MASS_CONSTANT_2010["value"])
4 changes: 4 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ stdlib="*"
name = "example_in_f"
main = "example.f90"

[[example]]
name = "example_in_c"
main = "example.c"

[[test]]
name = "tester"
main = "test_constants.f90"
Expand Down
Loading

0 comments on commit 5ec533d

Please sign in to comment.