n2p2 - A neural network potential package
nnp::CoreFunction Class Reference

#include <CoreFunction.h>

Collaboration diagram for nnp::CoreFunction:

Public Types

enum class  Type {
  COS , POLY1 , POLY2 , POLY3 ,
  POLY4 , EXP
}
 List of available function types. More...
 

Public Member Functions

 CoreFunction ()
 Constructor, initializes to #Type::POLY2. More...
 
void setType (Type const type)
 Set function type. More...
 
void setType (std::string const typeString)
 Set function type. More...
 
Type getType () const
 Getter for type. More...
 
void setAsymmetric (bool asymmetric)
 Set asymmetric property. More...
 
bool getAsymmetric () const
 Getter for asymmetric. More...
 
double f (double x) const
 Calculate function value \(f(x)\). More...
 
double df (double x) const
 Calculate derivative of function at argument \(\frac{df(x)}{dx}\). More...
 
void fdf (double x, double &fx, double &dfx) const
 Calculate function and derivative at once. More...
 
std::vector< std::string > info () const
 Get string with formula of compact function. More...
 

Private Member Functions

double fCOS (double x) const
 
double dfCOS (double x) const
 
void fdfCOS (double x, double &fx, double &dfx) const
 
double fPOLY1 (double x) const
 
double dfPOLY1 (double x) const
 
void fdfPOLY1 (double x, double &fx, double &dfx) const
 
double fPOLY2 (double x) const
 
double dfPOLY2 (double x) const
 
void fdfPOLY2 (double x, double &fx, double &dfx) const
 
double fPOLY3 (double x) const
 
double dfPOLY3 (double x) const
 
void fdfPOLY3 (double x, double &fx, double &dfx) const
 
double fPOLY4 (double x) const
 
double dfPOLY4 (double x) const
 
void fdfPOLY4 (double x, double &fx, double &dfx) const
 
double fEXP (double x) const
 
double dfEXP (double x) const
 
void fdfEXP (double x, double &fx, double &dfx) const
 

Private Attributes

Type type
 Core function type. More...
 
bool asymmetric
 Enables asymmetry modification (use with polynomials). More...
 
double(CoreFunction::* fPtr )(double x) const
 Function pointer to f. More...
 
double(CoreFunction::* dfPtr )(double x) const
 Function pointer to df. More...
 
void(CoreFunction::* fdfPtr )(double x, double &fx, double &dfx) const
 Function pointer to fdf. More...
 

Static Private Attributes

static double const PI = 4.0 * atan(1.0)
 
static double const PI_2 = 2.0 * atan(1.0)
 
static double const E = exp(1.0)
 

Detailed Description

Definition at line 28 of file CoreFunction.h.

Member Enumeration Documentation

◆ Type

enum class nnp::CoreFunction::Type
strong

List of available function types.

Enumerator
COS 

\(f(x) = \frac{1}{2} \left[ \cos (\pi x) + 1\right] \)

POLY1 

\(f(x) = (2x - 3)x^2 + 1\)

POLY2 

\(f(x) = ((15 - 6x)x - 10) x^3 + 1\)

POLY3 

\(f(x) = (x(x(20x - 70) + 84) - 35)x^4 + 1\)

POLY4 

\(f(x) = (x(x((315 - 70x)x - 540) + 420) - 126)x^5 + 1\)

EXP 

\(f(x) = e^{1 - \frac{1}{1 - x^2}}\)

Definition at line 32 of file CoreFunction.h.

33 {
36 COS,
39 POLY1,
42 POLY2,
45 POLY3,
48 POLY4,
51 EXP
52 };

Constructor & Destructor Documentation

◆ CoreFunction()

CoreFunction::CoreFunction ( )

Constructor, initializes to #Type::POLY2.

Definition at line 29 of file CoreFunction.cpp.

29 : type (Type::POLY2 ),
30#ifndef N2P2_NO_ASYM_POLY
31 asymmetric(false ),
32#endif
33 fPtr (&CoreFunction:: fPOLY2),
36{
37}
double dfPOLY2(double x) const
Definition: CoreFunction.h:244
Type type
Core function type.
Definition: CoreFunction.h:114
void(CoreFunction::* fdfPtr)(double x, double &fx, double &dfx) const
Function pointer to fdf.
Definition: CoreFunction.h:124
double(CoreFunction::* dfPtr)(double x) const
Function pointer to df.
Definition: CoreFunction.h:122
bool asymmetric
Enables asymmetry modification (use with polynomials).
Definition: CoreFunction.h:117
void fdfPOLY2(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:249
double(CoreFunction::* fPtr)(double x) const
Function pointer to f.
Definition: CoreFunction.h:120

Member Function Documentation

◆ setType() [1/2]

void CoreFunction::setType ( Type const  type)

Set function type.

Parameters
[in]typeType of core function used.

Definition at line 39 of file CoreFunction.cpp.

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}
double fPOLY4(double x) const
Definition: CoreFunction.h:276
double fCOS(double x) const
Definition: CoreFunction.h:204
void fdfEXP(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:312
double fPOLY1(double x) const
Definition: CoreFunction.h:222
void fdfPOLY3(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:268
double dfPOLY4(double x) const
Definition: CoreFunction.h:283
double dfEXP(double x) const
Definition: CoreFunction.h:306
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 fdfCOS(double x, double &fx, double &dfx) const
Definition: CoreFunction.h:214
double fPOLY2(double x) const
Definition: CoreFunction.h:239
double dfPOLY1(double x) const
Definition: CoreFunction.h:227
double fPOLY3(double x) const
Definition: CoreFunction.h:257

References COS, dfCOS(), dfEXP(), dfPOLY1(), dfPOLY2(), dfPOLY3(), dfPOLY4(), dfPtr, EXP, fCOS(), fdfCOS(), fdfEXP(), fdfPOLY1(), fdfPOLY2(), fdfPOLY3(), fdfPOLY4(), fdfPtr, fEXP(), fPOLY1(), fPOLY2(), fPOLY3(), fPOLY4(), fPtr, POLY1, POLY2, POLY3, POLY4, and type.

Referenced by nnp::CompactFunction::CompactFunction(), nnp::ScreeningFunction::ScreeningFunction(), nnp::CompactFunction::setCoreFunction(), nnp::ScreeningFunction::setCoreFunction(), nnp::CutoffFunction::setCutoffType(), and setType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setType() [2/2]

void CoreFunction::setType ( std::string const  typeString)

Set function type.

Parameters
[in]typeStringType (string version) of core function used.

Definition at line 87 of file CoreFunction.cpp.

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}
void setType(Type const type)
Set function type.
Type
List of available function types.
Definition: CoreFunction.h:33

References COS, EXP, POLY1, POLY2, POLY3, POLY4, and setType().

Here is the call graph for this function:

◆ getType()

CoreFunction::Type nnp::CoreFunction::getType ( ) const
inline

Getter for type.

Returns
Type used.

Definition at line 158 of file CoreFunction.h.

158{ return type; }

References type.

Referenced by nnp::CompactFunction::getCoreFunctionType(), and nnp::ScreeningFunction::getCoreFunctionType().

Here is the caller graph for this function:

◆ setAsymmetric()

void nnp::CoreFunction::setAsymmetric ( bool  asymmetric)
inline

Set asymmetric property.

Parameters
[in]asymmetricWhether asymmetry should be activated.

Definition at line 161 of file CoreFunction.h.

162{
163 this->asymmetric = asymmetric;
164
165 return;
166}

References asymmetric.

Referenced by nnp::CompactFunction::setAsymmetric().

Here is the caller graph for this function:

◆ getAsymmetric()

bool nnp::CoreFunction::getAsymmetric ( ) const
inline

Getter for asymmetric.

Returns
Whether asymmetry is activated.

Definition at line 168 of file CoreFunction.h.

168{ return asymmetric; }

References asymmetric.

Referenced by nnp::CompactFunction::getAsymmetric().

Here is the caller graph for this function:

◆ f()

double nnp::CoreFunction::f ( double  x) const
inline

Calculate function value \(f(x)\).

Parameters
[in]xFunction argument.
Returns
Function value.

Definition at line 171 of file CoreFunction.h.

172{
173#ifndef N2P2_NO_ASYM_POLY
174 if (asymmetric) x = (2.0 - x) * x;
175#endif
176 return (this->*fPtr)(x);
177}

References asymmetric, and fPtr.

Referenced by nnp::CompactFunction::f(), nnp::ScreeningFunction::f(), and nnp::CutoffFunction::fCORE().

Here is the caller graph for this function:

◆ df()

double nnp::CoreFunction::df ( double  x) const
inline

Calculate derivative of function at argument \(\frac{df(x)}{dx}\).

Parameters
[in]xFunction argument.
Returns
Function derivative value.

Definition at line 179 of file CoreFunction.h.

180{
181#ifndef N2P2_NO_ASYM_POLY
182 if (asymmetric) return (2.0 - 2.0 * x) * (this->*dfPtr)((2.0 - x) * x);
183 else return (this->*dfPtr)(x);
184#else
185 return (this->*dfPtr)(x);
186#endif
187}

References asymmetric, and dfPtr.

Referenced by nnp::CompactFunction::df(), nnp::ScreeningFunction::df(), and nnp::CutoffFunction::dfCORE().

Here is the caller graph for this function:

◆ fdf()

void nnp::CoreFunction::fdf ( double  x,
double &  fx,
double &  dfx 
) const
inline

Calculate function and derivative at once.

Parameters
[in]xFunction argument.
[out]fxFunction value.
[out]dfxDerivative value.

Definition at line 189 of file CoreFunction.h.

190{
191#ifndef N2P2_NO_ASYM_POLY
192 if (asymmetric)
193 {
194 (this->*fdfPtr)((2.0 - x) * x, fx, dfx);
195 dfx *= 2.0 - 2.0 * x;
196 }
197 else (this->*fdfPtr)(x, fx, dfx);
198#else
199 (this->*fdfPtr)(x, fx, dfx);
200#endif
201 return;
202}

References asymmetric, and fdfPtr.

Referenced by nnp::CompactFunction::fdf(), nnp::ScreeningFunction::fdf(), and nnp::CutoffFunction::fdfCORE().

Here is the caller graph for this function:

◆ info()

vector< string > CoreFunction::info ( ) const

Get string with formula of compact function.

Returns
Information string.

Definition at line 107 of file CoreFunction.cpp.

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}
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90

References COS, EXP, POLY1, POLY2, POLY3, POLY4, nnp::strpr(), and type.

Referenced by nnp::ScreeningFunction::info().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fCOS()

double nnp::CoreFunction::fCOS ( double  x) const
inlineprivate

Definition at line 204 of file CoreFunction.h.

205{
206 return 0.5 * (cos(PI * x) + 1.0);
207}
static double const PI
Definition: CoreFunction.h:110

References PI.

Referenced by setType().

Here is the caller graph for this function:

◆ dfCOS()

double nnp::CoreFunction::dfCOS ( double  x) const
inlineprivate

Definition at line 209 of file CoreFunction.h.

210{
211 return -PI_2 * sin(PI * x);
212}
static double const PI_2
Definition: CoreFunction.h:111

References PI, and PI_2.

Referenced by setType().

Here is the caller graph for this function:

◆ fdfCOS()

void nnp::CoreFunction::fdfCOS ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 214 of file CoreFunction.h.

215{
216 double const temp = cos(PI * x);
217 fx = 0.5 * (temp + 1.0);
218 dfx = -0.5 * PI * sqrt(1.0 - temp * temp);
219 return;
220}

References PI.

Referenced by setType().

Here is the caller graph for this function:

◆ fPOLY1()

double nnp::CoreFunction::fPOLY1 ( double  x) const
inlineprivate

Definition at line 222 of file CoreFunction.h.

223{
224 return (2.0 * x - 3.0) * x * x + 1.0;
225}

Referenced by setType().

Here is the caller graph for this function:

◆ dfPOLY1()

double nnp::CoreFunction::dfPOLY1 ( double  x) const
inlineprivate

Definition at line 227 of file CoreFunction.h.

228{
229 return x * (6.0 * x - 6.0);
230}

Referenced by setType().

Here is the caller graph for this function:

◆ fdfPOLY1()

void nnp::CoreFunction::fdfPOLY1 ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 232 of file CoreFunction.h.

233{
234 fx = (2.0 * x - 3.0) * x * x + 1.0;
235 dfx = x * (6.0 * x - 6.0);
236 return;
237}

Referenced by setType().

Here is the caller graph for this function:

◆ fPOLY2()

double nnp::CoreFunction::fPOLY2 ( double  x) const
inlineprivate

Definition at line 239 of file CoreFunction.h.

240{
241 return ((15.0 - 6.0 * x) * x - 10.0) * x * x * x + 1.0;
242}

Referenced by setType().

Here is the caller graph for this function:

◆ dfPOLY2()

double nnp::CoreFunction::dfPOLY2 ( double  x) const
inlineprivate

Definition at line 244 of file CoreFunction.h.

245{
246 return x * x * ((60.0 - 30.0 * x) * x - 30.0);
247}

Referenced by setType().

Here is the caller graph for this function:

◆ fdfPOLY2()

void nnp::CoreFunction::fdfPOLY2 ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 249 of file CoreFunction.h.

250{
251 double const x2 = x * x;
252 fx = ((15.0 - 6.0 * x) * x - 10.0) * x * x2 + 1.0;
253 dfx = x2 * ((60.0 - 30.0 * x) * x - 30.0);
254 return;
255}

Referenced by setType().

Here is the caller graph for this function:

◆ fPOLY3()

double nnp::CoreFunction::fPOLY3 ( double  x) const
inlineprivate

Definition at line 257 of file CoreFunction.h.

258{
259 double const x2 = x * x;
260 return (x * (x * (20.0 * x - 70.0) + 84.0) - 35.0) * x2 * x2 + 1.0;
261}

Referenced by setType().

Here is the caller graph for this function:

◆ dfPOLY3()

double nnp::CoreFunction::dfPOLY3 ( double  x) const
inlineprivate

Definition at line 263 of file CoreFunction.h.

264{
265 return x * x * x * (x * (x * (140.0 * x - 420.0) + 420.0) - 140.0);
266}

Referenced by setType().

Here is the caller graph for this function:

◆ fdfPOLY3()

void nnp::CoreFunction::fdfPOLY3 ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 268 of file CoreFunction.h.

269{
270 double const x2 = x * x;
271 fx = (x * (x * (20.0 * x - 70.0) + 84.0) - 35.0) * x2 * x2 + 1.0;
272 dfx = x2 * x * (x * (x * (140.0 * x - 420.0) + 420.0) - 140.0);
273 return;
274}

Referenced by setType().

Here is the caller graph for this function:

◆ fPOLY4()

double nnp::CoreFunction::fPOLY4 ( double  x) const
inlineprivate

Definition at line 276 of file CoreFunction.h.

277{
278 double const x2 = x * x;
279 return (x * (x * ((315.0 - 70.0 * x) * x - 540.0) + 420.0) - 126.0) *
280 x2 * x2 * x + 1.0;
281}

Referenced by setType().

Here is the caller graph for this function:

◆ dfPOLY4()

double nnp::CoreFunction::dfPOLY4 ( double  x) const
inlineprivate

Definition at line 283 of file CoreFunction.h.

284{
285 double const x2 = x * x;
286 return x2 * x2 *
287 (x * (x * ((2520.0 - 630.0 * x) * x - 3780.0) + 2520.0) - 630.0);
288}

Referenced by setType().

Here is the caller graph for this function:

◆ fdfPOLY4()

void nnp::CoreFunction::fdfPOLY4 ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 290 of file CoreFunction.h.

291{
292 double x4 = x * x;
293 x4 *= x4;
294 fx = (x * (x * ((315.0 - 70.0 * x) * x - 540.0) + 420.0) - 126.0) *
295 x * x4 + 1.0;
296 dfx = x4 *
297 (x * (x * ((2520.0 - 630.0 * x) * x - 3780.0) + 2520.0) - 630.0);
298 return;
299}

Referenced by setType().

Here is the caller graph for this function:

◆ fEXP()

double nnp::CoreFunction::fEXP ( double  x) const
inlineprivate

Definition at line 301 of file CoreFunction.h.

302{
303 return E * exp(1.0 / (x * x - 1.0));
304}
static double const E
Definition: CoreFunction.h:112

References E.

Referenced by setType().

Here is the caller graph for this function:

◆ dfEXP()

double nnp::CoreFunction::dfEXP ( double  x) const
inlineprivate

Definition at line 306 of file CoreFunction.h.

307{
308 double const temp = 1.0 / (x * x - 1.0);
309 return -2.0 * E * x * temp * temp * exp(temp);
310}

References E.

Referenced by setType().

Here is the caller graph for this function:

◆ fdfEXP()

void nnp::CoreFunction::fdfEXP ( double  x,
double &  fx,
double &  dfx 
) const
inlineprivate

Definition at line 312 of file CoreFunction.h.

313{
314 double const temp = 1.0 / (x * x - 1.0);
315 double const temp2 = exp(temp);
316 fc = E * temp2;
317 dfc = -2.0 * E * x * temp * temp * temp2;
318 return;
319}
CutoffFunction fc
Definition: nnp-cutoff.cpp:27

References E, and fc.

Referenced by setType().

Here is the caller graph for this function:

Member Data Documentation

◆ PI

double const CoreFunction::PI = 4.0 * atan(1.0)
staticprivate

Definition at line 110 of file CoreFunction.h.

Referenced by dfCOS(), fCOS(), and fdfCOS().

◆ PI_2

double const CoreFunction::PI_2 = 2.0 * atan(1.0)
staticprivate

Definition at line 111 of file CoreFunction.h.

Referenced by dfCOS().

◆ E

double const CoreFunction::E = exp(1.0)
staticprivate

Definition at line 112 of file CoreFunction.h.

Referenced by dfEXP(), fdfEXP(), and fEXP().

◆ type

Type nnp::CoreFunction::type
private

Core function type.

Definition at line 114 of file CoreFunction.h.

Referenced by getType(), info(), and setType().

◆ asymmetric

bool nnp::CoreFunction::asymmetric
private

Enables asymmetry modification (use with polynomials).

Definition at line 117 of file CoreFunction.h.

Referenced by df(), f(), fdf(), getAsymmetric(), and setAsymmetric().

◆ fPtr

double(CoreFunction::* nnp::CoreFunction::fPtr) (double x) const
private

Function pointer to f.

Definition at line 120 of file CoreFunction.h.

Referenced by f(), and setType().

◆ dfPtr

double(CoreFunction::* nnp::CoreFunction::dfPtr) (double x) const
private

Function pointer to df.

Definition at line 122 of file CoreFunction.h.

Referenced by df(), and setType().

◆ fdfPtr

void(CoreFunction::* nnp::CoreFunction::fdfPtr) (double x, double &fx, double &dfx) const
private

Function pointer to fdf.

Definition at line 124 of file CoreFunction.h.

Referenced by fdf(), and setType().


The documentation for this class was generated from the following files: