-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSFOutput.cpp
202 lines (183 loc) · 6.34 KB
/
PSFOutput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
GPU OPTIMIZED MONTE CARLO (GOMC) 2.31
Copyright (C) 2018 GOMC Group
A copy of the GNU General Public License can be found in the COPYRIGHT.txt
along with this program, also can be found at <http://www.gnu.org/licenses/>.
********************************************************************************/
#include "PSFOutput.h"
#include "Molecules.h"
#include <cstdio>
using namespace mol_setup;
namespace
{
const char* remarkHeader = "!NTITLE";
const char* remarkTag = " REMARKS ";
const char* atomHeader = "!NATOM";
const char* bondHeader = "!NBOND: bonds";
const char* angleHeader = "!NTHETA: angles";
const char* dihedralHeader = "!NPHI: dihedrals";
const char* headerFormat = "%8d %s \n";
//atom ID, segment name, residue ID, residue name,
//atom name, atom type, charge, mass, and an unused 0
//const char* atomFormat = "%8d%4s%3d%7s%4s%6s%12.6f%14.4f%12d\n";
const char* atomFormat = "%8d %-5s%-5d%-5s%-5s%-3s%12.6f%14.4f%12d\n";
const int bondPerLine = 4;
const int anglePerLine = 3;
const int dihPerLine = 2;
}
PSFOutput::PSFOutput(const Molecules& molecules, const System &sys,
mol_setup::MolMap& molMap,
const std::vector<std::string>& kindNames) :
molecules(&molecules), molNames(kindNames),
molLookRef(sys.molLookup)
{
molKinds.resize(molMap.size());
for(uint i = 0; i < kindNames.size(); ++i) {
molKinds[i] = molMap[kindNames[i]];
}
CountMolecules();
}
void PSFOutput::CountMolecules()
{
totalAngles = 0;
totalAtoms = 0;
totalBonds = 0;
totalDihs = 0;
uint atomT = 0;
for(uint b = 0; b < BOX_TOTAL; b++) {
for(uint k = 0; k < molKinds.size(); ++k) {
const MoleculeKind& molKind = molecules->GetKind(atomT);
totalAtoms += molKind.NumAtoms() * molLookRef.NumKindInBox(k, b);
totalBonds += molKind.NumBonds() * molLookRef.NumKindInBox(k, b);
totalAngles += molKind.NumAngles() * molLookRef.NumKindInBox(k, b);
totalDihs += molKind.NumDihs() * molLookRef.NumKindInBox(k, b);
atomT += molLookRef.NumKindInBox(k, b);
}
}
}
void PSFOutput::PrintPSF(const std::string& filename) const
{
std::vector<std::string> remarks;
//default file remarks
remarks.push_back("Combined PSF produced by GOMC");
remarks.push_back("Contains Geometry data for molecules in ALL boxes in the system");
PrintPSF(filename, remarks);
}
void PSFOutput::PrintPSF(const std::string& filename,
const std::vector<std::string>& remarks) const
{
FILE* outfile = fopen(filename.c_str(), "w");
if (outfile == NULL) {
fprintf(stderr, "Error opening PSF output file %s", filename.c_str());
return;
}
fprintf(outfile, "PSF\n\n");
PrintRemarks(outfile, remarks);
PrintAtoms(outfile);
PrintBonds(outfile);
PrintAngles(outfile);
PrintDihedrals(outfile);
fclose(outfile);
}
void PSFOutput::PrintRemarks(FILE* outfile, const std::vector<std::string>& remarks) const
{
fprintf(outfile, headerFormat, remarks.size(), remarkHeader);
for(uint i = 0; i < remarks.size(); ++i) {
fprintf(outfile, " REMARKS ");
fprintf(outfile, "%s", remarks[i].c_str());
fputc('\n', outfile);
}
fputc('\n', outfile);
}
void PSFOutput::PrintAtoms(FILE* outfile) const
{
fprintf(outfile, headerFormat, totalAtoms, atomHeader);
//silly psfs index from 1
uint atomID = 1;
uint molID = 1;
uint currKind = molecules->kIndex[0];
for(uint mol = 0; mol < molecules->count; ++mol) {
uint thisKind = molecules->kIndex[mol];
uint nAtoms = molKinds[thisKind].atoms.size();
if(thisKind != currKind) {
currKind = thisKind;
molID = 1;
}
for(uint at = 0; at < nAtoms; ++at) {
const Atom* thisAtom = &molKinds[thisKind].atoms[at];
//atom ID, segment name, residue ID, residue name,
//atom name, atom type, charge, mass, and an unused 0
fprintf(outfile, atomFormat, atomID, molNames[thisKind].c_str(),
molID, molNames[thisKind].c_str(), thisAtom->name.c_str(),
thisAtom->type.c_str(), thisAtom->charge, thisAtom->mass, 0);
++atomID;
}
++molID;
if(molID == 10000)
molID = 1;
}
fputc('\n', outfile);
}
void PSFOutput::PrintBonds(FILE* outfile) const
{
fprintf(outfile, headerFormat, totalBonds, bondHeader);
uint atomID = 1;
uint lineEntry = 0;
for(uint mol = 0; mol < molecules->count; ++mol) {
const MolKind& thisKind = molKinds[molecules->kIndex[mol]];
for(uint i = 0; i < thisKind.bonds.size(); ++i) {
fprintf(outfile, "%8d%8d", thisKind.bonds[i].a0 + atomID,
thisKind.bonds[i].a1 + atomID);
++lineEntry;
if(lineEntry == bondPerLine) {
lineEntry = 0;
fputc('\n', outfile);
}
}
atomID += thisKind.atoms.size();
}
fputs("\n\n", outfile);
}
void PSFOutput::PrintAngles(FILE* outfile) const
{
fprintf(outfile, headerFormat, totalAngles, angleHeader);
uint atomID = 1;
uint lineEntry = 0;
for(uint mol = 0; mol < molecules->count; ++mol) {
const MolKind& thisKind = molKinds[molecules->kIndex[mol]];
for(uint i = 0; i < thisKind.angles.size(); ++i) {
fprintf(outfile, "%8d%8d%8d", thisKind.angles[i].a0 + atomID,
thisKind.angles[i].a1 + atomID,
thisKind.angles[i].a2 + atomID);
++lineEntry;
if(lineEntry == anglePerLine) {
lineEntry = 0;
fputc('\n', outfile);
}
}
atomID += thisKind.atoms.size();
}
fputs("\n\n", outfile);
}
void PSFOutput::PrintDihedrals(FILE* outfile) const
{
fprintf(outfile, headerFormat, totalDihs, dihedralHeader);
uint atomID = 1;
uint lineEntry = 0;
for(uint mol = 0; mol < molecules->count; ++mol) {
const MolKind& thisKind = molKinds[molecules->kIndex[mol]];
for(uint i = 0; i < thisKind.dihedrals.size(); ++i) {
fprintf(outfile, "%8d%8d%8d%8d", thisKind.dihedrals[i].a0 + atomID,
thisKind.dihedrals[i].a1 + atomID,
thisKind.dihedrals[i].a2 + atomID,
thisKind.dihedrals[i].a3 + atomID);
++lineEntry;
if(lineEntry == dihPerLine) {
lineEntry = 0;
fputc('\n', outfile);
}
}
atomID += thisKind.atoms.size();
}
fputs("\n\n", outfile);
}