Loading...
Searching...
No Matches
FiniteDifference.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#pragma once
13
14#include "Eigen.h"
15
16#include <cctype>
17#include <string>
18#include <string_view>
19
20namespace eonc {
21
26
27inline FdScheme parseFdScheme(std::string_view scheme) {
28 std::string lower;
29 lower.reserve(scheme.size());
30 for (unsigned char c : scheme) {
31 lower.push_back(static_cast<char>(std::tolower(c)));
32 }
33 if (lower == "central") {
34 return FdScheme::Central;
35 }
36 if (lower == "fourth" || lower == "fourth_order" || lower == "central4") {
37 return FdScheme::Fourth;
38 }
39 return FdScheme::OneSided;
40}
41
45template <class M>
46M fdForceDerivative(FdScheme scheme, double dr, const M &f0, const M &fPlus,
47 const M &fMinus, const M &fPlus2, const M &fMinus2) {
48 M slope = fPlus;
49 switch (scheme) {
51 slope = (-fPlus2 + 8.0 * fPlus - 8.0 * fMinus + fMinus2) / (12.0 * dr);
52 break;
54 slope = (fPlus - fMinus) / (2.0 * dr);
55 break;
57 slope = (fPlus - f0) / dr;
58 break;
59 }
60 return slope;
61}
62
66template <class Eval>
67VectorXd fdHessianVector(FdScheme scheme, double dr, const VectorXd &force0,
68 Eval &&eval) {
69 const VectorXd zero = VectorXd::Zero(force0.size());
70 switch (scheme) {
71 case FdScheme::Fourth: {
72 const VectorXd fPlus = eval(1.0);
73 const VectorXd fMinus = eval(-1.0);
74 const VectorXd fPlus2 = eval(2.0);
75 const VectorXd fMinus2 = eval(-2.0);
76 return -fdForceDerivative(scheme, dr, force0, fPlus, fMinus, fPlus2,
77 fMinus2);
78 }
79 case FdScheme::Central: {
80 const VectorXd fPlus = eval(1.0);
81 const VectorXd fMinus = eval(-1.0);
82 return -fdForceDerivative(scheme, dr, force0, fPlus, fMinus, zero, zero);
83 }
85 break;
86 }
87 const VectorXd fPlus = eval(1.0);
88 return -fdForceDerivative(FdScheme::OneSided, dr, force0, fPlus, zero, zero,
89 zero);
90}
91
92} // namespace eonc
RAII resource manager for the ARTn C library with global synchronization.
FdScheme
Real finite-difference scheme for the assembled Hessian and for Lanczos/Davidson Hessian-vector produ...
M fdForceDerivative(FdScheme scheme, double dr, const M &f0, const M &fPlus, const M &fMinus, const M &fPlus2, const M &fMinus2)
Derivative of a sampled force map along one real step of length dr.
VectorXd fdHessianVector(FdScheme scheme, double dr, const VectorXd &force0, Eval &&eval)
Energy Hessian-vector product -dF.
FdScheme parseFdScheme(std::string_view scheme)