n2p2 - A neural network potential package
Loading...
Searching...
No Matches
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 "Kspace.h"
26#include "Log.h"
27#include "ScreeningFunction.h"
28#include "Settings.h"
29#include "Structure.h"
30#include "SymFnc.h"
31#include <cstddef> // std::size_t
32#include <map> // std::map
33#include <string> // std::string
34#include <vector> // std::vector
35
36namespace nnp
37{
38
87class Mode
88{
89public:
90 enum class NNPType
91 {
104 };
105
106 Mode();
109 void initialize();
114 void loadSettingsFile(std::string const& fileName
115 = "input.nn");
126 void setupGeneric(std::string const& nnpDir = "",
127 bool skipNormalize = false,
128 bool initialHardness = false);
137 void setupNormalization(bool standalone = true);
143 virtual void setupElementMap();
149 virtual void setupElements();
157 void setupCutoff();
163 virtual void setupSymmetryFunctions();
181 virtual void setupSymmetryFunctionScaling(
182 std::string const& fileName = "scaling.data");
188 virtual void setupSymmetryFunctionGroups();
189#ifndef N2P2_NO_SF_CACHE
197 virtual void setupSymmetryFunctionCache(bool verbose = false);
198#endif
205 void setupSymmetryFunctionMemory(bool verbose = false);
222 bool collectStatistics,
223 bool collectExtrapolationWarnings,
224 bool writeExtrapolationWarnings,
225 bool stopOnExtrapolationWarnings);
228 void setupCutoffMatrix();
236 virtual void setupNeuralNetwork();
248 virtual void setupNeuralNetworkWeights(
249 std::map<std::string,
250 std::string> fileNameFormats =
251 std::map<std::string,
252 std::string>());
266 virtual void setupNeuralNetworkWeights(
267 std::string directoryPrefix,
268 std::map<std::string,
269 std::string> fileNameFormats =
270 std::map<std::string,
271 std::string>());
281 virtual void setupElectrostatics(bool initialHardness =
282 false,
283 std::string directoryPrefix =
284 "",
285 std::string fileNameFormat =
286 "hardness.%03zu.data");
299 Structure& structure,
300 bool const derivatives);
314 Structure& structure,
315 bool const derivatives);
316 // /** Calculate a single atomic neural network for a given atom and nn type.
317 // *
318 // * @param[in] nnId Neural network identifier, e.g. "short", "charge".
319 // * @param[in] atom Input atom.
320 // * @param[in] derivatives If `true` calculate also derivatives of neural
321 // * networks with respect to input layer neurons
322 // * (required for force calculation).
323 // *
324 // * The atomic energy and charge is stored in Atom::energy and Atom::charge,
325 // * respectively. If derivatives are calculated the results are stored in
326 // * Atom::dEdG or Atom::dQdG.
327 // */
328 //void calculateAtomicNeuralNetwork(
329 // std::string const& nnId,
330 // Atom& atom,
331 // bool const derivatives);
342 Structure& structure,
343 bool const derivatives,
344 std::string id = "");
353 Structure& structure,
354 bool const derivativesElec);
362 void calculateEnergy(Structure& structure) const;
370 void calculateCharge(Structure& structure) const;
378 void calculateForces(Structure& structure) const;
385 void evaluateNNP(Structure& structure,
386 bool useForces = true,
387 bool useDEdG = true);
393 void addEnergyOffset(Structure& structure,
394 bool ref = true);
402 void removeEnergyOffset(Structure& structure,
403 bool ref = true);
410 double getEnergyOffset(Structure const& structure) const;
421 double getEnergyWithOffset(
422 Structure const& structure,
423 bool ref = true) const;
432 double normalized(std::string const& property,
433 double value) const;
441 double normalizedEnergy(
442 Structure const& structure,
443 bool ref = true) const;
452 double physical(std::string const& property,
453 double value) const;
461 double physicalEnergy(Structure const& structure,
462 bool ref = true) const;
468 Structure& structure) const;
474 Structure& structure) const;
478 void logEwaldCutoffs();
484 std::size_t getNumExtrapolationWarnings() const;
492 NNPType getNnpType() const;
497 double getMeanEnergy() const;
502 double getConvEnergy() const;
507 double getConvLength() const;
512 double getConvCharge() const;
518 double getEwaldPrecision() const;
524 double getEwaldMaxCharge() const;
530 double getEwaldMaxSigma() const;
549 double getMaxCutoffRadius() const;
556 std::size_t getNumElements() const;
566 std::vector<std::size_t> getNumSymmetryFunctions() const;
571 bool useNormalization() const;
579 std::string const& keyword) const;
586 std::string settingsGetValue(
587 std::string const& keyword) const;
596 std::vector<std::size_t> pruneSymmetryFunctionsRange(double threshold);
606 std::vector<std::size_t> pruneSymmetryFunctionsSensitivity(
607 double threshold,
608 std::vector<
609 std::vector<double> > sensitivity);
616 std::vector<std::size_t> prune,
617 std::string fileName
618 = "output.nn") const;
624 std::ofstream* const& file) const;
625
630
631protected:
633 struct NNSetup
634 {
648
650 std::string id;
652 std::string name;
654 std::string weightFileFormat;
656 std::string keywordSuffix;
658 std::string keywordSuffix2;
660 std::vector<Topology> topology;
661 };
662
663
667 std::size_t numElements;
668 std::vector<std::size_t> minNeighbors;
669 std::vector<double> minCutoffRadius;
676 double fourPiEps;
683 std::vector<Element> elements;
684 std::vector<std::string> nnk;
685 std::map<
686 std::string, NNSetup> nns;
688 std::vector<
689 std::vector<double>> cutoffs;
691
697 void readNeuralNetworkWeights(std::string const& id,
698 std::string const& fileName);
699};
700
702// Inlined function definitions //
704
706{
707 return nnpType;
708}
709
710inline double Mode::getMeanEnergy() const
711{
712 return meanEnergy;
713}
714
715inline double Mode::getConvEnergy() const
716{
717 return convEnergy;
718}
719
720inline double Mode::getConvLength() const
721{
722 return convLength;
723}
724
725inline double Mode::getConvCharge() const
726{
727 return convCharge;
728}
729
730inline double Mode::getMaxCutoffRadius() const
731{
732 return maxCutoffRadius;
733}
734
735inline std::size_t Mode::getNumElements() const
736{
737 return numElements;
738}
739
740inline double Mode::getEwaldPrecision() const
741{
742 return ewaldSetup.getPrecision();
743}
744
745inline double Mode::getEwaldMaxCharge() const
746{
747 return ewaldSetup.getMaxCharge();
748}
749
750inline double Mode::getEwaldMaxSigma() const
751{
752 return ewaldSetup.getMaxQSigma();
753}
754
756{
757 return ewaldSetup.getTruncMethod();
758}
759
761{
762 return kspaceGrid.kspaceSolver;
763}
764
765inline bool Mode::useNormalization() const
766{
767 return normalize;
768}
769
770}
771
772#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
double physicalEnergy(Structure const &structure, bool ref=true) const
Undo normalization for a given energy of structure.
Definition Mode.cpp:2138
bool checkExtrapolationWarnings
Definition Mode.h:666
std::vector< std::vector< double > > cutoffs
Matrix storing all symmetry function cut-offs for all elements.
Definition Mode.h:689
bool normalize
Definition Mode.h:665
NNPType getNnpType() const
Getter for Mode::nnpType.
Definition Mode.h:705
double convEnergy
Definition Mode.h:673
KSPACESolver kspaceSolver() const
Getter for Mode::kspaceSolver.
Definition Mode.h:760
void setupNormalization(bool standalone=true)
Set up normalization.
Definition Mode.cpp:239
ElementMap elementMap
Global element map, populated by setupElementMap().
Definition Mode.h:627
NNPType nnpType
Definition Mode.h:664
double fourPiEps
Definition Mode.h:676
std::vector< double > minCutoffRadius
Definition Mode.h:669
EWALDTruncMethod getEwaldTruncationMethod() const
Getter for Mode::ewaldSetup.truncMethod.
Definition Mode.h:755
void addEnergyOffset(Structure &structure, bool ref=true)
Add atomic energy offsets to reference energy.
Definition Mode.cpp:2034
void initialize()
Write welcome message with version information.
Definition Mode.cpp:56
double convLength
Definition Mode.h:674
void logEwaldCutoffs()
Logs Ewald params whenever they change.
Definition Mode.cpp:2165
virtual void setupElementMap()
Set up the element map.
Definition Mode.cpp:302
void readNeuralNetworkWeights(std::string const &id, std::string const &fileName)
Read in weights for a specific type of neural network.
Definition Mode.cpp:2286
std::size_t getNumElements() const
Getter for Mode::numElements.
Definition Mode.h:735
virtual void setupNeuralNetwork()
Set up neural networks for all elements.
Definition Mode.cpp:1182
virtual void setupSymmetryFunctionCache(bool verbose=false)
Set up symmetry function cache.
Definition Mode.cpp:1008
double getConvLength() const
Getter for Mode::convLength.
Definition Mode.h:720
double maxCutoffRadius
Definition Mode.h:670
double getEwaldPrecision() const
Getter for Mode::ewaldSetup.precision.
Definition Mode.h:740
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:213
NNPType
Definition Mode.h:91
@ HDNNP_2G
Short range NNP (2G-HDNNP).
Definition Mode.h:93
@ HDNNP_Q
Short range NNP with charge NN, no electrostatics/Qeq (M.
Definition Mode.h:103
@ HDNNP_4G
NNP with electrostatics and non-local charge transfer (4G-HDNNP).
Definition Mode.h:95
std::vector< std::string > nnk
Definition Mode.h:684
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:369
std::vector< std::size_t > pruneSymmetryFunctionsRange(double threshold)
Prune symmetry functions according to their range and write settings file.
Definition Mode.cpp:2244
double meanEnergy
Definition Mode.h:672
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:1469
ScreeningFunction screeningFunction
Definition Mode.h:682
SymFnc::ScalingType scalingType
Definition Mode.h:680
void calculateAtomicNeuralNetworks(Structure &structure, bool const derivatives, std::string id="")
Calculate a single atomic neural network for a given atom and nn type.
Definition Mode.cpp:1666
double physical(std::string const &property, double value) const
Undo normalization for a given property.
Definition Mode.cpp:2126
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:2220
virtual void setupSymmetryFunctionGroups()
Set up symmetry function groups.
Definition Mode.cpp:866
std::vector< Element > elements
Definition Mode.h:683
double getMaxCutoffRadius() const
Getter for Mode::maxCutoffRadius.
Definition Mode.h:730
double normalized(std::string const &property, double value) const
Apply normalization to given property.
Definition Mode.cpp:2100
std::size_t numElements
Definition Mode.h:667
bool useNormalization() const
Check if normalization is enabled.
Definition Mode.h:765
void convertToNormalizedUnits(Structure &structure) const
Convert one structure to normalized units.
Definition Mode.cpp:2151
void calculateEnergy(Structure &structure) const
Calculate potential energy for a given structure.
Definition Mode.cpp:1831
void calculateSymmetryFunctionGroups(Structure &structure, bool const derivatives)
Calculate all symmetry function groups for all atoms in given structure.
Definition Mode.cpp:1585
settings::Settings settings
Definition Mode.h:679
double getConvEnergy() const
Getter for Mode::convEnergy.
Definition Mode.h:715
ScreeningFunction getScreeningFunction() const
Getter for Mode::screeningFunction.
void setupSymmetryFunctionScalingNone()
Set up "empy" symmetry function scaling.
Definition Mode.cpp:716
std::string settingsGetValue(std::string const &keyword) const
Get value for given keyword in Settings instance.
Definition Mode.cpp:2214
virtual void setupSymmetryFunctionScaling(std::string const &fileName="scaling.data")
Set up symmetry function scaling from file.
Definition Mode.cpp:736
double convCharge
Definition Mode.h:675
std::vector< std::size_t > getNumSymmetryFunctions() const
Get number of symmetry functions per element.
Definition Mode.cpp:2196
ErfcBuf erfcBuf
Definition Mode.h:690
std::size_t getNumExtrapolationWarnings() const
Count total number of extrapolation warnings encountered for all elements and symmetry functions.
Definition Mode.cpp:2182
void removeEnergyOffset(Structure &structure, bool ref=true)
Remove atomic energy offsets from reference energy.
Definition Mode.cpp:2053
bool settingsKeywordExists(std::string const &keyword) const
Check if keyword was found in settings file.
Definition Mode.cpp:2209
Log log
Global log file.
Definition Mode.h:629
KspaceGrid kspaceGrid
Definition Mode.h:678
double getMeanEnergy() const
Getter for Mode::meanEnergy.
Definition Mode.h:710
void resetExtrapolationWarnings()
Erase all extrapolation warnings and reset counters.
Definition Mode.cpp:2171
double getEnergyWithOffset(Structure const &structure, bool ref=true) const
Add atomic energy offsets and return energy.
Definition Mode.cpp:2085
std::vector< std::size_t > minNeighbors
Definition Mode.h:668
void convertToPhysicalUnits(Structure &structure) const
Convert one structure to physical units.
Definition Mode.cpp:2158
void calculateCharge(Structure &structure) const
Calculate total charge for a given structure.
Definition Mode.cpp:1858
double getEwaldMaxSigma() const
Getter for Mode::ewaldSetup.maxQsigma.
Definition Mode.h:750
virtual void setupSymmetryFunctions()
Set up all symmetry functions.
Definition Mode.cpp:639
void chargeEquilibration(Structure &structure, bool const derivativesElec)
Perform global charge equilibration method.
Definition Mode.cpp:1779
void calculateSymmetryFunctions(Structure &structure, bool const derivatives)
Calculate all symmetry functions for all atoms in given structure.
Definition Mode.cpp:1504
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:1984
double cutoffAlpha
Definition Mode.h:671
void setupCutoffMatrix()
Setup matrix storing all symmetry function cut-offs for each element.
Definition Mode.cpp:1167
std::map< std::string, NNSetup > nns
Definition Mode.h:686
EwaldSetup ewaldSetup
Definition Mode.h:677
void calculateForces(Structure &structure) const
Calculate forces for all atoms in given structure.
Definition Mode.cpp:1877
void setupSymmetryFunctionStatistics(bool collectStatistics, bool collectExtrapolationWarnings, bool writeExtrapolationWarnings, bool stopOnExtrapolationWarnings)
Set up symmetry function statistics collection.
Definition Mode.cpp:1127
void setupCutoff()
Set up cutoff function for all symmetry functions.
Definition Mode.cpp:544
void loadSettingsFile(std::string const &fileName="input.nn")
Open settings file and load all keywords into memory.
Definition Mode.cpp:162
std::vector< std::size_t > pruneSymmetryFunctionsSensitivity(double threshold, std::vector< std::vector< double > > sensitivity)
Prune symmetry functions with sensitivity analysis data.
Definition Mode.cpp:2265
double getConvCharge() const
Getter for Mode::convCharge.
Definition Mode.h:725
double getEnergyOffset(Structure const &structure) const
Get atomic energy offset for given structure.
Definition Mode.cpp:2072
void setupSymmetryFunctionMemory(bool verbose=false)
Extract required memory dimensions for symmetry function derivatives.
Definition Mode.cpp:918
void writeSettingsFile(std::ofstream *const &file) const
Write complete settings file.
Definition Mode.cpp:2237
double normalizedEnergy(Structure const &structure, bool ref=true) const
Apply normalization to given energy of structure.
Definition Mode.cpp:2112
CutoffFunction::CutoffType cutoffType
Definition Mode.h:681
virtual void setupElements()
Set up all Element instances.
Definition Mode.cpp:323
double getEwaldMaxCharge() const
Getter for Mode::ewaldSetup.maxCharge.
Definition Mode.h:745
ActivationFunction
List of available activation function types.
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
EWALDTruncMethod
Definition EwaldSetup.h:29
KSPACESolver
Definition Kspace.h:26
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:643
Topology()
Constructor.
Definition Mode.h:646
std::vector< int > numNeuronsPerLayer
Number of neurons per layer.
Definition Mode.h:640
int numLayers
Number of NN layers (including input and output layer).
Definition Mode.h:638
Setup data for one neural network.
Definition Mode.h:634
std::string keywordSuffix
Suffix for keywords (NN topology related).
Definition Mode.h:656
std::vector< Topology > topology
Per-element NN topology.
Definition Mode.h:660
std::string weightFileFormat
Format for weight files.
Definition Mode.h:654
std::string id
NN identifier, e.g. "short", "charge",...
Definition Mode.h:650
std::string keywordSuffix2
Suffix for some other keywords (weight file loading related).
Definition Mode.h:658
std::string name
Description string for log output, e.g. "electronegativity".
Definition Mode.h:652
Storage for one atomic configuration.
Definition Structure.h:39