Skip to content

Commit

Permalink
Mixer modernisation (#87)
Browse files Browse the repository at this point in the history
* Replace mixer implementations with more modern variants

* Implement FYPP macros and re-enable assertions
  • Loading branch information
vanderhe authored Jun 22, 2024
1 parent 0b2aa17 commit 4a9709a
Show file tree
Hide file tree
Showing 19 changed files with 1,016 additions and 572 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ set(PYTHON_VERSION_MAJOR_MINOR "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR

set(FYPP "${PROJECT_SOURCE_DIR}/external/fypp/bin/fypp" CACHE FILEPATH "Fypp preprocessor")
skprogs_add_fypp_defines(FYPP_FLAGS)
set(FYPP_FLAGS "${FYPP_FLAGS}")

set(FYPP_CONFIG_FLAGS "${FYPP_FLAGS}")
# Make sure, the line-marker option is not set
list(REMOVE_ITEM FYPP_CONFIG_FLAGS "-n")
set(FYPP_BUILD_FLAGS "${FYPP_FLAGS}" "$<IF:$<CONFIG:Debug>,-DDEBUG=1,-DDEBUG=0>")
set(FYPP_BUILD_FLAGS "${FYPP_FLAGS}" "--file-var-root=${CMAKE_SOURCE_DIR}"
"$<IF:$<CONFIG:Debug>,-DDEBUG=1,-DDEBUG=0>")

set(PYTHON_INTERPRETER "python3" CACHE STRING
"Python interpreter to use for installing and test python components")
Expand Down
19 changes: 18 additions & 1 deletion common/include/common.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,26 @@


#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#! DEBUG related macros
#! ASSERT and DEBUG related macros
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#! Check a condition if WITH_ASSERT is True and call assertError if condition is False.
#! If an optional text string is included, print this in addition as an error
#:def ASSERT(cond, msg=None)
#:if WITH_ASSERT
if (.not. (${cond}$)) then
block
use common_assert, only : assertError
#:if msg
call assertError("${_FILE_}$", ${_LINE_}$, ${msg}$)
#:else
call assertError("${_FILE_}$", ${_LINE_}$)
#:endif
end block
end if
#:endif
#:enddef ASSERT

#! Insert code if DEBUG level is greater than zero.
#:def DEBUG_CODE(code)
#:if DEBUG > 0
Expand Down
13 changes: 9 additions & 4 deletions common/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../" projectdir)
set(projectdir ${PROJECT_SOURCE_DIR})

#
# General options for all targets
#
set(fypp_flags ${FYPP_BUILD_FLAGS})
list(APPEND fypp_flags -I${CMAKE_CURRENT_SOURCE_DIR}/../include -DRELEASE="'${RELEASE}'")
set(fypp_flags ${FYPP_BUILD_FLAGS} ${FYPP_CONFIG_FLAGS})
list(APPEND fypp_flags -I${projectdir}/common/include -DRELEASE="'${RELEASE}'")

set(sources-f90
accuracy.F90
Expand All @@ -30,7 +30,12 @@ set(sources-f90
taggedout.F90
utils.F90)

add_library(skprogs-common ${sources-f90})
set(sources-fpp
assert.F90)

skprogs_preprocess("${FYPP}" "${fypp_flags}" "F90" "f90" "${sources-fpp}" sources-f90-preproc)

add_library(skprogs-common ${sources-f90} ${sources-f90-preproc})

set(moddir ${CMAKE_CURRENT_BINARY_DIR}/modfiles)
set_target_properties(skprogs-common PROPERTIES Fortran_MODULE_DIRECTORY ${moddir})
Expand Down
41 changes: 41 additions & 0 deletions common/lib/assert.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#:include 'common.fypp'

!> Auxiliary subroutines for the ASSERT command
module common_assert

implicit none

private
#:block DEBUG_CODE
public :: assertError
#:endblock DEBUG_CODE

contains

#:block DEBUG_CODE

!> Prints assertion error and abort program execution.
subroutine assertError(fileName, lineNr, message)

!> Name of the file in which the error occurred.
character(*), intent(in) :: fileName

!> Nr. of the line at which the error occurred.
integer, intent(in) :: lineNr

!> Additional message for error
character(*), intent(in), optional :: message

write(*, '(A)') "!!! UNFULLFILLED ASSERTION"
write(*, '(A,A)') "!!! FILE: ", fileName
write(*, '(A,I0)') "!!! LINE NR.: ", lineNr
if (present(message)) then
write(*, '(A,A,A)') '!!! MESSAGE: "', trim(message), '"'
end if
error stop

end subroutine assertError

#:endblock DEBUG_CODE

end module common_assert
2 changes: 1 addition & 1 deletion sktools/src/sktools/calculators/slateratom.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def write(self, workdir):
out.append("{:s} \t\t{:s} write eigenvectors".format(
self._LOGICALSTRS[False], self._COMMENT))
out.append("{} {:g} \t\t{:s} broyden mixer, mixing factor".format(
self._LOGICALSTRS[True], 0.1, self._COMMENT))
2, 0.1, self._COMMENT))

# Occupations
for ll, occperl in enumerate(self._atomconfig.occupations):
Expand Down
22 changes: 20 additions & 2 deletions slateratom/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
set(projectdir ${PROJECT_SOURCE_DIR})

#
# General options for all targets
#
set(fypp_flags ${FYPP_BUILD_FLAGS} ${FYPP_CONFIG_FLAGS})
list(APPEND fypp_flags -I${projectdir}/common/include -DRELEASE="'${RELEASE}'")

set(sources-f90
avgpot.f90
broyden.f90
blas.f90
core_overlap.f90
coulomb_hfex.f90
coulomb_potential.f90
Expand All @@ -12,14 +20,24 @@ set(sources-f90
hamiltonian.f90
input.f90
integration.f90
lapack.f90
mixer.f90
numerical_differentiation.f90
output.f90
total_energy.f90
utilities.f90
xcfunctionals.f90
zora_routines.f90)

add_library(skprogs-slateratom ${sources-f90})
set(sources-fpp
blasroutines.F90
broydenmixer.F90
lapackroutines.F90
simplemixer.F90)

skprogs_preprocess("${FYPP}" "${fypp_flags}" "F90" "f90" "${sources-fpp}" sources-f90-preproc)

add_library(skprogs-slateratom ${sources-f90} ${sources-f90-preproc})

target_link_libraries(skprogs-slateratom skprogs-common Libxc::xcf03 Libxc::xc)
target_link_libraries(skprogs-slateratom skprogs-common LAPACK::LAPACK)
Expand Down
48 changes: 48 additions & 0 deletions slateratom/lib/blas.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
!> Interface wrapper for the blas routines.
!!
!! ALL BLAS routines which are called from the main code must be included here.
module blas

use common_accuracy, only : rdp
public

interface


!> Performs the rank 1 operation
!! A := alpha*x*y**T + A,
subroutine dger(mm, nn, alpha, xx, incx, yy, incy, aa, lda)
import rdp

!> Matrix sizing
integer, intent(in) :: mm

!> Matrix size
integer, intent(in) :: nn

!> Scale factor
real(rdp), intent(in) :: alpha

!> Vector
real(rdp), intent(in) :: xx(*)

!> Stride
integer, intent(in) :: incx

!> Vector
real(rdp), intent(in) :: yy(*)

!> Stride
integer, intent(in) :: incy

!> Leading matrix dimension
integer, intent(in) :: lda

!> Matrix A
real(rdp), intent(inout) :: aa(lda, *)

end subroutine dger

end interface

end module blas
51 changes: 51 additions & 0 deletions slateratom/lib/blasroutines.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#:include 'common.fypp'

!> Contains F90 wrapper functions for some commonly used blas calls needed in the code.
!! The interface of all BLAS calls must be defined in the module blas.
module blasroutines

use common_accuracy, only : rdp
use blas, only : dger
implicit none

private
public :: ger


!> Rank 1 update of a matrix A := alpha*x*y' + A
!! Wrapper for the level 2 blas routine xger to perform the rank 1 update of a general matrix
interface ger
module procedure ger_dble
end interface ger


contains

!> Double precision rank 1 update of a general matrix
subroutine ger_dble(a, alpha, x, y)

!> Contains the matrix for the update
real(rdp), intent(inout) :: a(:,:)

!> Scaling value for the update contribution
real(rdp), intent(in) :: alpha

!> Vector of values for the update
real(rdp), intent(in) :: x(:)

!> Vector of values for the update
real(rdp), intent(in) :: y(:)

integer :: n, m

@:ASSERT(size(a,dim=1) == size(x))
@:ASSERT(size(a,dim=2) == size(y))

m = size(x)
n = size(y)

call dger(m, n, alpha, x, 1, y, 1, a, m)

end subroutine ger_dble

end module blasroutines
Loading

0 comments on commit 4a9709a

Please sign in to comment.