From 4e3e17f84b451ff6800f783adc25b2707dc71404 Mon Sep 17 00:00:00 2001 From: Joey Dumont Date: Wed, 16 Jul 2014 14:46:09 -0400 Subject: [PATCH] Improved formatting of CMakeLists and of source files. Temporary hack regarding issue #1. --- CMakeLists.txt | 5 ++- include/wignerSymbols.h | 1 + include/wignerSymbols/commonFunctions.h | 31 ++++++++++++++- src/wignerSymbols-cpp.cpp | 29 ++++++++++++++ src/wignerSymbols-fortran.cpp | 2 +- tests/gh-issue-1.cpp | 52 +++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 tests/gh-issue-1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d5ae930..49f15da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,10 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) aux_source_directory(./src SRC_LIST) # Build a shared library -add_library(${PROJECT_NAME} SHARED ${SRC_LIST} "./src/wignerSymbols-fortran.f" "./src/wignerSymbols-fortran-c-binding.f90" ) +add_library(${PROJECT_NAME} SHARED + ${SRC_LIST} + "./src/wignerSymbols-fortran.f" + "./src/wignerSymbols-fortran-c-binding.f90" ) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES diff --git a/include/wignerSymbols.h b/include/wignerSymbols.h index ec34fa8..75e4b0a 100644 --- a/include/wignerSymbols.h +++ b/include/wignerSymbols.h @@ -10,5 +10,6 @@ #include "wignerSymbols/wignerSymbols-cpp.h" #include "wignerSymbols/wignerSymbols-fortran.h" +#include "wignerSymbols/commonFunctions.h" #endif // WIGNER_SYMBOLS_H diff --git a/include/wignerSymbols/commonFunctions.h b/include/wignerSymbols/commonFunctions.h index bb32578..07e90ca 100644 --- a/include/wignerSymbols/commonFunctions.h +++ b/include/wignerSymbols/commonFunctions.h @@ -19,7 +19,34 @@ */ namespace WignerSymbols { -template double sgn(T val) { +template +double sgn(T val) +{ return (T(0) < val) - (val < T(0)); } -} \ No newline at end of file + +template +int maxThree(T a, T b, T c) +{ + int maxIndex; + T max; + if (a > b) + { + max = a; + maxIndex = 1; + } + else + { + max = b; + maxIndex = 2; + } + + if (max < c) + { + max = c; + maxIndex = 3; + } + + return maxIndex; +} +} diff --git a/src/wignerSymbols-cpp.cpp b/src/wignerSymbols-cpp.cpp index e0b2abe..fc949e6 100644 --- a/src/wignerSymbols-cpp.cpp +++ b/src/wignerSymbols-cpp.cpp @@ -210,6 +210,35 @@ double wigner3j(double l1, double l2, double l3, if (!select) return 0.0; + // Temp fix of gh-issue-1. + // We permute the indices such that the largest of the + // three is in first position. + switch (maxThree(l1, l2, l3)) + { + case 1: + { + break; + } + + case 2: + { + std::swap(l1, l2); + std::swap(l2, l3); + std::swap(m1, m2); + std::swap(m2, m3); + break; + } + + case 3: + { + std::swap(l1, l3); + std::swap(l2, l3); + std::swap(m1, m3); + std::swap(m2, m3); + break; + } + } + // We compute l1min and the position of the array we will want. double l1min = std::max(std::fabs(l2-l3),std::fabs(m1)); diff --git a/src/wignerSymbols-fortran.cpp b/src/wignerSymbols-fortran.cpp index e08957b..5aae75a 100644 --- a/src/wignerSymbols-fortran.cpp +++ b/src/wignerSymbols-fortran.cpp @@ -83,4 +83,4 @@ double wigner6j_f(double l1, double l2, double l3, int index = (int)l1-l1min; return sixcof[index]; } -} \ No newline at end of file +} diff --git a/tests/gh-issue-1.cpp b/tests/gh-issue-1.cpp new file mode 100644 index 0000000..97ab970 --- /dev/null +++ b/tests/gh-issue-1.cpp @@ -0,0 +1,52 @@ +/*******************************************************-/ + * This source code is subject to the terms of the GNU -/ + * Lesser Public License. If a copy of the LGPL was not -/ + * distributed with this file, you can obtain one at -/ + * https://www.gnu.org/licenses/lgpl.html -/ + ********************************************************/ + +/*! \file gh-issue-1.cpp + * \author Joey Dumont + * \since 2014-07-11 + * \brief Tests Issue 1 of WignerSymbols (see valandil.github.com/wignerSymbols. + * \copyright LGPL + * This file tests the bug described in Issue #1 of WignerSymbols (GitHub). + * It seems that for some values of l1, the algorithm returns a set + * of zeros even though it should return proper coefficients. + * Permuting the l's lead to the proper output. + */ + +#include + +int main (int argc, char* argv[]) +{ + double test11 = WignerSymbols::wigner3j(325, 999, 1221, 280, 899, -1179); + double test12 = WignerSymbols::wigner3j(999, 1221, 325, 899, -1179, 280); + double test13 = WignerSymbols::wigner3j(1221, 325, 999, -1179, 280, 899); + + double test21 = WignerSymbols::wigner3j(693, 896, 1371, 513, -838, 325); + double test22 = WignerSymbols::wigner3j(896, 1371, 693, -838, 325, 513); + double test23 = WignerSymbols::wigner3j(1371, 693, 896, 325, 513, -838); + + double test31 = WignerSymbols::wigner3j(772, 874, 1231, 442, 756, -1198); + double test32 = WignerSymbols::wigner3j(874, 1231, 772, 756, -1198, 442); + double test33 = WignerSymbols::wigner3j(1231, 772, 874, -1198, 442, 756); + +//[ 772 874 1231 442 756 -1198] 0.00171146251356 0.0 0.0 0.00171146251356 +//[ 842 996 1714 376 979 -1355] -9.12730017838e-14 -0.0 0.0 -9.12730017838e-14 +//[ 101 987 1084 -80 -935 1015] -0.00274486614091 -0.0 0.0 -0.00274486614091 +//[ 51 1003 978 -32 993 -961] -0.00561491486489 -0.0 0.0 -0.00561491486489 +//[ 217 1008 1107 -76 -987 1063] -0.00129163704248 -0.0 0.0 -0.00129163704248 +//[ 408 894 954 -114 -840 954] 0.000141186572123 0.0 0.0 0.000141186572123 +//[ 808 980 1734 341 954 -1295] -1.13581014918e-35 -0.0 0.0 -1.13581014918e-35 +//[ 827 1020 1570 590 902 -1492] -0.00091099434249 -0.0 0.0 -0.00091099434249 + + std::cout << "C++ impl.: " << test11 << ", " << test12 << ", " << test13 << std::endl; + std::cout << "C++ impl.: " << test21 << ", " << test22 << ", " << test23 << std::endl; + std::cout << "C++ impl.: " << test31 << ", " << test32 << ", " << test33 << std::endl; + + std::cout << WignerSymbols::maxThree(2.0, 1.0, 4.0) << std::endl; + + return 0; +} +