n2p2 - A neural network potential package
Mode.h
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef MODE_H
18#define MODE_H
19
20#include "CutoffFunction.h"
21#include "Element.h"
22#include "ElementMap.h"
23#include "ErfcBuf.h"
24#include "EwaldSetup.h"
25#include "Log.h"
26#include "ScreeningFunction.h"
27#include "Settings.h"
28#include "Structure.h"
29#include "SymFnc.h"
30#include <cstddef> // std::size_t
31#include <map> // std::map
32#include <string> // std::string
33#include <vector> // std::vector
34
35namespace nnp
36{
37
86class Mode
87{
88public:
89 enum class NNPType
90 {
92 HDNNP_2G = 2,
94 HDNNP_4G = 4,
102 HDNNP_Q = 10
103 };
104
105 Mode();
108 void initialize();
113 void loadSettingsFile(std::string const& fileName
114 = "input.nn");
125 void setupGeneric(std::string const& nnpDir = "",
126 bool skipNormalize = false,
127 bool initialHardness = false);
136 void setupNormalization(bool standalone = true);
142 virtual void setupElementMap();
148 virtual void setupElements();
156 void setupCutoff();
162 virtual void setupSymmetryFunctions();
180 virtual void setupSymmetryFunctionScaling(
181 std::string const& fileName = "scaling.data");
187 virtual void setupSymmetryFunctionGroups();
188#ifndef N2P2_NO_SF_CACHE
196 virtual void setupSymmetryFunctionCache(bool verbose = false);
197#endif
204 void setupSymmetryFunctionMemory(bool verbose = false);
221 bool collectStatistics,
222 bool collectExtrapolationWarnings,
223 bool writeExtrapolationWarnings,
224 bool stopOnExtrapolationWarnings);
227 void setupCutoffMatrix();
235 virtual void setupNeuralNetwork();
247 virtual void setupNeuralNetworkWeights(
248 std::map<std::string,
249 std::string> fileNameFormats =
250 std::map<std::string,
251 std::string>());
265 virtual void setupNeuralNetworkWeights(
266 std::string directoryPrefix,
267 std::map<std::string,
268 std::string> fileNameFormats =
269 std::map<std::string,
270 std::string>());
280 virtual void setupElectrostatics(bool initialHardness =
281 false,
282 std::string directoryPrefix =
283 "",
284 std::string fileNameFormat =
285 "hardness.%03zu.data");
298 Structure& structure,
299 bool const derivatives);
313 Structure& structure,
314 bool const derivatives);
315 // /** Calculate a single atomic neural network for a given atom and nn type.
316 // *
317 // * @param[in] nnId Neural network identifier, e.g. "short", "charge".
318 // * @param[in] atom Input atom.
319 // * @param[in] derivatives If `true` calculate also derivatives of neural
320 // * networks with respect to input layer neurons
321 // * (required for force calculation).
322 // *
323 // * The atomic energy and charge is stored in Atom::energy and Atom::charge,
324 // * respectively. If derivatives are calculated the results are stored in
325 // * Atom::dEdG or Atom::dQdG.
326 // */
327 //void calculateAtomicNeuralNetwork(
328 // std::string const& nnId,
329 // Atom& atom,
330 // bool const derivatives);
341 Structure& structure,
342 bool const derivatives,
343 std::string id = "");
352 Structure& structure,
353 bool const derivativesElec);
361 void calculateEnergy(Structure& structure) const;
369 void calculateCharge(Structure& structure) const;
377 void calculateForces(Structure& structure) const;
384 void evaluateNNP(Structure& structure,
385 bool useForces = true,
386 bool useDEdG = true);
392 void addEnergyOffset(Structure& structure,
393 bool ref = true);
401 void removeEnergyOffset(Structure& structure,
402 bool ref = true);
409 double getEnergyOffset(Structure const& structure) const;
420 double getEnergyWithOffset(
421 Structure const& structure,
422 bool ref = true) const;
431 double normalized(std::string const& property,
432 double value) const;
440 double normalizedEnergy(
441 Structure const& structure,
442 bool ref = true) const;
451 double physical(std::string const& property,
452 double value) const;
460 double physicalEnergy(Structure const& structure,
461 bool ref = true) const;
467 Structure& structure) const;
473 Structure& structure) const;
477 void logEwaldCutoffs();
483 std::size_t getNumExtrapolationWarnings() const;
491 NNPType getNnpType() const;
496 double getMeanEnergy() const;
501 double getConvEnergy() const;
506 double getConvLength() const;
511 double getConvCharge() const;
518 double getMaxCutoffRadius() const;
525 std::size_t getNumElements() const;
530 std::vector<std::size_t> getNumSymmetryFunctions() const;
535 bool useNormalization() const;
543 std::string const& keyword) const;
550 std::string settingsGetValue(
551 std::string const& keyword) const;
560 std::vector<std::size_t> pruneSymmetryFunctionsRange(double threshold);
570 std::vector<std::size_t> pruneSymmetryFunctionsSensitivity(
571 double threshold,
572 std::vector<
573 std::vector<double> > sensitivity);
580 std::vector<std::size_t> prune,
581 std::string fileName
582 = "output.nn") const;
588 std::ofstream* const& file) const;
589
594
595protected:
597 struct NNSetup
598 {
599 struct Topology
600 {
604 std::vector<int> numNeuronsPerLayer;
606 std::vector<
608
611 };
612
614 std::string id;
616 std::string name;
618 std::string weightFileFormat;
620 std::string keywordSuffix;
622 std::string keywordSuffix2;
624 std::vector<Topology> topology;
625 };
626
627
631 std::size_t numElements;
632 std::vector<std::size_t> minNeighbors;
633 std::vector<double> minCutoffRadius;
640 double fourPiEps;
646 std::vector<Element> elements;
647 std::vector<std::string> nnk;
648 std::map<
649 std::string, NNSetup> nns;
651 std::vector<
652 std::vector<double>> cutoffs;
654
660 void readNeuralNetworkWeights(std::string const& id,
661 std::string const& fileName);
662};
663
665// Inlined function definitions //
667
669{
670 return nnpType;
671}
672
673inline double Mode::getMeanEnergy() const
674{
675 return meanEnergy;
676}
677
678inline double Mode::getConvEnergy() const
679{
680 return convEnergy;
681}
682
683inline double Mode::getConvLength() const
684{
685 return convLength;
686}
687
688inline double Mode::getConvCharge() const
689{
690 return convCharge;
691}
692
693inline double Mode::getMaxCutoffRadius() const
694{
695 return maxCutoffRadius;
696}
697
698inline std::size_t Mode::getNumElements() const
699{
700 return numElements;
701}
702
703inline bool Mode::useNormalization() const
704{
705 return normalize;
706}
707
708}
709
710#endif
CutoffType
List of available cutoff function types.
Contains element map.
Definition: ElementMap.h:30
Setup data for Ewald summation.
Definition: EwaldSetup.h:37
Logging class for library output.
Definition: Log.h:34
Base class for all NNP applications.
Definition: Mode.h:87
double physicalEnergy(Structure const &structure, bool ref=true) const
Undo normalization for a given energy of structure.
Definition: Mode.cpp:2122
bool checkExtrapolationWarnings
Definition: Mode.h:630
std::vector< std::vector< double > > cutoffs
Matrix storing all symmetry function cut-offs for all elements.
Definition: Mode.h:652
bool normalize
Definition: Mode.h:629
NNPType getNnpType() const
Getter for Mode::nnpType.
Definition: Mode.h:668
double convEnergy
Definition: Mode.h:637
Mode()
Definition: Mode.cpp:40
void setupNormalization(bool standalone=true)
Set up normalization.
Definition: Mode.cpp:238
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition: Mode.h:591
NNPType nnpType
Definition: Mode.h:628
double fourPiEps
Definition: Mode.h:640
std::vector< double > minCutoffRadius
Definition: Mode.h:633
void addEnergyOffset(Structure &structure, bool ref=true)
Add atomic energy offsets to reference energy.
Definition: Mode.cpp:2018
void initialize()
Write welcome message with version information.
Definition: Mode.cpp:55
double convLength
Definition: Mode.h:638
void logEwaldCutoffs()
Logs Ewald params whenever they change.
Definition: Mode.cpp:2149
virtual void setupElementMap()
Set up the element map.
Definition: Mode.cpp:301
void readNeuralNetworkWeights(std::string const &id, std::string const &fileName)
Read in weights for a specific type of neural network.
Definition: Mode.cpp:2270
std::size_t getNumElements() const
Getter for Mode::numElements.
Definition: Mode.h:698
virtual void setupNeuralNetwork()
Set up neural networks for all elements.
Definition: Mode.cpp:1158
virtual void setupSymmetryFunctionCache(bool verbose=false)
Set up symmetry function cache.
Definition: Mode.cpp:984
double getConvLength() const
Getter for Mode::convLength.
Definition: Mode.h:683
double maxCutoffRadius
Definition: Mode.h:634
void setupGeneric(std::string const &nnpDir="", bool skipNormalize=false, bool initialHardness=false)
Combine multiple setup routines and provide a basic NNP setup.
Definition: Mode.cpp:212
NNPType
Definition: Mode.h:90
std::vector< std::string > nnk
Definition: Mode.h:647
virtual void setupElectrostatics(bool initialHardness=false, std::string directoryPrefix="", std::string fileNameFormat="hardness.%03zu.data")
Set up electrostatics related stuff (hardness, screening, ...).
Definition: Mode.cpp:368
std::vector< std::size_t > pruneSymmetryFunctionsRange(double threshold)
Prune symmetry functions according to their range and write settings file.
Definition: Mode.cpp:2228
double meanEnergy
Definition: Mode.h:636
virtual void setupNeuralNetworkWeights(std::map< std::string, std::string > fileNameFormats=std::map< std::string, std::string >())
Set up neural network weights from files with given name format.
Definition: Mode.cpp:1445
ScreeningFunction screeningFunction
Definition: Mode.h:645
SymFnc::ScalingType scalingType
Definition: Mode.h:643
void calculateAtomicNeuralNetworks(Structure &structure, bool const derivatives, std::string id="")
Calculate atomic neural networks for all atoms in given structure.
Definition: Mode.cpp:1642
double physical(std::string const &property, double value) const
Undo normalization for a given property.
Definition: Mode.cpp:2110
void writePrunedSettingsFile(std::vector< std::size_t > prune, std::string fileName="output.nn") const
Copy settings file but comment out lines provided.
Definition: Mode.cpp:2204
virtual void setupSymmetryFunctionGroups()
Set up symmetry function groups.
Definition: Mode.cpp:842
std::vector< Element > elements
Definition: Mode.h:646
double getMaxCutoffRadius() const
Getter for Mode::maxCutoffRadius.
Definition: Mode.h:693
double normalized(std::string const &property, double value) const
Apply normalization to given property.
Definition: Mode.cpp:2084
std::size_t numElements
Definition: Mode.h:631
bool useNormalization() const
Check if normalization is enabled.
Definition: Mode.h:703
void convertToNormalizedUnits(Structure &structure) const
Convert one structure to normalized units.
Definition: Mode.cpp:2135
void calculateEnergy(Structure &structure) const
Calculate potential energy for a given structure.
Definition: Mode.cpp:1803
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
Definition: Mode.cpp:1561
settings::Settings settings
Definition: Mode.h:642
double getConvEnergy() const
Getter for Mode::convEnergy.
Definition: Mode.h:678
void setupSymmetryFunctionScalingNone()
Set up "empy" symmetry function scaling.
Definition: Mode.cpp:692
std::string settingsGetValue(std::string const &keyword) const
Get value for given keyword in Settings instance.
Definition: Mode.cpp:2198
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
Definition: Mode.cpp:712
double convCharge
Definition: Mode.h:639
std::vector< std::size_t > getNumSymmetryFunctions() const
Get number of symmetry functions per element.
Definition: Mode.cpp:2180
ErfcBuf erfcBuf
Definition: Mode.h:653
std::size_t getNumExtrapolationWarnings() const
Count total number of extrapolation warnings encountered for all elements and symmetry functions.
Definition: Mode.cpp:2166
void removeEnergyOffset(Structure &structure, bool ref=true)
Remove atomic energy offsets from reference energy.
Definition: Mode.cpp:2037
bool settingsKeywordExists(std::string const &keyword) const
Check if keyword was found in settings file.
Definition: Mode.cpp:2193
Log log
Global log file.
Definition: Mode.h:593
double getMeanEnergy() const
Getter for Mode::meanEnergy.
Definition: Mode.h:673
void resetExtrapolationWarnings()
Erase all extrapolation warnings and reset counters.
Definition: Mode.cpp:2155
double getEnergyWithOffset(Structure const &structure, bool ref=true) const
Add atomic energy offsets and return energy.
Definition: Mode.cpp:2069
std::vector< std::size_t > minNeighbors
Definition: Mode.h:632
void convertToPhysicalUnits(Structure &structure) const
Convert one structure to physical units.
Definition: Mode.cpp:2142
void calculateCharge(Structure &structure) const
Calculate total charge for a given structure.
Definition: Mode.cpp:1830
virtual void setupSymmetryFunctions()
Set up all symmetry functions.
Definition: Mode.cpp:615
void chargeEquilibration(Structure &structure, bool const derivativesElec)
Perform global charge equilibration method.
Definition: Mode.cpp:1754
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
Definition: Mode.cpp:1480
void evaluateNNP(Structure &structure, bool useForces=true, bool useDEdG=true)
Evaluate neural network potential (includes total energy, optionally forces and in some cases charges...
Definition: Mode.cpp:1967
double cutoffAlpha
Definition: Mode.h:635
void setupCutoffMatrix()
Setup matrix storing all symmetry function cut-offs for each element.
Definition: Mode.cpp:1143
std::map< std::string, NNSetup > nns
Definition: Mode.h:649
EwaldSetup ewaldSetup
Definition: Mode.h:641
void calculateForces(Structure &structure) const
Calculate forces for all atoms in given structure.
Definition: Mode.cpp:1849
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
Definition: Mode.cpp:1103
void setupCutoff()
Set up cutoff function for all symmetry functions.
Definition: Mode.cpp:520
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition: Mode.cpp:161
std::vector< std::size_t > pruneSymmetryFunctionsSensitivity(double threshold, std::vector< std::vector< double > > sensitivity)
Prune symmetry functions with sensitivity analysis data.
Definition: Mode.cpp:2249
double getConvCharge() const
Getter for Mode::convCharge.
Definition: Mode.h:688
double getEnergyOffset(Structure const &structure) const
Get atomic energy offset for given structure.
Definition: Mode.cpp:2056
void setupSymmetryFunctionMemory(bool verbose=false)
Extract required memory dimensions for symmetry function derivatives.
Definition: Mode.cpp:894
void writeSettingsFile(std::ofstream *const &file) const
Write complete settings file.
Definition: Mode.cpp:2221
double normalizedEnergy(Structure const &structure, bool ref=true) const
Apply normalization to given energy of structure.
Definition: Mode.cpp:2096
CutoffFunction::CutoffType cutoffType
Definition: Mode.h:644
virtual void setupElements()
Set up all Element instances.
Definition: Mode.cpp:322
ActivationFunction
List of available activation function types.
Definition: NeuralNetwork.h:33
A screening functions for use with electrostatics.
ScalingType
List of available scaling types.
Definition: SymFnc.h:44
Reads and analyzes settings file and stores parameters.
Definition: Settings.h:38
Definition: Atom.h:29
Helper class to store previously calculated values of erfc() that are needed during the charge equili...
Definition: ErfcBuf.h:17
std::vector< NeuralNetwork::ActivationFunction > activationFunctionsPerLayer
Activation function type per layer.
Definition: Mode.h:607
Topology()
Constructor.
Definition: Mode.h:610
std::vector< int > numNeuronsPerLayer
Number of neurons per layer.
Definition: Mode.h:604
int numLayers
Number of NN layers (including input and output layer).
Definition: Mode.h:602
Setup data for one neural network.
Definition: Mode.h:598
std::string keywordSuffix
Suffix for keywords (NN topology related).
Definition: Mode.h:620
std::vector< Topology > topology
Per-element NN topology.
Definition: Mode.h:624
std::string weightFileFormat
Format for weight files.
Definition: Mode.h:618
std::string id
NN identifier, e.g. "short", "charge",...
Definition: Mode.h:614
std::string keywordSuffix2
Suffix for some other keywords (weight file loading related).
Definition: Mode.h:622
std::string name
Description string for log output, e.g. "electronegativity".
Definition: Mode.h:616
Storage for one atomic configuration.
Definition: Structure.h:39