Skip to content

Commit

Permalink
Merge pull request #31 from HaoZeke/fixupHeaders
Browse files Browse the repository at this point in the history
ENH: Fixup headers, add single file exporter
  • Loading branch information
HaoZeke authored Aug 28, 2023
2 parents 14d93dd + 3a2d74a commit 2a265ce
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Added
/subprojects/*
!/subprojects/*.wrap
dist/

# Created by https://www.toptal.com/developers/gitignore/api/fortran
# Edit at https://www.toptal.com/developers/gitignore?templates=fortran
Expand Down
18 changes: 18 additions & 0 deletions interfaces/CInterface/GaussJacobiQuadCInterp.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
// BEGIN_HEADER
// -----------------------------------------------------------------------------
// Gauss-Jacobi Quadrature Implementation
// Authors: Rohit Goswami <rgoswami[at]ieee.org>
// Source: GaussJacobiQuad Library
// License: MIT
// GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
// Date: 2023-08-28
// Commit: c442f77
// -----------------------------------------------------------------------------
// This code is part of the GaussJacobiQuad library, providing an efficient
// implementation for Gauss-Jacobi quadrature nodes and weights computation.
// -----------------------------------------------------------------------------
// To cite this software:
// Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
// Zenodo: https://doi.org/10.5281/ZENODO.8285112
// ---------------------------------------------------------------------
// END_HEADER
#include "GaussJacobiQuadCInterp.h"

void gauss_jacobi_c(int* npts, double* alpha, double* beta, double x[], double wts[], const char* method) {
Expand Down
8 changes: 6 additions & 2 deletions interfaces/CInterface/GaussJacobiQuadCInterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
// Source: GaussJacobiQuad Library
// License: MIT
// GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
// Date: 2023-08-26
// Commit: 5dd0ffe
// Date: 2023-08-28
// Commit: c442f77
// -----------------------------------------------------------------------------
// This code is part of the GaussJacobiQuad library, providing an efficient
// implementation for Gauss-Jacobi quadrature nodes and weights computation.
// -----------------------------------------------------------------------------
// To cite this software:
// Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
// Zenodo: https://doi.org/10.5281/ZENODO.8285112
// ---------------------------------------------------------------------
// END_HEADER
#ifndef GAUSSJACOBIQUADCINTERP_H
#define GAUSSJACOBIQUADCINTERP_H
Expand Down
8 changes: 6 additions & 2 deletions interfaces/CInterface/c_cli_gjpq.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
// Source: GaussJacobiQuad Library
// License: MIT
// GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
// Date: 2023-08-26
// Commit: 5dd0ffe
// Date: 2023-08-28
// Commit: c442f77
// -----------------------------------------------------------------------------
// This code is part of the GaussJacobiQuad library, providing an efficient
// implementation for Gauss-Jacobi quadrature nodes and weights computation.
// -----------------------------------------------------------------------------
// To cite this software:
// Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
// Zenodo: https://doi.org/10.5281/ZENODO.8285112
// ---------------------------------------------------------------------
// END_HEADER
#include <stdio.h>
#include <stdlib.h>
Expand Down
8 changes: 6 additions & 2 deletions interfaces/PyInterface/gjquad_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
# Source: GaussJacobiQuad Library
# License: MIT
# GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
# Date: 2023-08-26
# Commit: 5dd0ffe
# Date: 2023-08-28
# Commit: c442f77
# -----------------------------------------------------------------------------
# This code is part of the GaussJacobiQuad library, providing an efficient
# implementation for Gauss-Jacobi quadrature nodes and weights computation.
# -----------------------------------------------------------------------------
# To cite this software:
# Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
# Zenodo: https://doi.org/10.5281/ZENODO.8285112
# ---------------------------------------------------------------------
# END_HEADER
"""!
@brief This script computes Gauss-Jacobi quadrature roots and weights using
Expand Down
1 change: 1 addition & 0 deletions newsfragments/10.explib.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a single file exporter for external libraries, which concatenates required files into one and updates the headers accordingly.
1 change: 0 additions & 1 deletion newsfragments/7.algo665.feature.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

Added a variant of algorithm 665, refactored into gjp_common to
accentuate similarities between 665 and standard GW
11 changes: 11 additions & 0 deletions readme.org
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ meson compile -C bbdir
./bbdir/gjp_quad <npoints> <alpha> <beta> <method>
#+end_src

*** External Fortran libaries
We support and encourage users to generate single file versions of the algorithms here to include in their code bases. This can be done with the ~scripts/add_headers.py~ script:
#+begin_src bash
# Strips comments by default
python scripts/export_single.py --modulename "gjp_algo665"
python scripts/export_single.py --modulename "gjp_gw" --keep-comments
#+end_src
These can be dropped into any code base or compiled as is into a shared library.
#+begin_src bash
gfortran dist/gjp_algo665_single.f90 --shared -fPIC -o libgjp_algo665.so
#+end_src
*** Interfaces
*** C/C++ header interface
We provide a header only interface, which bypasses passing strings between
Expand Down
34 changes: 19 additions & 15 deletions scripts/add_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
{{cchar}} This code is part of the {{library_name}} library, providing an efficient
{{cchar}} implementation for Gauss-Jacobi quadrature nodes and weights computation.
{{cchar}} -----------------------------------------------------------------------------
{{cchar}} To cite this software:
{{cchar}} Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
{{cchar}} Zenodo: https://doi.org/10.5281/ZENODO.8285112
{{cchar}} ---------------------------------------------------------------------
{{cchar}} END_HEADER
"""
Expand Down Expand Up @@ -83,19 +87,19 @@ def add_headers(
print("Headers added or updated in all files in" f" {directory} using {cchar}.")


# Command-line arguments
parser = argparse.ArgumentParser(description="Add headers to source files.")
parser.add_argument(
"--dirs", type=str, required=True, nargs="+", help="Directory(s) to process"
)
parser.add_argument(
"--ftypes",
type=str,
required=True,
help="Comma-separated list of file types to process",
)
parser.add_argument("--cchar", type=str, required=True, help="Comment character")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Add headers to source files.")
parser.add_argument(
"--dirs", type=str, required=True, nargs="+", help="Directory(s) to process"
)
parser.add_argument(
"--ftypes",
type=str,
required=True,
help="Comma-separated list of file types to process",
)
parser.add_argument("--cchar", type=str, required=True, help="Comment character")

args = parser.parse_args()
file_types = args.ftypes.split(",")
add_headers(args.dirs, file_types, args.cchar)
args = parser.parse_args()
file_types = args.ftypes.split(",")
add_headers(args.dirs, file_types, args.cchar)
83 changes: 83 additions & 0 deletions scripts/export_single.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3
# Cannot use ! in statements with this if strip-comments is true

import argparse
import os
import re
import subprocess
from pathlib import Path

import add_headers
import fortdepend


def get_git_root():
try:
git_root = (
subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
.strip()
.decode("utf-8")
)
return Path(git_root)
except subprocess.CalledProcessError:
print("This directory is not part of a Git repository.")
return None


def process_files(fname, strip_comments=True):
with open(fname, "r") as f:
content = f.read()
# Remove everything between ! BEGIN_HEADER and ! END_HEADER
content = re.sub(r"! BEGIN_HEADER.*?! END_HEADER\n", "", content, flags=re.DOTALL)
# Optionally, remove lines starting with !
if strip_comments:
content = re.sub(r"(!.*?$)|(\s*!.*?$)", "", content, flags=re.MULTILINE)
return content


def export_single(modulename, strip_comments=True, outname=None):
if outname is None:
outname = f"{modulename}_single.f90"

prj_srcs = get_git_root() / "src"
os.chdir(prj_srcs) # Needed for fortdepend

gaussjacobiquad = fortdepend.FortranProject()
concat_files = gaussjacobiquad.get_all_used_files(modulename)
print(f"Concatenating in reverse order {concat_files}")

exp_dat = []

for fname in reversed(concat_files):
processed_content = process_files(prj_srcs / fname, strip_comments)
exp_dat.append(processed_content)

with Path(get_git_root() / "dist" / outname).open("w") as f_out:
f_out.write("".join(exp_dat))


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Export single file algorithms.")
parser.add_argument(
"--modulename", type=str, required=True, help="Module to export"
)
parser.add_argument(
"--outname", type=str, required=False, help="Defaults to modulename_single.f90"
)
parser.add_argument(
"--strip-comments",
dest="strip_comments",
action="store_true",
help="Remove comments",
)
parser.add_argument(
"--keep-comments",
dest="strip_comments",
action="store_false",
help="Keep comments",
)
parser.set_defaults(strip_comments=True)

args = parser.parse_args()
export_single(args.modulename, args.strip_comments, args.outname)
add_headers.add_headers([get_git_root() / "dist"], ["f90"])
8 changes: 6 additions & 2 deletions scripts/scipy_gauss_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
# Source: GaussJacobiQuad Library
# License: MIT
# GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
# Date: 2023-08-26
# Commit: 5dd0ffe
# Date: 2023-08-28
# Commit: c442f77
# -----------------------------------------------------------------------------
# This code is part of the GaussJacobiQuad library, providing an efficient
# implementation for Gauss-Jacobi quadrature nodes and weights computation.
# -----------------------------------------------------------------------------
# To cite this software:
# Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
# Zenodo: https://doi.org/10.5281/ZENODO.8285112
# ---------------------------------------------------------------------
# END_HEADER
"""!
@brief This script computes Gauss-Jacobi quadrature roots and weights using SymPy.
Expand Down
8 changes: 6 additions & 2 deletions scripts/sympy_gauss_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
# Source: GaussJacobiQuad Library
# License: MIT
# GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
# Date: 2023-08-26
# Commit: 5dd0ffe
# Date: 2023-08-28
# Commit: c442f77
# -----------------------------------------------------------------------------
# This code is part of the GaussJacobiQuad library, providing an efficient
# implementation for Gauss-Jacobi quadrature nodes and weights computation.
# -----------------------------------------------------------------------------
# To cite this software:
# Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
# Zenodo: https://doi.org/10.5281/ZENODO.8285112
# ---------------------------------------------------------------------
# END_HEADER
"""!
@brief This script computes Gauss-Jacobi quadrature roots and weights using SciPy.
Expand Down
8 changes: 6 additions & 2 deletions src/GaussJacobiQuad.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief Overall driver for Gauss-Jacobi quadrature.
!
Expand Down Expand Up @@ -76,4 +80,4 @@ subroutine gauss_jacobi(npts, alpha, beta, x, wts, method)
end select
end subroutine gauss_jacobi

end module
end module GaussJacobiQuad
6 changes: 5 additions & 1 deletion src/GaussJacobiQuadCCompat.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief Compatibility layer for Gauss-Jacobi quadrature with C bindings.
!
Expand Down
6 changes: 5 additions & 1 deletion src/gjp_algo665.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief This module provides routines for numerical integration using Gauss-Jacobi quadrature.
!! @details The implementation is based on the GW method as implemented in the GaussJacobiQuad
Expand Down
6 changes: 5 additions & 1 deletion src/gjp_common.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief This module provides utility functions for computing Jacobi matrices and zeroth moments.
!!
Expand Down
8 changes: 6 additions & 2 deletions src/gjp_constants.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief Constants contain more digits than double precision, so that
!> they are rounded correctly.
Expand Down Expand Up @@ -41,4 +45,4 @@ module gjp_constants
real(dp), parameter :: c_2002 = 137.03599911_dp
real(dp), parameter :: c_1998 = 137.03599976_dp
real(dp), parameter :: c_1986 = 137.0359895_dp ! To compare with dftatom
end module
end module gjp_constants
6 changes: 5 additions & 1 deletion src/gjp_gw.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
! License: MIT
! GitHub Repository: https://github.com/HaoZeke/GaussJacobiQuad
! Date: 2023-08-28
! Commit: 69e8946
! Commit: c442f77
! -----------------------------------------------------------------------------
! This code is part of the GaussJacobiQuad library, providing an efficient
! implementation for Gauss-Jacobi quadrature nodes and weights computation.
! -----------------------------------------------------------------------------
! To cite this software:
! Rohit Goswami (2023). HaoZeke/GaussJacobiQuad: v0.1.0.
! Zenodo: https://doi.org/10.5281/ZENODO.8285112
! ---------------------------------------------------------------------
! END_HEADER
!> @brief Module for computing Gauss-Jacobi quadrature nodes and weights using the Golub-Welsch (GW) method
!> @details The implementation is based on the Golub-Welsch method as used in chebfun (https://chebfun.org) and references:
Expand Down
Loading

0 comments on commit 2a265ce

Please sign in to comment.