1
0
mirror of https://github.com/TREX-CoE/qmckl.git synced 2024-12-22 12:23:56 +01:00

Removed iso-c-bindings in C. Will be done for Fortran

This commit is contained in:
Anthony Scemama 2020-10-14 09:54:12 +02:00
parent baf48d1987
commit 90714f069b
3 changed files with 113 additions and 69 deletions

View File

@ -6,7 +6,12 @@ FFLAGS=
SOURCE_FILES=$(wildcard *.org)
%.c %_f.f90 %.h: %.c.org
.PHONY: clean
%.c %.h: %.org
emacs --quick --no-init-file --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "$^")'
%.c %.h %_f.f90: %.org
emacs --quick --no-init-file --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "$^")'
%.o: %.c
@ -15,4 +20,7 @@ SOURCE_FILES=$(wildcard *.org)
%.o: %.f90
$(FC) $(FFLAGS) -c $*.f90 -o $*.o
clean:
rm -f qmckl_*.f90 qmckl_*.c qmckl_*.o qmckl_*.h

View File

@ -2,17 +2,50 @@
** Introduction
Source files are written in org-mode format, to provide useful
comments and LaTex formulas close to the code. There exists multiple
possibilities to convert org-mode files into different formats such as
HTML or pdf.
The main objective of present library is documentation. Therefore,
literate programming is particularly adapted in this context.
Source files are written in org-mode format, to provide useful
comments and LaTex formulas close to the code. There exists multiple
possibilities to convert org-mode files into different formats such as
HTML or pdf.
For a tutorial on literate programming with org-mode, follow
[[http://www.howardism.org/Technical/Emacs/literate-programming-tutorial.html][this link]].
The code is extracted from the org files using Emacs in the =Makefile=,
and then the produced files are compiled.
The code is extracted from the org files using Emacs as a command-line
tool in the =Makefile=, and then the produced files are compiled.
For a better experience, editing can be done with Emacs. Note that
Emacs can behave like Vim when switched into ``Evil'' mode.
*** Source code editing
Any text editor can be used to edit org-mode files. For a better
user experience Emacs is recommended.
For users hating Emacs, it is good to know that Emacs can behave
like Vim when switched into ``Evil'' mode. There also exists
[[https://www.spacemacs.org][Spacemacs]] which is particularly well adapted to Vim users.
For users with a preference for Jupyter notebooks, the following
script can convert jupyter notebooks to org-mode files:
#+BEGIN_SRC sh tangle: nb_to_org.sh
#!/bin/bash
# $ nb_to_org.sh notebook.ipynb
# produces the org-mode file notebook.org
set -e
nb=$(basename $1 .ipynb)
jupyter nbconvert --to markdown ${nb}.ipynb --output ${nb}.md
pandoc ${nb}.md -o ${nb}.org
rm ${nb}.md
#+END_SRC
And pandoc can convert multiple markdown formats into org-mode.
*** Writing in Fortran
The Fortran source files should provide a C interface using
iso-c-binding. The name of the Fortran source files should end
with =_f.f90= to be properly handled by the Makefile.
** Documentation
- [[qmckl_context.c.org][Context]]

View File

@ -1,7 +1,9 @@
# -*- mode: org -*-
#+TITLE: Context
This file is written in C because it is more natural to express the context in
C than in Fortran. This file also produces the Fortran binding.
C than in Fortran.
#+BEGIN_SRC C :tangle qmckl_context.c
@ -15,10 +17,12 @@ C than in Fortran. This file also produces the Fortran binding.
is stored in the following data structure, which can't be seen
outside of the library.
#+BEGIN_SRC C :tangle qmckl_context.h
#define QMCKL_DEFAULT_PRECISION 53
#define QMCKL_DEFAULT_RANGE 2
/* 64-bit integer */
typedef long long int qmckl_context ;
#+END_SRC
@ -31,15 +35,17 @@ typedef struct qmckl_context_struct_ {
} qmckl_context_struct;
#+END_SRC
To create a new context, use =qmckl_context_create()=. If the creation
failed, the function returns 0. On success, a pointer to a context
is returned as a long integer.
** =qmckl_context_create=
#+BEGIN_SRC C :tangle qmckl_context.h
To create a new context, use =qmckl_context_create()=. If the creation
failed, the function returns =0=. On success, a pointer to a context
is returned as a 64-bit integer.
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_create();
#+END_SRC
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_create() {
qmckl_context_struct* context;
@ -55,30 +61,15 @@ qmckl_context qmckl_context_create() {
return (qmckl_context) context;
}
#+END_SRC
#+END_SRC
#+BEGIN_SRC fortran :tangle qmckl_context_f.f90
integer(8) function qmckl_context_create()
use, intrinsic :: iso_c_binding
implicit none
** =qmckl_context_copy=
interface
integer(c_long_long) function c_qmckl_context_create() &
bind(C, name='qmckl_context_create')
use, intrinsic :: iso_c_binding, only : c_long_long
end function
end interface
qmckl_context_create = c_qmckl_context_create()
end
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.h
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_copy(qmckl_context context);
#+END_SRC
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_copy(qmckl_context context) {
qmckl_context_struct* old_context;
@ -97,40 +88,24 @@ qmckl_context qmckl_context_copy(qmckl_context context) {
return (qmckl_context) new_context;
}
#+END_SRC
#+BEGIN_SRC fortran :tangle qmckl_context_f.f90
integer(8) function qmckl_context_copy(context)
use, intrinsic :: iso_c_binding
implicit none
integer(8), intent(in) :: context
interface
integer(c_long_long) function c_qmckl_context_copy(context) &
bind(C, name='qmckl_context_copy')
use, intrinsic :: iso_c_binding, only : c_long_long
integer(c_long_long), intent(in) :: context
end function
end interface
qmckl_context_copy = c_qmckl_context_copy(context)
end
#+END_SRC
#+END_SRC
* Precision
The following functions set the expected required precision and range.
=precision= should be an integer between 2 and 53, and =range= should be
an integer between 2 and 11.
These functions return 0 upon success.
The following functions set and get the expected required precision
and range. =precision= should be an integer between 2 and 53, and
=range= should be an integer between 2 and 11.
The setter functions functions return a new context as a 64-bit integer.
The getter functions return the value, as a 32-bit integer.
#+BEGIN_SRC C :tangle qmckl_context.h
** =qmckl_context_set_precision=
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_set_precision(qmckl_context context, int precision);
#+END_SRC
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_set_precision(qmckl_context context, int precision) {
qmckl_context_struct* ctx;
@ -141,14 +116,14 @@ qmckl_context qmckl_context_set_precision(qmckl_context context, int precision)
ctx->precision = precision;
return (qmckl_context) ctx;
}
#+END_SRC
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.h
** =qmckl_context_set_range=
#+BEGIN_SRC C :tangle qmckl_context.h
qmckl_context qmckl_context_set_range(qmckl_context context, int range);
#+END_SRC
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
#+BEGIN_SRC C :tangle qmckl_context.c
qmckl_context qmckl_context_set_range(qmckl_context context, int range) {
qmckl_context_struct* ctx;
@ -159,7 +134,35 @@ qmckl_context qmckl_context_set_range(qmckl_context context, int range) {
ctx->range = range;
return (qmckl_context) ctx;
}
#+END_SRC
#+END_SRC
** =qmckl_context_get_precision=
#+BEGIN_SRC C :tangle qmckl_context.h
int qmckl_context_get_precision(qmckl_context context);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
int qmckl_context_get_precision(qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->precision;
}
#+END_SRC
** =qmckl_context_get_range=
#+BEGIN_SRC C :tangle qmckl_context.h
int qmckl_context_get_range(qmckl_context context);
#+END_SRC
#+BEGIN_SRC C :tangle qmckl_context.c
int qmckl_context_get_range(qmckl_context context) {
qmckl_context_struct* ctx;
ctx = (qmckl_context_struct*) context;
return ctx->range;
}
#+END_SRC