Update Colvars library to version 2024-06-04

This commit is contained in:
Giacomo Fiorin
2024-08-06 01:07:43 +02:00
parent 278accd9ea
commit 133dee9ac1
74 changed files with 7343 additions and 4676 deletions

View File

@ -29,7 +29,6 @@
colvarproxy_io::colvarproxy_io()
{
input_buffer_ = NULL;
restart_frequency_engine = 0;
input_stream_error_ = new std::istringstream();
input_stream_error_->setstate(std::ios::badbit);
@ -135,7 +134,9 @@ int colvarproxy_io::rename_file(char const *filename, char const *newfilename)
int error_code = COLVARS_OK;
#if defined(_WIN32) && !defined(__CYGWIN__)
// On straight Windows, must remove the destination before renaming it
error_code |= remove_file(newfilename);
if (_access(newfilename, 00) == 0) {
error_code |= remove_file(newfilename);
}
#endif
int rename_exit_code = 0;
while ((rename_exit_code = std::rename(filename, newfilename)) != 0) {
@ -151,6 +152,51 @@ int colvarproxy_io::rename_file(char const *filename, char const *newfilename)
}
int colvarproxy_io::set_input_prefix(std::string const &prefix)
{
// set input restart name and strip the extension, if present
input_prefix_str = prefix;
if (input_prefix_str.rfind(".colvars.state") != std::string::npos) {
input_prefix_str.erase(input_prefix_str.rfind(".colvars.state"),
std::string(".colvars.state").size());
}
return COLVARS_OK;
}
int colvarproxy_io::set_output_prefix(std::string const &prefix)
{
// set input restart name and strip the extension, if present
output_prefix_str = prefix;
if (output_prefix_str.rfind(".colvars.state") != std::string::npos) {
output_prefix_str.erase(output_prefix_str.rfind(".colvars.state"),
std::string(".colvars.state").size());
}
return COLVARS_OK;
}
int colvarproxy_io::set_restart_output_prefix(std::string const &prefix)
{
// set input restart name and strip the extension, if present
restart_output_prefix_str = prefix;
if (restart_output_prefix_str.rfind(".colvars.state") != std::string::npos) {
restart_output_prefix_str.erase(restart_output_prefix_str.rfind(".colvars.state"),
std::string(".colvars.state").size());
}
return COLVARS_OK;
}
int colvarproxy_io::set_default_restart_frequency(int freq)
{
// TODO check for compatibility with colvarsRestartFrequency
restart_frequency_engine = freq;
return COLVARS_OK;
}
std::istream &colvarproxy_io::input_stream(std::string const &input_name,
std::string const description,
bool error_on_fail)
@ -162,14 +208,19 @@ std::istream &colvarproxy_io::input_stream(std::string const &input_name,
}
if (colvarproxy_io::input_stream_exists(input_name)) {
return *(input_streams_[input_name]);
std::ifstream *ifs =
dynamic_cast<std::ifstream *>(input_streams_[input_name]);
if (ifs && !ifs->is_open()) {
// This file was opened before, re-open it. Using std::ios::binary to
// work around differences in line termination conventions
// See https://github.com/Colvars/colvars/commit/8236879f7de4
ifs->open(input_name.c_str(), std::ios::binary);
}
} else {
input_streams_[input_name] = new std::ifstream(input_name.c_str(),
std::ios::binary);
}
// Using binary to work around differences in line termination conventions
// See https://github.com/Colvars/colvars/commit/8236879f7de4
input_streams_[input_name] = new std::ifstream(input_name.c_str(),
std::ios::binary);
if (input_streams_[input_name]->fail() && error_on_fail) {
cvm::error("Error: cannot open "+description+" \""+input_name+"\".\n",
COLVARS_FILE_ERROR);
@ -179,6 +230,41 @@ std::istream &colvarproxy_io::input_stream(std::string const &input_name,
}
std::istream &
colvarproxy_io::input_stream_from_string(std::string const &input_name,
std::string const &content,
std::string const description)
{
if (!io_available()) {
cvm::error("Error: trying to access an input file/channel "
"from the wrong thread.\n", COLVARS_BUG_ERROR);
return *input_stream_error_;
}
if (colvarproxy_io::input_stream_exists(input_name)) {
std::istringstream *iss =
dynamic_cast<std::istringstream *>(input_streams_[input_name]);
if (iss) {
// If there is already a stringstream, replace it
delete iss;
} else {
std::ifstream *ifs =
dynamic_cast<std::ifstream *>(input_streams_[input_name]);
if (ifs) {
if (ifs->is_open()) {
ifs->close();
}
}
}
}
input_streams_[input_name] = new std::istringstream(content);
return *(input_streams_[input_name]);
}
bool colvarproxy_io::input_stream_exists(std::string const &input_name)
{
return (input_streams_.count(input_name) > 0);
@ -188,7 +274,38 @@ bool colvarproxy_io::input_stream_exists(std::string const &input_name)
int colvarproxy_io::close_input_stream(std::string const &input_name)
{
if (colvarproxy_io::input_stream_exists(input_name)) {
delete input_streams_[input_name];
std::ifstream *ifs = dynamic_cast<std::ifstream *>(input_streams_[input_name]);
if (ifs) {
if (ifs->is_open()) {
ifs->close();
}
} else {
// From a string, just rewind to the begining
std::istringstream * iss = dynamic_cast<std::istringstream *>(input_streams_[input_name]);
if (iss) {
iss->clear();
iss->seekg(0);
}
}
return COLVARS_OK;
}
return cvm::error("Error: input file/channel \""+input_name+
"\" does not exist.\n", COLVARS_FILE_ERROR);
}
int colvarproxy_io::delete_input_stream(std::string const &input_name)
{
if (colvarproxy_io::close_input_stream(input_name) == COLVARS_OK) {
std::ifstream *ifs = dynamic_cast<std::ifstream *>(input_streams_[input_name]);
if (ifs) {
delete ifs;
} else {
std::istringstream * iss = dynamic_cast<std::istringstream *>(input_streams_[input_name]);
if (iss) {
delete iss;
}
}
input_streams_.erase(input_name);
return COLVARS_OK;
}
@ -199,7 +316,8 @@ int colvarproxy_io::close_input_stream(std::string const &input_name)
int colvarproxy_io::close_input_streams()
{
for (std::map<std::string, std::istream *>::iterator ii = input_streams_.begin();
for (std::map<std::string,
std::istream *>::iterator ii = input_streams_.begin();
ii != input_streams_.end();
ii++) {
delete ii->second;
@ -209,6 +327,19 @@ int colvarproxy_io::close_input_streams()
}
std::list<std::string> colvarproxy_io::list_input_stream_names() const
{
std::list<std::string> result;
for (std::map<std::string,
std::istream *>::const_iterator ii = input_streams_.begin();
ii != input_streams_.end();
ii++) {
result.push_back(ii->first);
}
return result;
}
std::ostream & colvarproxy_io::output_stream(std::string const &output_name,
std::string const description)
{
@ -228,7 +359,7 @@ std::ostream & colvarproxy_io::output_stream(std::string const &output_name,
backup_file(output_name.c_str());
output_streams_[output_name] = new std::ofstream(output_name.c_str());
output_streams_[output_name] = new std::ofstream(output_name.c_str(), std::ios::binary);
if (!*(output_streams_[output_name])) {
cvm::error("Error: cannot write to "+description+" \""+output_name+"\".\n",
COLVARS_FILE_ERROR);
@ -303,6 +434,7 @@ int colvarproxy_io::close_output_streams()
osi != output_streams_.end();
osi++) {
(dynamic_cast<std::ofstream *>(osi->second))->close();
delete osi->second;
}
output_streams_.clear();