diff --git a/src/colvar.cpp b/src/colvar.cpp index 3b4461867..c60a7d0e2 100644 --- a/src/colvar.cpp +++ b/src/colvar.cpp @@ -894,9 +894,7 @@ void colvar::define_component_types() add_component_type("neural network CV for other CVs", "neuralNetwork"); -#ifdef COLVARS_TORCH add_component_type("CV defined by PyTorch artifical neural network models", "torchANN"); -#endif if (proxy->check_volmaps_available() == COLVARS_OK) { add_component_type("total value of atomic map", "mapTotal"); diff --git a/src/colvar.h b/src/colvar.h index 099ad6c26..1dacff431 100644 --- a/src/colvar.h +++ b/src/colvar.h @@ -586,9 +586,6 @@ class colvar : public colvarparse, public colvardeps { // collective variable component base class class cvc; - // placeholder/stub for unavailable functionality - class componentDisabled; - // list of available collective variable components // scalar colvar components diff --git a/src/colvarcomp.cpp b/src/colvarcomp.cpp index b4a610715..e6729f43a 100644 --- a/src/colvarcomp.cpp +++ b/src/colvarcomp.cpp @@ -98,7 +98,8 @@ int colvar::cvc::init(std::string const &conf) if (period != 0.0) { if (!is_available(f_cvc_periodic)) { error_code |= - cvm::error("Error: invalid use of period and/or wrapAround in a \"" + + cvm::error("Error: invalid use of period and/or " + "wrapAround in a \"" + function_type() + "\" component.\n" + "Period: " + cvm::to_str(period) + " wrapAround: " + cvm::to_str(wrap_center), COLVARS_INPUT_ERROR); @@ -706,20 +707,6 @@ void colvar::cvc::wrap(colvarvalue &x_unwrapped) const } - -colvar::componentDisabled::componentDisabled() {} - -colvar::componentDisabled::~componentDisabled() {} - -int colvar::componentDisabled::init(std::string const & /* conf */) -{ - return cvm::error("Error: components of type " + function_type() + - " are not enabled in the current build", - COLVARS_NOT_IMPLEMENTED); -} - - - // Static members std::vector colvar::cvc::cvc_features; diff --git a/src/colvarcomp.h b/src/colvarcomp.h index 94447c3cc..334fdc1f6 100644 --- a/src/colvarcomp.h +++ b/src/colvarcomp.h @@ -26,11 +26,6 @@ #include "colvar.h" #include "colvar_geometricpath.h" -#ifdef COLVARS_TORCH -#include -#include -#endif - /// \brief Colvar component (base class for collective variables) /// @@ -320,16 +315,6 @@ inline colvarvalue const & colvar::cvc::Jacobian_derivative() const } -/// \brief Colvar component class for a feature not currently available -class colvar::componentDisabled - : public colvar::cvc -{ -public: - componentDisabled(); - virtual ~componentDisabled(); - int init(std::string const & /* conf */); -}; - /// \brief Colvar component: distance between the centers of mass of /// two groups (colvarvalue::type_scalar type, range [0:*)) @@ -1547,40 +1532,6 @@ class colvar::neuralNetwork virtual void wrap(colvarvalue &x_unwrapped) const; }; -#ifdef COLVARS_TORCH -// only when LibTorch is available -class colvar::torchANN - : public colvar::linearCombination -{ -protected: - torch::jit::script::Module nn; - /// the index of nn output component - size_t m_output_index; - bool use_double_input; - //bool use_gpu; - // 1d tensor, concatenation of values of sub-cvcs - torch::Tensor input_tensor; - torch::Tensor nn_outputs; - torch::Tensor input_grad; - // record the initial index of of sub-cvcs in input_tensor - std::vector cvc_indices; -public: - torchANN(); - virtual ~torchANN(); - virtual int init(std::string const &conf); - virtual void calc_value(); - virtual void calc_gradients(); - virtual void apply_force(colvarvalue const &force); -}; -#else -class colvar::torchANN - : public colvar::componentDisabled -{ -public: - torchANN(); - virtual ~torchANN(); -}; -#endif // COLVARS_TORCH checking // \brief Colvar component: total value of a scalar map // (usually implemented as a grid by the simulation engine) diff --git a/src/colvarcomp_torchann.cpp b/src/colvarcomp_torchann.cpp index 26291ea76..7f8325681 100644 --- a/src/colvarcomp_torchann.cpp +++ b/src/colvarcomp_torchann.cpp @@ -13,6 +13,7 @@ #include "colvarparse.h" #include "colvarvalue.h" +#include "colvarcomp_torchann.h" #ifdef COLVARS_TORCH @@ -202,14 +203,21 @@ void colvar::torchANN::apply_force(colvarvalue const &force) { #else - colvar::torchANN::torchANN() { set_function_type("torchANN"); } - colvar::torchANN::~torchANN() {} +int colvar::torchANN::init(std::string const &conf) { + + return cvm::error( + "torchANN requires the libtorch library, but it is not enabled during compilation.\n" + "Please refer to the Compilation Notes section of the Colvars manual for more " + "information.\n", + COLVARS_NOT_IMPLEMENTED); + +} #endif diff --git a/src/colvarcomp_torchann.h b/src/colvarcomp_torchann.h new file mode 100644 index 000000000..670688be9 --- /dev/null +++ b/src/colvarcomp_torchann.h @@ -0,0 +1,63 @@ +// -*- c++ -*- + +// This file is part of the Collective Variables module (Colvars). +// The original version of Colvars and its updates are located at: +// https://github.com/Colvars/colvars +// Please update all Colvars source files before making any changes. +// If you wish to distribute your changes, please submit them to the +// Colvars repository at GitHub. +// +#ifndef COLVARCOMP_TORCH_H +#define COLVARCOMP_TORCH_H + +// Declaration of torchann + +#include + +#include "colvar.h" +#include "colvarcomp.h" +#include "colvarmodule.h" + +#ifdef COLVARS_TORCH + +#include +#include + +class colvar::torchANN + : public colvar::linearCombination +{ +protected: + torch::jit::script::Module nn; + /// the index of nn output component + size_t m_output_index; + bool use_double_input; + //bool use_gpu; + // 1d tensor, concatenation of values of sub-cvcs + torch::Tensor input_tensor; + torch::Tensor nn_outputs; + torch::Tensor input_grad; + // record the initial index of of sub-cvcs in input_tensor + std::vector cvc_indices; +public: + torchANN(); + virtual ~torchANN(); + virtual int init(std::string const &conf); + virtual void calc_value(); + virtual void calc_gradients(); + virtual void apply_force(colvarvalue const &force); +}; + +#else + +class colvar::torchANN + : public colvar::cvc +{ +public: + torchANN(); + virtual ~torchANN(); + virtual int init(std::string const &conf); +}; +#endif // COLVARS_TORCH checking + +#endif +