sisi4s
Loading...
Searching...
No Matches
MovecsParser.hpp
Go to the documentation of this file.
1#ifndef _MOVECS_PARSER_HEADE
2#define _MOVECS_PARSER_HEADE
3
4#include <regex>
5#include <string>
6#include <algorithm>
7#include <vector>
8#include <fstream>
9#include <assert.h>
10#include <util/Log.hpp>
11#include <util/Parsing.hpp>
12
13#define _L(_l) LOG(_l, "MovecReader")
14
15template <typename F>
16std::vector<F> readFortranChunk(std::fstream &file) {
17 uint32_t fortran_section, copy;
18 std::vector<F> buffer;
19
20 file.read((char *)&fortran_section, sizeof(int32_t));
21 copy = fortran_section;
22 buffer.resize(fortran_section / sizeof(F));
23 file.read((char *)buffer.data(), fortran_section);
24 file.read((char *)&fortran_section, sizeof(int32_t));
25 assert(copy == fortran_section);
26
27 return buffer;
28}
29using namespace pars;
30namespace nwchem {
31
33 size_t nsets, Np;
34 std::vector<int64_t> nmo;
35 std::vector<std::vector<double>> occupations, eigenvalues, mos;
36 size_t iset = 0; // This allows to skip the first set, dont need this yet
37 MovecReader(const std::string &fileName) {
38 std::fstream file(fileName.c_str(), std::ios::binary | std::ios::in);
39 if (!file.good()) throw "File " + fileName + " not found";
40 if (!file.is_open()) throw "File IO error: " + fileName;
41 readFortranChunk<char>(file); // convergence info
42 readFortranChunk<char>(file); // scftype
43 readFortranChunk<char>(file); // lentit
44 {
45 const auto _rfile(readFortranChunk<char>(file).data());
46 _L(0) << "title: " << _rfile << std::endl;
47 }
48 readFortranChunk<char>(file).data(); // lenbas
49 {
50 const auto _rbasis(readFortranChunk<char>(file).data());
51 _L(0) << "basis: " << _rbasis << std::endl;
52 }
53
54 nsets = *readFortranChunk<int64_t>(file).data(); // nsets
55 Np = *readFortranChunk<int64_t>(file).data(); // Np
56
57 _L(0) << "nsets: " << nsets << std::endl;
58 _L(0) << "Np: " << Np << std::endl;
59
60 // resize occupations and such
61 occupations.resize(nsets);
62 eigenvalues.resize(nsets);
63 mos.resize(nsets);
64
65 // read nmos
66 nmo = readFortranChunk<int64_t>(file);
67 for (size_t i = 0; i < nsets; i++) {
68 _L(0) << "nmo[" << i << "]: " << nmo[i] << std::endl;
69 }
70
71 // loop which reject MOs from the set
72 for (size_t i = 0; i < iset; i++) {
73 readFortranChunk<char>(file);
74 readFortranChunk<char>(file);
75 for (uint64_t j = 0; j < nmo[i]; j++) { // do i = 1, nmo(jset)
76 // read(unitno)
77 readFortranChunk<char>(file);
78 }
79 }
80
81 for (size_t s(0); s < nsets; s++) { // do s = 1, nsets
82
83 // Read occupation numbers
84 occupations[s] = readFortranChunk<double>(file);
85 _L(0) << "#occupations read: " << occupations[s].size() << std::endl;
86 assert(occupations[s].size() == Np);
87 // Eigenvaluess [read(unitno) (evals(j), j=1,Np)]
88 eigenvalues[s] = readFortranChunk<double>(file);
89 _L(0) << "#eigenvalues read: " << eigenvalues[s].size() << std::endl;
90 assert(eigenvalues[s].size() == Np);
91 mos[s].resize(Np * Np);
92 for (size_t i(0); i < nmo[iset]; i++) { // do i = 1, nmo(iset)
93 const std::vector<double> mos_buff(readFortranChunk<double>(file));
94 assert(mos_buff.size() == Np);
95 for (size_t j(0); j < mos_buff.size(); j++)
96 mos[s][j + Np * i] = mos_buff[j];
97 }
98 }
99
100 file.close();
101 }
102};
103
104} // namespace nwchem
105
106#endif
std::vector< F > readFortranChunk(std::fstream &file)
Definition MovecsParser.hpp:16
#define _L(_l)
Definition MovecsParser.hpp:13
Definition BasisParser.hpp:15
Definition Parsing.cxx:6
Definition MovecsParser.hpp:32
std::vector< std::vector< double > > occupations
Definition MovecsParser.hpp:35
std::vector< std::vector< double > > mos
Definition MovecsParser.hpp:35
size_t nsets
Definition MovecsParser.hpp:33
std::vector< int64_t > nmo
Definition MovecsParser.hpp:34
size_t Np
Definition MovecsParser.hpp:33
size_t iset
Definition MovecsParser.hpp:36
MovecReader(const std::string &fileName)
Definition MovecsParser.hpp:37
std::vector< std::vector< double > > eigenvalues
Definition MovecsParser.hpp:35