Skip to content

Commit

Permalink
Add a generic output reader - use internal, then cclib, then OB (#1347)
Browse files Browse the repository at this point in the history
* Add a generic output reader - use internal, then cclib, then OB

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis authored Sep 15, 2023
1 parent f9013e4 commit f3a462e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 6 deletions.
2 changes: 2 additions & 0 deletions avogadro/qtplugins/surfaces/surfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace {
#include <avogadro/quantumio/gamessus.h>
#include <avogadro/quantumio/gaussiancube.h>
#include <avogadro/quantumio/gaussianfchk.h>
#include <avogadro/quantumio/genericoutput.h>
#include <avogadro/quantumio/molden.h>
#include <avogadro/quantumio/mopacaux.h>
#include <avogadro/quantumio/nwchemjson.h>
Expand Down Expand Up @@ -89,6 +90,7 @@ Surfaces::Surfaces(QObject* p) : ExtensionPlugin(p), d(new PIMPL())
Io::FileFormatManager::registerFormat(new QuantumIO::GAMESSUSOutput);
Io::FileFormatManager::registerFormat(new QuantumIO::GaussianFchk);
Io::FileFormatManager::registerFormat(new QuantumIO::GaussianCube);
Io::FileFormatManager::registerFormat(new QuantumIO::GenericOutput);
Io::FileFormatManager::registerFormat(new QuantumIO::MoldenFile);
Io::FileFormatManager::registerFormat(new QuantumIO::MopacAux);
Io::FileFormatManager::registerFormat(new QuantumIO::NWChemJson);
Expand Down
2 changes: 2 additions & 0 deletions avogadro/quantumio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ avogadro_headers(QuantumIO
gamessus.h
gaussianfchk.h
gaussiancube.h
genericoutput.h
molden.h
mopacaux.h
nwchemjson.h
Expand All @@ -16,6 +17,7 @@ target_sources(QuantumIO PRIVATE
gamessus.cpp
gaussianfchk.cpp
gaussiancube.cpp
genericoutput.cpp
molden.cpp
mopacaux.cpp
nwchemjson.cpp
Expand Down
2 changes: 0 additions & 2 deletions avogadro/quantumio/gamessus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ std::vector<std::string> GAMESSUSOutput::fileExtensions() const
std::vector<std::string> extensions;
extensions.emplace_back("gamout");
extensions.emplace_back("gamess");
extensions.emplace_back("log");
extensions.emplace_back("out");
return extensions;
}

Expand Down
96 changes: 96 additions & 0 deletions avogadro/quantumio/genericoutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "genericoutput.h"

#include <avogadro/io/fileformat.h>
#include <avogadro/io/fileformatmanager.h>

#include "gamessus.h"
#include "nwchemlog.h"
#include "orca.h"

#include <iostream>

namespace Avogadro::QuantumIO {

GenericOutput::GenericOutput() {}

GenericOutput::~GenericOutput() {}

std::vector<std::string> GenericOutput::fileExtensions() const
{
std::vector<std::string> extensions;
extensions.emplace_back("out");
extensions.emplace_back("output");
extensions.emplace_back("log");
return extensions;
}

std::vector<std::string> GenericOutput::mimeTypes() const
{
return std::vector<std::string>();
}

bool GenericOutput::read(std::istream& in, Core::Molecule& molecule)
{
// check the stream line-by-line until we see the program name
FileFormat* reader = nullptr;

std::string line;
while (std::getline(in, line)) {
if (line.find("Northwest Computational Chemistry Package") !=
std::string::npos) {
// NWChem
reader = new NWChemLog;
break;
} else if (line.find("GAMESS VERSION") != std::string::npos) {
// GAMESS-US .. don't know if we can read Firefly or GAMESS-UK
reader = new GAMESSUSOutput;
break;
} else if (line.find("O R C A") != std::string::npos) {
// ORCA reader
reader = new ORCAOutput;
break;
}
}

// if we didn't find a program name, check for cclib or OpenBabel
// prefer cclib if it's available
if (reader == nullptr) {
// check what output is available
std::vector<const FileFormat*> readers =
Io::FileFormatManager::instance().fileFormatsFromFileExtension(
"out", FileFormat::File | FileFormat::Read);

// loop through writers to check for "cclib" or "Open Babel"
for (const FileFormat* r : readers) {
if (r->name() == "cclib") {
reader = r->newInstance();
break;
} else if (r->identifier().compare(0, 9, "OpenBabel") == 0) {
reader = r->newInstance();
break;
}
}
}

// rewind the stream
in.seekg(0, std::ios::beg);
in.clear();

if (reader) {
bool success = reader->readFile(fileName(), molecule);
delete reader;
return success;
} else {
appendError(
"Could not determine the program used to generate this output file.");
delete reader;
return false;
}
}

} // namespace Avogadro::QuantumIO
50 changes: 50 additions & 0 deletions avogadro/quantumio/genericoutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_QUANTUMIO_GENERICOUTPUT_H
#define AVOGADRO_QUANTUMIO_GENERICOUTPUT_H

#include "avogadroquantumioexport.h"
#include <avogadro/io/fileformat.h>

#include <map>
#include <vector>

namespace Avogadro {
namespace QuantumIO {

class AVOGADROQUANTUMIO_EXPORT GenericOutput : public Io::FileFormat
{
public:
GenericOutput();
~GenericOutput() override;

Operations supportedOperations() const override
{
return Read | File | Stream | String;
}

FileFormat* newInstance() const override { return new GenericOutput; }
std::string identifier() const override { return "Avogadro: Generic Output"; }
std::string name() const override { return "Generic Output"; }
std::string description() const override { return "Generic output format."; }

std::string specificationUrl() const override { return ""; }

std::vector<std::string> fileExtensions() const override;
std::vector<std::string> mimeTypes() const override;

bool read(std::istream& in, Core::Molecule& molecule) override;
bool write(std::ostream&, const Core::Molecule&) override
{
// Empty, as we do not write output files.
return false;
}
};

} // namespace QuantumIO
} // namespace Avogadro

#endif
2 changes: 0 additions & 2 deletions avogadro/quantumio/nwchemlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ NWChemLog::~NWChemLog()
std::vector<std::string> NWChemLog::fileExtensions() const
{
std::vector<std::string> extensions;
extensions.emplace_back("log");
extensions.emplace_back("out");
extensions.emplace_back("nwchem");
return extensions;
}
Expand Down
3 changes: 1 addition & 2 deletions avogadro/quantumio/orca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ ORCAOutput::~ORCAOutput() {}
std::vector<std::string> ORCAOutput::fileExtensions() const
{
std::vector<std::string> extensions;
extensions.emplace_back("log");
extensions.emplace_back("out");
extensions.emplace_back("orca");
return extensions;
}

Expand Down

0 comments on commit f3a462e

Please sign in to comment.