File size: 7,962 Bytes
3af0570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* ----------------------------------------------------------------------
 LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
 https://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 "fix_pi.h"
#include "atom.h"
#include "force.h"
#include "update.h"
#include "error.h"

using namespace LAMMPS_NS;
using namespace FixConst;

/* ---------------------------------------------------------------------- */

FixPI::FixPI(LAMMPS *lmp, int narg, char **arg) :
  Fix(lmp, narg, arg) {

  if ((atom->esph_flag != 1) || (atom->rho_flag != 1))
    error->all(FLERR,
        "Fix sph command requires atom_style with both energy and density");

  // Accept optional Oldroyd-B parameters:
  //   fix ID group pi                       (Newtonian, default)
  //   fix ID group pi tau                   (Oldroyd-B with G_p=1)
  //   fix ID group pi tau Gp                (Oldroyd-B with explicit G_p)
  if (narg < 3 || narg > 5)
    error->all(FLERR,"Illegal number of arguments for fix pi (expected 3..5)");

  tau_p = -1.0;   // <0 -> polymer evolution OFF
  Gp    = 1.0;
  if (narg >= 4) tau_p = utils::numeric(FLERR, arg[3], false, lmp);
  if (narg == 5) Gp    = utils::numeric(FLERR, arg[4], false, lmp);
  if (tau_p > 0.0 && Gp < 0.0)
    error->all(FLERR,"fix pi: polymer modulus Gp must be non-negative");

  time_integrate = 1;
}

/* ---------------------------------------------------------------------- */

int FixPI::setmask() {
  int mask = 0;
  mask |= INITIAL_INTEGRATE;
  mask |= FINAL_INTEGRATE;
  mask |= PRE_FORCE;
  return mask;
}

/* ---------------------------------------------------------------------- */

void FixPI::init() {
  dtv = update->dt;
  dtf = 0.5 * update->dt * force->ftm2v;
}

void FixPI::setup_pre_force(int /*vflag*/)
{
  // set vest equal to v
  double **v = atom->v;
  double **vest = atom->vest;
  int *mask = atom->mask;
  int nlocal = atom->nlocal;
  if (igroup == atom->firstgroup)
    nlocal = atom->nfirst;

  for (int i = 0; i < nlocal; i++) {
    if (mask[i] & groupbit) {
      vest[i][0] = v[i][0];
      vest[i][1] = v[i][1];
      vest[i][2] = v[i][2];
    }
  }
}

void FixPI::pre_force(int /*vflag*/)
{
  // set vest equal to v
 // double **stfluid = atom->stfluid;

//  double **gradv = atom->gradv;
  int *mask = atom->mask;
  int nlocal = atom->nlocal;
  if (igroup == atom->firstgroup)
    nlocal = atom->nfirst;

  //for (int i = 0; i < nlocal; i++) {
  //  if (mask[i] & groupbit) {
     // gradv[i][0] = 0.0;
     // gradv[i][1] = 0.0;
     // gradv[i][2] = 0.0;
     // gradv[i][3] = 0.0;
     // gradv[i][4] = 0.0;
     // gradv[i][5] = 0.0;
     // gradv[i][6] = 0.0;
      //gradv[i][7] = 0.0;
     // gradv[i][8] = 0.0;


    //}
  //}
}

/* ----------------------------------------------------------------------
 allow for both per-type and per-atom mass
 ------------------------------------------------------------------------- */

void FixPI::initial_integrate(int /*vflag*/) {
  // update v and x and rho and e of atoms in group
  double **gradv = atom->gradv;
 // double **stfluid = atom->stfluid;

  double **x = atom->x;
  double **v = atom->v;
  double **f = atom->f;
  double **vest = atom->vest;
  double *rho = atom->rho;
  double *drho = atom->drho;
  double *esph = atom->esph;
  double *desph = atom->desph;
  double *mass = atom->mass;
  double *rmass = atom->rmass;
  int rmass_flag = atom->rmass_flag;

  int *type = atom->type;
  int *mask = atom->mask;
  int nlocal = atom->nlocal;
  int i;
  double dtfm;

  if (igroup == atom->firstgroup)
    nlocal = atom->nfirst;

  for (i = 0; i < nlocal; i++) {
    if (mask[i] & groupbit) {
      if (rmass_flag) {
        dtfm = dtf / rmass[i];
      } else {
        dtfm = dtf / mass[type[i]];
      }

          //Only 2d to test
      // double trace;
       //trace = gradv[i][0]+gradv[i][1];//+gradv[i][0]
      // stfluid[i][0] =  0;//(2*gradv[i][0]+trace/2);
      // stfluid[i][1] =  0;//(2*gradv[i][1]+trace/2);
      // stfluid[i][2] =  0.0;//(gradv[i][2]-trace/2);
      // stfluid[i][3] =  (gradv[i][3]+gradv[i][6]);
     // stfluid[i][4] =  0.0;//0.5*(gradv[i][4]+gradv[i][7]);
     //  stfluid[i][5] =  0.0;//0.5*(gradv[i][5]+gradv[i][8]);

      gradv[i][0] = 0.0;
      gradv[i][1] = 0.0;
      gradv[i][2] = 0.0;
      gradv[i][3] = 0.0;
      gradv[i][4] = 0.0;
      gradv[i][5] = 0.0;
      gradv[i][6] = 0.0;
      gradv[i][7] = 0.0;
      gradv[i][8] = 0.0;

      esph[i] += dtf * desph[i]; // half-step update of particle internal energy
      rho[i] += dtf * drho[i]; // ... and density

      // extrapolate velocity for use with velocity-dependent potentials, e.g. SPH
      vest[i][0] = v[i][0] + 2.0 * dtfm * f[i][0];
      vest[i][1] = v[i][1] + 2.0 * dtfm * f[i][1];
      vest[i][2] = v[i][2] + 2.0 * dtfm * f[i][2];

      v[i][0] += dtfm * f[i][0];
      v[i][1] += dtfm * f[i][1];
      v[i][2] += dtfm * f[i][2];

      x[i][0] += dtv * v[i][0];
      x[i][1] += dtv * v[i][1];
      x[i][2] += dtv * v[i][2];
    }
  }
}

/* ---------------------------------------------------------------------- */

void FixPI::final_integrate() {
  double **stfluid = atom->stfluid;
  double **gradv = atom->gradv;
  double **stmic = atom->stmic;
  // update v, rho, and e of atoms in group

  double **v = atom->v;
  double **f = atom->f;
  double *esph = atom->esph;
  double *desph = atom->desph;
  double *rho = atom->rho;
  double *drho = atom->drho;
  int *type = atom->type;
  int *mask = atom->mask;
  double *mass = atom->mass;
  int nlocal = atom->nlocal;
  if (igroup == atom->firstgroup)
    nlocal = atom->nfirst;
  double dtfm;
  double *rmass = atom->rmass;
  int rmass_flag = atom->rmass_flag;

  for (int i = 0; i < nlocal; i++) {
    if (mask[i] & groupbit) {

      if (rmass_flag) {
        dtfm = dtf / rmass[i];
      } else {
        dtfm = dtf / mass[type[i]];
      }
      v[i][0] += dtfm * f[i][0];
      v[i][1] += dtfm * f[i][1];
      v[i][2] += dtfm * f[i][2];

      esph[i] += dtf * desph[i];
      rho[i]  += dtf * drho[i];

      // ---- Oldroyd-B polymer stress evolution (2D, Hookean dumbbell) ----
      // Storage in stmic[i]: [0]=sxx [1]=syy [2]=szz [3]=sxy [4]=sxz [5]=syz.
      // gradv[i] convention: [0]=du/dx [1]=dv/dy [2]=dw/dz
      //                       [3]=du/dy [4]=du/dz [5]=dv/dz
      //                       [6]=dv/dx [7]=dw/dx [8]=dw/dy
      // Eq:  d(sigma)/dt = Gp*(grad v + grad v^T) + grad v . sigma
      //                  + sigma . grad v^T - sigma/tau
      // For 2D we update sxx, syy, sxy only; the rest stay 0.
      if (tau_p > 0.0) {
        double sxx = stmic[i][0];
        double syy = stmic[i][1];
        double sxy = stmic[i][3];
        double duxx = gradv[i][0];
        double dvyy = gradv[i][1];
        double duxy = gradv[i][3];
        double dvyx = gradv[i][6];

        double dsxx = 2.0*Gp*duxx + 2.0*(sxx*duxx + sxy*duxy) - sxx/tau_p;
        double dsyy = 2.0*Gp*dvyy + 2.0*(sxy*dvyx + syy*dvyy) - syy/tau_p;
        double dsxy = Gp*(duxy + dvyx)
                    + sxx*dvyx + sxy*(duxx + dvyy) + syy*duxy
                    - sxy/tau_p;

        // forward Euler over full dt (= 2*dtf in LJ units, force->ftm2v=1)
        double dt_full = 2.0 * dtf / force->ftm2v;
        stmic[i][0] = sxx + dt_full * dsxx;
        stmic[i][1] = syy + dt_full * dsyy;
        stmic[i][3] = sxy + dt_full * dsxy;
      }
    }
  }
}

/* ---------------------------------------------------------------------- */

void FixPI::reset_dt() {
  dtv = update->dt;
  dtf = 0.5 * update->dt * force->ftm2v;
}