forked from lammps/lammps
/
dump_cfg_gz.cpp
166 lines (135 loc) · 4.65 KB
/
dump_cfg_gz.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
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "dump_cfg_gz.h"
#include "atom.h"
#include "domain.h"
#include "error.h"
#include "update.h"
#include <cstring>
using namespace LAMMPS_NS;
#define UNWRAPEXPAND 10.0
DumpCFGGZ::DumpCFGGZ(LAMMPS *lmp, int narg, char **arg) :
DumpCFG(lmp, narg, arg)
{
gzFp = NULL;
if (!compressed)
error->all(FLERR,"Dump cfg/gz only writes compressed files");
}
/* ---------------------------------------------------------------------- */
DumpCFGGZ::~DumpCFGGZ()
{
if (gzFp) gzclose(gzFp);
gzFp = NULL;
fp = NULL;
}
/* ----------------------------------------------------------------------
generic opening of a dump file
ASCII or binary or gzipped
some derived classes override this function
------------------------------------------------------------------------- */
void DumpCFGGZ::openfile()
{
// single file, already opened, so just return
if (singlefile_opened) return;
if (multifile == 0) singlefile_opened = 1;
// if one file per timestep, replace '*' with current timestep
char *filecurrent = filename;
if (multiproc) filecurrent = multiname;
if (multifile) {
char *filestar = filecurrent;
filecurrent = new char[strlen(filestar) + 16];
char *ptr = strchr(filestar,'*');
*ptr = '\0';
if (padflag == 0)
sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",
filestar,update->ntimestep,ptr+1);
else {
char bif[8],pad[16];
strcpy(bif,BIGINT_FORMAT);
sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]);
sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1);
}
*ptr = '*';
if (maxfiles > 0) {
if (numfiles < maxfiles) {
nameslist[numfiles] = new char[strlen(filecurrent)+1];
strcpy(nameslist[numfiles],filecurrent);
++numfiles;
} else {
remove(nameslist[fileidx]);
delete[] nameslist[fileidx];
nameslist[fileidx] = new char[strlen(filecurrent)+1];
strcpy(nameslist[fileidx],filecurrent);
fileidx = (fileidx + 1) % maxfiles;
}
}
}
// each proc with filewriter = 1 opens a file
if (filewriter) {
if (append_flag) {
gzFp = gzopen(filecurrent,"ab9");
} else {
gzFp = gzopen(filecurrent,"wb9");
}
if (gzFp == NULL) error->one(FLERR,"Cannot open dump file");
} else gzFp = NULL;
// delete string with timestep replaced
if (multifile) delete [] filecurrent;
}
/* ---------------------------------------------------------------------- */
void DumpCFGGZ::write_header(bigint n)
{
// set scale factor used by AtomEye for CFG viz
// default = 1.0
// for peridynamics, set to pre-computed PD scale factor
// so PD particles mimic C atoms
// for unwrapped coords, set to UNWRAPEXPAND (10.0)
// so molecules are not split across periodic box boundaries
double scale = 1.0;
if (atom->peri_flag) scale = atom->pdscale;
else if (unwrapflag == 1) scale = UNWRAPEXPAND;
char str[64];
sprintf(str,"Number of particles = %s\n",BIGINT_FORMAT);
gzprintf(gzFp,str,n);
gzprintf(gzFp,"A = %g Angstrom (basic length-scale)\n",scale);
gzprintf(gzFp,"H0(1,1) = %g A\n",domain->xprd);
gzprintf(gzFp,"H0(1,2) = 0 A \n");
gzprintf(gzFp,"H0(1,3) = 0 A \n");
gzprintf(gzFp,"H0(2,1) = %g A \n",domain->xy);
gzprintf(gzFp,"H0(2,2) = %g A\n",domain->yprd);
gzprintf(gzFp,"H0(2,3) = 0 A \n");
gzprintf(gzFp,"H0(3,1) = %g A \n",domain->xz);
gzprintf(gzFp,"H0(3,2) = %g A \n",domain->yz);
gzprintf(gzFp,"H0(3,3) = %g A\n",domain->zprd);
gzprintf(gzFp,".NO_VELOCITY.\n");
gzprintf(gzFp,"entry_count = %d\n",nfield-2);
for (int i = 0; i < nfield-5; i++)
gzprintf(gzFp,"auxiliary[%d] = %s\n",i,auxname[i]);
}
/* ---------------------------------------------------------------------- */
void DumpCFGGZ::write_data(int n, double *mybuf)
{
gzwrite(gzFp,mybuf,sizeof(char)*n);
}
/* ---------------------------------------------------------------------- */
void DumpCFGGZ::write()
{
DumpCFG::write();
if (filewriter) {
if (multifile) {
gzclose(gzFp);
gzFp = NULL;
} else {
if (flush_flag)
gzflush(gzFp,Z_SYNC_FLUSH);
}
}
}