n2p2 - A neural network potential package
CoreFunction.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3// Copyright (C) 2020 Martin P. Bircher
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18#include "CoreFunction.h"
19#include "utility.h"
20#include <stdexcept>
21
22using namespace std;
23using namespace nnp;
24
25double const CoreFunction::PI = 4.0 * atan(1.0);
26double const CoreFunction::PI_2 = 2.0 * atan(1.0);
27double const CoreFunction::E = exp(1.0);
28
29CoreFunction::CoreFunction() : type (Type::POLY2 ),
30#ifndef N2P2_NO_ASYM_POLY
31 asymmetric(false ),
32#endif
33 fPtr (&CoreFunction:: fPOLY2),
34 dfPtr (&CoreFunction:: dfPOLY2),
35 fdfPtr (&CoreFunction::fdfPOLY2)
36{
37}
38
40{
41 this->type = type;
42
43 if (type == Type::COS)
44 {
48 }
49 else if (type == Type::POLY1)
50 {
54 }
55 else if (type == Type::POLY2)
56 {
60 }
61 else if (type == Type::POLY3)
62 {
66 }
67 else if (type == Type::POLY4)
68 {
72 }
73 else if (type == Type::EXP)
74 {
78 }
79 else
80 {
81 throw invalid_argument("ERROR: Unknown core function.\n");
82 }
83
84 return;
85}
86
87void CoreFunction::setType(string const typeString)
88{
90
91 if (typeString == "c") t = CoreFunction::Type::COS;
92 else if (typeString == "p1") t = CoreFunction::Type::POLY1;
93 else if (typeString == "p2") t = CoreFunction::Type::POLY2;
94 else if (typeString == "p3") t = CoreFunction::Type::POLY3;
95 else if (typeString == "p4") t = CoreFunction::Type::POLY4;
96 else if (typeString == "e") t = CoreFunction::Type::EXP;
97 else
98 {
99 throw invalid_argument("ERROR: Unknown CoreFunction type.\n");
100 }
101
102 setType(t);
103
104 return;
105}
106
107vector<string> CoreFunction::info() const
108{
109 vector<string> v;
110
111 if (type == Type::COS)
112 {
113 v.push_back(strpr("CoreFunction::Type::COS (%d):\n", type));
114 v.push_back("f(x) := 1/2 * (cos(pi*x) + 1)\n");
115 }
116 else if (type == Type::POLY1)
117 {
118 v.push_back(strpr("CoreFunction::Type::POLY1 (%d):\n", type));
119 v.push_back("f(x) := (2x - 3)x^2 + 1\n");
120 }
121 else if (type == Type::POLY2)
122 {
123 v.push_back(strpr("CoreFunction::Type::POLY2 (%d):\n", type));
124 v.push_back("f(x) := ((15 - 6x)x - 10)x^3 + 1\n");
125 }
126 else if (type == Type::POLY3)
127 {
128 v.push_back(strpr("CoreFunction::Type::POLY3 (%d):\n", type));
129 v.push_back("f(x) := (x(x(20x - 70) + 84) - 35)x^4 + 1\n");
130 }
131 else if (type == Type::POLY4)
132 {
133 v.push_back(strpr("CoreFunction::Type::POLY4 (%d):\n", type));
134 v.push_back("f(x) := (x(x((315 - 70x)x - 540) + 420) - 126)x^5 + 1\n");
135 }
136 else if (type == Type::EXP)
137 {
138 v.push_back(strpr("CoreFunction::Type::EXP (%d):\n", type));
139 v.push_back("f(x) := exp(-1 / 1 - x^2)\n");
140 }
141
142 return v;
143}
double fPOLY4(double x) const
Definition: CoreFunction.h:276
double dfPOLY2(double x) const
Definition: CoreFunction.h:244
double fCOS(double x) const
Definition: CoreFunction.h:204
void fdfEXP(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:312
std::vector< std::string > info() const
Get string with formula of compact function.
double fPOLY1(double x) const
Definition: CoreFunction.h:222
Type type
Core function type.
Definition: CoreFunction.h:114
void fdfPOLY3(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:268
double dfPOLY4(double x) const
Definition: CoreFunction.h:283
void(CoreFunction::* fdfPtr)(double x, double &fx, double &dfx) const
Function pointer to fdf.
Definition: CoreFunction.h:124
double dfEXP(double x) const
Definition: CoreFunction.h:306
double(CoreFunction::* dfPtr)(double x) const
Function pointer to df.
Definition: CoreFunction.h:122
double dfPOLY3(double x) const
Definition: CoreFunction.h:263
double fEXP(double x) const
Definition: CoreFunction.h:301
void fdfPOLY4(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:290
double dfCOS(double x) const
Definition: CoreFunction.h:209
void fdfPOLY1(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:232
void setType(Type const type)
Set function type.
void fdfCOS(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:214
double fPOLY2(double x) const
Definition: CoreFunction.h:239
void fdfPOLY2(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:249
double dfPOLY1(double x) const
Definition: CoreFunction.h:227
Type
List of available function types.
Definition: CoreFunction.h:33
double fPOLY3(double x) const
Definition: CoreFunction.h:257
double(CoreFunction::* fPtr)(double x) const
Function pointer to f.
Definition: CoreFunction.h:120
Definition: Atom.h:29
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90