Skip to content

Commit 9b72143

Browse files
committed
add dcd support in analyze bar testgrad
1 parent cfd3795 commit 9b72143

File tree

8 files changed

+214
-69
lines changed

8 files changed

+214
-69
lines changed

include/ff/atom.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "ff/precision.h"
33
#include "tool/rcman.h"
4-
#include <istream>
4+
#include <fstream>
55

66
namespace tinker {
77
/// \addtogroup ff
@@ -29,7 +29,9 @@ void copyPosToXyz(bool refreshNBList);
2929
/// - Tinker uses centers of mass.
3030
void bounds();
3131

32-
void readFrameCopyinToXyz(std::istream& input, bool& done);
32+
void readFrameOpen(const std::string& filename, std::ifstream& input);
33+
void readFrameCopyinToXyz(std::ifstream& input, bool& done);
34+
void readFrameClose(std::ifstream& input);
3335

3436
//====================================================================//
3537
// //

include/tool/tinkersuppl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ void tinker_f_rewind(int* unit);
1010
void tinker_f_close(int* unit);
1111

1212
void tinker_f_open(int* unit, std::string file, std::string status);
13+
void tinker_f_open(int* unit, std::string file, std::string form, std::string status);
1314

1415
// memory
1516
int tinker_f_allocated(void* p);

src/atom.cpp

Lines changed: 157 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include "tool/error.h"
77
#include "tool/externfunc.h"
88
#include "tool/gpucard.h"
9-
#include <cassert>
109
#include <tinker/detail/atomid.hh>
1110
#include <tinker/detail/atoms.hh>
1211
#include <tinker/detail/bound.hh>
1312
#include <tinker/detail/usage.hh>
1413

14+
#include <cassert>
15+
1516
namespace tinker {
1617
void nData(RcOp op)
1718
{
@@ -154,49 +155,166 @@ void bounds()
154155
copyPosToXyz();
155156
}
156157

157-
void readFrameCopyinToXyz(std::istream& ipt, bool& done)
158+
inline namespace v1 {
159+
enum
158160
{
159-
if (done)
160-
return;
161+
DCD_HEADER = 0,
162+
DCD_TDELTA = 10,
163+
DCD_USEBOX = 11,
164+
DCD_CTRL_LEN = 21,
161165

162-
if (ipt) {
163-
std::string line;
164-
std::getline(ipt, line); // n and title
165-
std::getline(ipt, line); // either box size or first atom
166-
// 18.643000 18.643000 18.643000 90.000000 90.000000 90.000000
167-
// 1 O 8.733783 7.084710 -0.688468 1 2 3
168-
double l1, l2, l3, a1, a2, a3;
169-
int matched = std::sscanf(line.data(), "%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &a1, &a2, &a3);
170-
int row = 0;
171-
int index;
172-
char name[32];
173-
double xr, yr, zr;
174-
if (matched == 6) {
175-
Box p;
176-
boxLattice(p, box_shape, l1, l2, l3, a1, a2, a3);
177-
boxSetCurrent(p);
178-
} else {
179-
std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr);
180-
index -= 1;
181-
atoms::x[index] = xr;
182-
atoms::y[index] = yr;
183-
atoms::z[index] = zr;
184-
row = 1;
185-
}
166+
DCD_TITLE_NCHARS = 80,
186167

187-
for (int ir = row; ir < n; ++ir) {
188-
std::getline(ipt, line);
189-
std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr);
190-
index -= 1;
191-
atoms::x[index] = xr;
192-
atoms::y[index] = yr;
193-
atoms::z[index] = zr;
194-
}
168+
DCD_AX = 0,
169+
DCD_COS_G = 1,
170+
DCD_BX = 2,
171+
DCD_COS_B = 3,
172+
DCD_COS_A = 4,
173+
DCD_CX = 5,
174+
DCD_XTAL_LEN = 6,
175+
};
176+
177+
enum class Archive
178+
{
179+
NONE = 0,
180+
XYZ = 1,
181+
DCD = 2,
182+
};
183+
}
184+
185+
static int dcdControl[DCD_CTRL_LEN] = {0};
186+
static std::vector<float> dcdx, dcdy, dcdz;
187+
static Archive archive = Archive::NONE;
188+
189+
static void dcdReadIntoBuffer(void* buffer, int nbyte, std::ifstream& ipt)
190+
{
191+
int size1, size2;
192+
ipt.read((char*)&size1, sizeof(int));
193+
if (nbyte > 0) assert(nbyte == size1);
194+
ipt.read((char*)buffer, size1);
195+
ipt.read((char*)&size2, sizeof(int));
196+
}
197+
198+
void readFrameOpen(const std::string& filename, std::ifstream& ipt)
199+
{
200+
// get file format type by inspection of first character
201+
char a1;
202+
ipt.open(filename);
203+
ipt >> a1;
204+
auto arc = Archive::NONE;
205+
if (a1 == ' ')
206+
arc = Archive::XYZ;
207+
else if ('0' <= a1 and a1 <= '9')
208+
arc = Archive::XYZ;
209+
else
210+
arc = Archive::DCD;
211+
212+
if (arc == Archive::DCD) {
213+
ipt.close();
214+
ipt.open(filename, std::ios::in | std::ios::binary);
215+
216+
// read header info along with title and number of atoms
217+
dcdReadIntoBuffer(dcdControl, sizeof(int) * DCD_CTRL_LEN, ipt);
218+
219+
int dcdTitleRecordLen;
220+
ipt.read((char*)&dcdTitleRecordLen, sizeof(int));
221+
std::vector<char> titlebuf;
222+
titlebuf.resize(dcdTitleRecordLen + sizeof(int));
223+
ipt.read(titlebuf.data(), dcdTitleRecordLen + sizeof(int));
224+
225+
int dcdNAtom;
226+
dcdReadIntoBuffer(&dcdNAtom, sizeof(int), ipt);
227+
assert(n == dcdNAtom);
228+
dcdx.resize(n);
229+
dcdy.resize(n);
230+
dcdz.resize(n);
231+
}
232+
233+
archive = arc;
234+
}
235+
236+
static void readFrameDCD(std::ifstream& ipt)
237+
{
238+
if (dcdControl[DCD_USEBOX]) {
239+
double dcdXtal[DCD_XTAL_LEN];
240+
dcdReadIntoBuffer(dcdXtal, sizeof(double) * DCD_XTAL_LEN, ipt);
241+
double ax = dcdXtal[DCD_AX], bx = dcdXtal[DCD_BX], cx = dcdXtal[DCD_CX];
242+
double al = 90., be = 90., ga = 90.;
243+
if (dcdXtal[DCD_COS_A] != 0.0) al = std::acos(dcdXtal[DCD_COS_A]) * radian;
244+
if (dcdXtal[DCD_COS_B] != 0.0) be = std::acos(dcdXtal[DCD_COS_B]) * radian;
245+
if (dcdXtal[DCD_COS_G] != 0.0) ga = std::acos(dcdXtal[DCD_COS_G]) * radian;
246+
Box p;
247+
boxLattice(p, box_shape, ax, bx, cx, al, be, ga);
248+
boxSetCurrent(p);
249+
}
250+
251+
dcdReadIntoBuffer(dcdx.data(), sizeof(float) * n, ipt);
252+
dcdReadIntoBuffer(dcdy.data(), sizeof(float) * n, ipt);
253+
dcdReadIntoBuffer(dcdz.data(), sizeof(float) * n, ipt);
254+
for (int i = 0; i < n; ++i) {
255+
atoms::x[i] = dcdx[i];
256+
atoms::y[i] = dcdy[i];
257+
atoms::z[i] = dcdz[i];
258+
}
259+
}
260+
261+
static void readFrameXYZ(std::ifstream& ipt)
262+
{
263+
std::string line;
264+
std::getline(ipt, line); // n and title
265+
std::getline(ipt, line); // either box size or first atom
266+
// 18.643000 18.643000 18.643000 90.000000 90.000000 90.000000
267+
// 1 O 8.733783 7.084710 -0.688468 1 2 3
268+
double l1, l2, l3, a1, a2, a3;
269+
int matched = std::sscanf(line.data(), "%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &a1, &a2, &a3);
270+
int row = 0;
271+
int index;
272+
char name[32];
273+
double xr, yr, zr;
274+
if (matched == 6) {
275+
Box p;
276+
boxLattice(p, box_shape, l1, l2, l3, a1, a2, a3);
277+
boxSetCurrent(p);
278+
} else {
279+
std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr);
280+
index -= 1;
281+
atoms::x[index] = xr;
282+
atoms::y[index] = yr;
283+
atoms::z[index] = zr;
284+
row = 1;
285+
}
195286

196-
xyzData(RcOp::INIT);
287+
for (int ir = row; ir < n; ++ir) {
288+
std::getline(ipt, line);
289+
std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr);
290+
index -= 1;
291+
atoms::x[index] = xr;
292+
atoms::y[index] = yr;
293+
atoms::z[index] = zr;
197294
}
295+
}
296+
297+
void readFrameCopyinToXyz(std::ifstream& ipt, bool& done)
298+
{
299+
if (!ipt) done = true;
300+
if (done) return;
198301

199-
if (ipt.peek() == EOF)
200-
done = true;
302+
if (archive == Archive::XYZ)
303+
readFrameXYZ(ipt);
304+
else if (archive == Archive::DCD)
305+
readFrameDCD(ipt);
306+
307+
xyzData(RcOp::INIT);
308+
309+
if (!ipt.good() or ipt.peek() == EOF) done = true;
310+
}
311+
312+
void readFrameClose(std::ifstream& ipt)
313+
{
314+
ipt.close();
315+
dcdx.clear();
316+
dcdy.clear();
317+
dcdz.clear();
318+
archive = Archive::NONE;
201319
}
202320
}

src/f/suppl.f

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ subroutine suppl_close (unit)
2727

2828

2929
#ifdef TINKER_SUPPL_DECL
30-
void suppl_open_(int* unit, const char* file, const char* status,
31-
tinker_fchar_len_t file_cap, tinker_fchar_len_t status_cap);
30+
void suppl_open_(int* unit, const char* file, const char* form, const char* status,
31+
tinker_fchar_len_t file_cap, tinker_fchar_len_t form_cap, tinker_fchar_len_t status_cap);
3232
#endif
3333
#ifdef TINKER_SUPPL_IMPL
34-
subroutine suppl_open (unit,file,status)
34+
subroutine suppl_open (unit,file,form,status)
3535
implicit none
3636
integer unit
37-
character*(*) file,status
38-
open (unit=unit,file=file,status=status)
37+
character*(*) file,form,status
38+
open (unit=unit,file=file,form=form,status=status)
3939
return
4040
end
4141
#endif

src/tinkersuppl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ void tinker_f_close(int* unit)
2929

3030
void tinker_f_open(int* unit, std::string file, std::string status)
3131
{
32-
suppl_open_(unit, file.c_str(), status.c_str(), file.length(), status.length());
32+
std::string form = "formatted";
33+
suppl_open_(unit, file.c_str(), form.c_str(), status.c_str(), file.length(), form.length(),
34+
status.length());
35+
}
36+
37+
void tinker_f_open(int* unit, std::string file, std::string form, std::string status)
38+
{
39+
suppl_open_(unit, file.c_str(), form.c_str(), status.c_str(), file.length(), form.length(),
40+
status.length());
3341
}
3442

3543
int tinker_f_allocated(void* p)

src/xanalyze.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
#include "tinker9.h"
3535

36-
#include <fstream>
37-
3836
namespace tinker {
3937
static void xAnalyzeE()
4038
{
@@ -419,7 +417,8 @@ namespace tinker {
419417
void xAnalyze(int, char**)
420418
{
421419
initial();
422-
tinker_f_getxyz();
420+
int ixyz;
421+
tinker_f_getcart(&ixyz);
423422
tinker_f_mechanic();
424423
mechanic2();
425424

@@ -457,7 +456,8 @@ void xAnalyze(int, char**)
457456
auto out = stdout;
458457
FstrView fsw = files::filename;
459458
std::string fname = fsw.trim();
460-
std::ifstream ipt(fname);
459+
std::ifstream ipt;
460+
readFrameOpen(fname, ipt);
461461
bool done = false;
462462
int nframe_processed = 0;
463463
do {
@@ -473,6 +473,7 @@ void xAnalyze(int, char**)
473473
if (opt.find("V") != failed)
474474
xAnalyzeV();
475475
} while (not done);
476+
readFrameClose(ipt);
476477

477478
finish();
478479
tinker_f_final();

0 commit comments

Comments
 (0)