lammps-sys 0.6.0

Generates bindings to LAMMPS' C interface (with optional builds from source)
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/* ----------------------------------------------------------------------
   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.
------------------------------------------------------------------------- */

/* ----------------------------------------------------------------------
   Contributing authors: Mark Stevens (SNL), Paul Crozier (SNL)
------------------------------------------------------------------------- */

#include "respa.h"
#include <cstring>
#include "neighbor.h"
#include "atom.h"
#include "atom_vec.h"
#include "domain.h"
#include "comm.h"
#include "fix.h"
#include "force.h"
#include "pair.h"
#include "bond.h"
#include "angle.h"
#include "dihedral.h"
#include "improper.h"
#include "kspace.h"
#include "output.h"
#include "update.h"
#include "modify.h"
#include "fix_respa.h"
#include "timer.h"
#include "error.h"
#include "utils.h"
#include "pair_hybrid.h"

using namespace LAMMPS_NS;

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

Respa::Respa(LAMMPS *lmp, int narg, char **arg) :
  Integrate(lmp, narg, arg),
  step(NULL), loop(NULL), hybrid_level(NULL), hybrid_compute(NULL),
  newton(NULL), fix_respa(NULL)
{
  nhybrid_styles = 0;
  if (narg < 1) error->all(FLERR,"Illegal run_style respa command");

  nlevels = force->inumeric(FLERR,arg[0]);
  if (nlevels < 1) error->all(FLERR,"Respa levels must be >= 1");

  if (narg < nlevels) error->all(FLERR,"Illegal run_style respa command");
  loop = new int[nlevels];
  for (int iarg = 1; iarg < nlevels; iarg++) {
    loop[iarg-1] = force->inumeric(FLERR,arg[iarg]);
    if (loop[iarg-1] <= 0) error->all(FLERR,"Illegal run_style respa command");
  }
  loop[nlevels-1] = 1;

  // set level at which each force is computed
  // argument settings override defaults

  level_bond = level_angle = level_dihedral = level_improper = -1;
  level_pair = level_kspace = -1;
  level_inner = level_middle = level_outer = -1;

  // defaults for hybrid pair styles

  nhybrid_styles = 0;
  tally_global = 1;
  pair_compute = 1;

  int iarg = nlevels;
  while (iarg < narg) {
    if (strcmp(arg[iarg],"bond") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_bond = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"angle") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_angle = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"dihedral") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_dihedral = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"improper") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_improper = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"pair") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_pair = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"inner") == 0) {
      if (iarg+4 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_inner = force->inumeric(FLERR,arg[iarg+1]) - 1;
      cutoff[0] = force->numeric(FLERR,arg[iarg+2]);
      cutoff[1] = force->numeric(FLERR,arg[iarg+3]);
      iarg += 4;
    } else if (strcmp(arg[iarg],"middle") == 0) {
      if (iarg+4 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_middle = force->inumeric(FLERR,arg[iarg+1]) - 1;
      cutoff[2] = force->numeric(FLERR,arg[iarg+2]);
      cutoff[3] = force->numeric(FLERR,arg[iarg+3]);
      iarg += 4;
    } else if (strcmp(arg[iarg],"outer") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_outer = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"kspace") == 0) {
      if (iarg+2 > narg) error->all(FLERR,"Illegal run_style respa command");
      level_kspace = force->inumeric(FLERR,arg[iarg+1]) - 1;
      iarg += 2;
    } else if (strcmp(arg[iarg],"hybrid") == 0) {
      // the hybrid keyword requires a hybrid pair style
      if (!utils::strmatch(force->pair_style,"^hybrid"))
        error->all(FLERR,"Illegal run_style respa command");
      PairHybrid *hybrid = (PairHybrid *) force->pair;
      nhybrid_styles = hybrid->nstyles;
      // each hybrid sub-style needs to be assigned to a respa level
      if (iarg+nhybrid_styles > narg)
        error->all(FLERR,"Illegal run_style respa command");
      hybrid_level = new int[nhybrid_styles];
      hybrid_compute = new int[nhybrid_styles];
      for (int i=0; i < nhybrid_styles; ++i) {
        ++iarg;
        hybrid_level[i] = force->inumeric(FLERR,arg[iarg])-1;
      }
      ++iarg;
    } else error->all(FLERR,"Illegal run_style respa command");
  }

  // cannot specify both pair and inner/middle/outer

  if (level_pair >= 0 &&
      (level_inner >= 0 || level_middle >= 0 || level_outer >= 0))
    error->all(FLERR,"Cannot set both respa pair and inner/middle/outer");

  // if either inner and outer is specified, then both must be

  if ((level_inner >= 0 && level_outer == -1) ||
      (level_outer >= 0 && level_inner == -1))
    error->all(FLERR,"Must set both respa inner and outer");

  // middle cannot be set without inner/outer

  if (level_middle >= 0 && level_inner == -1)
    error->all(FLERR,"Cannot set respa middle without inner/outer");

  // cannot combine hybrid with any of pair/inner/middle/outer

  if ((nhybrid_styles > 0) && (level_pair >= 0 || level_inner >= 0
                               || level_middle >= 0 || level_outer >= 0))
    error->all(FLERR,"Cannot set respa hybrid and "
               "any of pair/inner/middle/outer");

  // set defaults if user did not specify level
  // bond to innermost level
  // angle same as bond, dihedral same as angle, improper same as dihedral
  // pair to outermost level if no inner/middle/outer
  // inner/middle/outer have no defaults
  // kspace same as pair or outer

  if (level_bond == -1) level_bond = 0;
  if (level_angle == -1) level_angle = level_bond;
  if (level_dihedral == -1) level_dihedral = level_angle;
  if (level_improper == -1) level_improper = level_dihedral;

  if (level_pair == -1 && level_inner == -1 && nhybrid_styles < 1)
    level_pair = nlevels-1;

  if (level_kspace == -1 && level_pair >= 0) level_kspace = level_pair;
  if (level_kspace == -1 && level_pair == -1) {
    if (nhybrid_styles < 1) {
      level_kspace = level_outer;
    } else {
      int max_hybrid_level = -1;
      for (int i=0; i < nhybrid_styles; ++i) {
        if (max_hybrid_level < hybrid_level[i])
          max_hybrid_level = hybrid_level[i];
      }
      level_kspace = max_hybrid_level;
    }
  }

  // print respa levels

  if (comm->me == 0) {
    if (screen) {
      fprintf(screen,"Respa levels:\n");
      for (int i = 0; i < nlevels; i++) {
        fprintf(screen,"  %d =",i+1);
        if (level_bond == i) fprintf(screen," bond");
        if (level_angle == i) fprintf(screen," angle");
        if (level_dihedral == i) fprintf(screen," dihedral");
        if (level_improper == i) fprintf(screen," improper");
        if (level_pair == i) fprintf(screen," pair");
        if (level_inner == i) fprintf(screen," pair-inner");
        if (level_middle == i) fprintf(screen," pair-middle");
        if (level_outer == i) fprintf(screen," pair-outer");
        for (int j=0;j<nhybrid_styles;j++) {
          if (hybrid_level[j] == i) fprintf(screen, " hybrid-%d",j+1);
        }
        if (level_kspace == i) fprintf(screen," kspace");
        fprintf(screen,"\n");
      }
    }
    if (logfile) {
      fprintf(logfile,"Respa levels:\n");
      for (int i = 0; i < nlevels; i++) {
        fprintf(logfile,"  %d =",i+1);
        if (level_bond == i) fprintf(logfile," bond");
        if (level_angle == i) fprintf(logfile," angle");
        if (level_dihedral == i) fprintf(logfile," dihedral");
        if (level_improper == i) fprintf(logfile," improper");
        if (level_pair == i) fprintf(logfile," pair");
        if (level_inner == i) fprintf(logfile," pair-inner");
        if (level_middle == i) fprintf(logfile," pair-middle");
        if (level_outer == i) fprintf(logfile," pair-outer");
        for (int j=0;j<nhybrid_styles;j++) {
          if (hybrid_level[j] == i) fprintf(logfile, " hybrid-%d",j+1);
        }
        if (level_kspace == i) fprintf(logfile," kspace");
        fprintf(logfile,"\n");
      }
    }
  }

  // check that levels are in correct order

  if (level_angle < level_bond || level_dihedral < level_angle ||
      level_improper < level_dihedral)
    error->all(FLERR,"Invalid order of forces within respa levels");
  if (level_pair >= 0) {
    if (level_pair < level_improper || level_kspace < level_pair)
      error->all(FLERR,"Invalid order of forces within respa levels");
  }
  if (level_pair == -1 && level_middle == -1 && nhybrid_styles < 1) {
    if (level_inner < level_improper || level_outer < level_inner ||
        level_kspace < level_outer)
      error->all(FLERR,"Invalid order of forces within respa levels");
  }
  if (level_pair == -1 && level_middle >= 0) {
    if (level_inner < level_improper || level_middle < level_inner ||
        level_outer < level_inner || level_kspace < level_outer)
      error->all(FLERR,"Invalid order of forces within respa levels");
  }

  // warn if any levels are devoid of forces

  int flag = 0;
  for (int i = 0; i < nlevels; i++)
    if (level_bond != i && level_angle != i && level_dihedral != i &&
        level_improper != i && level_pair != i && level_inner != i &&
        level_middle != i && level_outer != i && level_kspace != i) flag = 1;
  if (flag && comm->me == 0)
    error->warning(FLERR,"One or more respa levels compute no forces");

  // check cutoff consistency if inner/middle/outer are enabled

  if (level_inner >= 0 && cutoff[1] < cutoff[0])
    error->all(FLERR,"Respa inner cutoffs are invalid");
  if (level_middle >= 0 && (cutoff[3] < cutoff[2] || cutoff[2] < cutoff[1]))
    error->all(FLERR,"Respa middle cutoffs are invalid");

  // set outer pair of cutoffs to inner pair if middle is not enabled

  if (level_inner >= 0 && level_middle < 0) {
    cutoff[2] = cutoff[0];
    cutoff[3] = cutoff[1];
  }

  // ensure that pair->compute() is run properly
  // when the hybrid keyword is not used

  if (nhybrid_styles < 1) {
    pair_compute = 1;
    tally_global = 1;
  }

  // allocate other needed arrays

  newton = new int[nlevels];
  step = new double[nlevels];
}

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

Respa::~Respa()
{
  delete [] loop;
  delete [] newton;
  delete [] step;
  if (nhybrid_styles > 0) {
    delete [] hybrid_level;
    delete [] hybrid_compute;
  }
}

/* ----------------------------------------------------------------------
   initialization before run
------------------------------------------------------------------------- */

void Respa::init()
{
  Integrate::init();

  // warn if no fixes

  if (modify->nfix == 0 && comm->me == 0)
    error->warning(FLERR,"No fixes defined, atoms won't move");

  // create fix needed for storing atom-based respa level forces
  // will delete it at end of run

  char **fixarg = new char*[5];
  fixarg[0] = (char *) "RESPA";
  fixarg[1] = (char *) "all";
  fixarg[2] = (char *) "RESPA";
  fixarg[3] = new char[8];
  sprintf(fixarg[3],"%d",nlevels);
  // if supported, we also store torques on a per-level basis
  if (atom->torque_flag) {
    fixarg[4] = (char *) "torque";
    modify->add_fix(5,fixarg);
  } else {
    modify->add_fix(4,fixarg);
  }
  delete [] fixarg[3];
  delete [] fixarg;
  fix_respa = (FixRespa *) modify->fix[modify->nfix-1];

  // insure respa inner/middle/outer is using Pair class that supports it

  if (level_inner >= 0)
    if (force->pair && force->pair->respa_enable == 0)
      error->all(FLERR,"Pair style does not support rRESPA inner/middle/outer");

  // virial_style = 1 (explicit) since never computed implicitly like Verlet

  virial_style = 1;

  // setup lists of computes for global and per-atom PE and pressure

  ev_setup();

  // detect if fix omp is present and will clear force arrays

  int ifix = modify->find_fix("package_omp");
  if (ifix >= 0) external_force_clear = 1;

  // set flags for arrays to clear in force_clear()

  torqueflag = extraflag = 0;
  if (atom->torque_flag) torqueflag = 1;
  if (atom->avec->forceclearflag) extraflag = 1;

  // step[] = timestep for each level

  step[nlevels-1] = update->dt;
  for (int ilevel = nlevels-2; ilevel >= 0; ilevel--)
    step[ilevel] = step[ilevel+1]/loop[ilevel];

  // set newton flag for each level

  for (int ilevel = 0; ilevel < nlevels; ilevel++) {
    newton[ilevel] = 0;
    if (force->newton_bond) {
      if (level_bond == ilevel || level_angle == ilevel ||
          level_dihedral == ilevel || level_improper == ilevel)
        newton[ilevel] = 1;
    }
    if (force->newton_pair) {
      if (level_pair == ilevel || level_inner == ilevel ||
          level_middle == ilevel || level_outer == ilevel)
        newton[ilevel] = 1;

      if (nhybrid_styles > 0) {
        set_compute_flags(ilevel);
        if (pair_compute) newton[ilevel] = 1;
      }
    }
  }

  // orthogonal vs triclinic simulation box

  triclinic = domain->triclinic;
}

/* ----------------------------------------------------------------------
   setup before run
------------------------------------------------------------------------- */

void Respa::setup(int flag)
{
  if (comm->me == 0 && screen) {
    fprintf(screen,"Setting up r-RESPA run ...\n");
    if (flag) {
      fprintf(screen,"  Unit style    : %s\n", update->unit_style);
      fprintf(screen,"  Current step  : " BIGINT_FORMAT "\n",
              update->ntimestep);
      fprintf(screen,"  Time steps    :");
      for (int ilevel=0; ilevel < nlevels; ++ilevel)
        fprintf(screen," %d:%g",ilevel+1, step[ilevel]);
      fprintf(screen,"\n  r-RESPA fixes :");
      for (int l=0; l < modify->n_post_force_respa; ++l) {
        Fix *f = modify->fix[modify->list_post_force_respa[l]];
        if (f->respa_level >= 0)
          fprintf(screen," %d:%s[%s]",
                  MIN(f->respa_level+1,nlevels),f->style,f->id);
      }
      fprintf(screen,"\n");
      timer->print_timeout(screen);
    }
  }

  update->setupflag = 1;

  // setup domain, communication and neighboring
  // acquire ghosts
  // build neighbor lists

  atom->setup();
  modify->setup_pre_exchange();
  if (triclinic) domain->x2lamda(atom->nlocal);
  domain->pbc();
  domain->reset_box();
  comm->setup();
  if (neighbor->style) neighbor->setup_bins();
  comm->exchange();
  if (atom->sortfreq > 0) atom->sort();
  comm->borders();
  if (triclinic) domain->lamda2x(atom->nlocal+atom->nghost);
  domain->image_check();
  domain->box_too_small_check();
  modify->setup_pre_neighbor();
  neighbor->build(1);
  modify->setup_post_neighbor();
  neighbor->ncalls = 0;

  // compute all forces

  force->setup();
  ev_set(update->ntimestep);

  for (int ilevel = 0; ilevel < nlevels; ilevel++) {
    force_clear(newton[ilevel]);
    modify->setup_pre_force_respa(vflag,ilevel);

    if (nhybrid_styles > 0) {
      set_compute_flags(ilevel);
      force->pair->compute(eflag,vflag);
    }
    if (level_pair == ilevel && pair_compute_flag)
      force->pair->compute(eflag,vflag);
    if (level_inner == ilevel && pair_compute_flag)
      force->pair->compute_inner();
    if (level_middle == ilevel && pair_compute_flag)
      force->pair->compute_middle();
    if (level_outer == ilevel && pair_compute_flag)
      force->pair->compute_outer(eflag,vflag);
    if (level_bond == ilevel && force->bond)
      force->bond->compute(eflag,vflag);
    if (level_angle == ilevel && force->angle)
      force->angle->compute(eflag,vflag);
    if (level_dihedral == ilevel && force->dihedral)
      force->dihedral->compute(eflag,vflag);
    if (level_improper == ilevel && force->improper)
      force->improper->compute(eflag,vflag);
    if (level_kspace == ilevel && force->kspace) {
      force->kspace->setup();
      if (kspace_compute_flag) force->kspace->compute(eflag,vflag);
    }

    modify->setup_pre_reverse(eflag,vflag);
    if (newton[ilevel]) comm->reverse_comm();
    copy_f_flevel(ilevel);
  }

  sum_flevel_f();
  modify->setup(vflag);
  output->setup(flag);
  update->setupflag = 0;
}

/* ----------------------------------------------------------------------
   setup without output
   flag = 0 = just force calculation
   flag = 1 = reneighbor and force calculation
------------------------------------------------------------------------- */

void Respa::setup_minimal(int flag)
{
  update->setupflag = 1;

  // setup domain, communication and neighboring
  // acquire ghosts
  // build neighbor lists

  if (flag) {
    modify->setup_pre_exchange();
    if (triclinic) domain->x2lamda(atom->nlocal);
    domain->pbc();
    domain->reset_box();
    comm->setup();
    if (neighbor->style) neighbor->setup_bins();
    comm->exchange();
    comm->borders();
    if (triclinic) domain->lamda2x(atom->nlocal+atom->nghost);
    domain->image_check();
    domain->box_too_small_check();
    modify->setup_pre_neighbor();
    neighbor->build(1);
    modify->setup_post_neighbor();
    neighbor->ncalls = 0;
  }

  // compute all forces

  ev_set(update->ntimestep);

  for (int ilevel = 0; ilevel < nlevels; ilevel++) {
    force_clear(newton[ilevel]);
    modify->setup_pre_force_respa(vflag,ilevel);

    if (nhybrid_styles > 0) {
      set_compute_flags(ilevel);
      force->pair->compute(eflag,vflag);
    }

    if (level_pair == ilevel && pair_compute_flag)
      force->pair->compute(eflag,vflag);
    if (level_inner == ilevel && pair_compute_flag)
      force->pair->compute_inner();
    if (level_middle == ilevel && pair_compute_flag)
      force->pair->compute_middle();
    if (level_outer == ilevel && pair_compute_flag)
      force->pair->compute_outer(eflag,vflag);
    if (level_bond == ilevel && force->bond)
      force->bond->compute(eflag,vflag);
    if (level_angle == ilevel && force->angle)
      force->angle->compute(eflag,vflag);
    if (level_dihedral == ilevel && force->dihedral)
      force->dihedral->compute(eflag,vflag);
    if (level_improper == ilevel && force->improper)
      force->improper->compute(eflag,vflag);
    if (level_kspace == ilevel && force->kspace) {
      force->kspace->setup();
      if (kspace_compute_flag) force->kspace->compute(eflag,vflag);
    }

    modify->setup_pre_reverse(eflag,vflag);
    if (newton[ilevel]) comm->reverse_comm();
    copy_f_flevel(ilevel);
  }

  sum_flevel_f();
  modify->setup(vflag);
  update->setupflag = 0;
}

/* ----------------------------------------------------------------------
   run for N steps
------------------------------------------------------------------------- */

void Respa::run(int n)
{
  bigint ntimestep;

  for (int i = 0; i < n; i++) {
    if (timer->check_timeout(i)) {
      update->nsteps = i;
      break;
    }

    ntimestep = ++update->ntimestep;
    ev_set(ntimestep);

    recurse(nlevels-1);

    // needed in case end_of_step() or output() use total force

    sum_flevel_f();

    if (modify->n_end_of_step) {
      timer->stamp();
      modify->end_of_step();
      timer->stamp(Timer::MODIFY);
    }

    if (ntimestep == output->next) {
      timer->stamp();
      output->write(update->ntimestep);
      timer->stamp(Timer::OUTPUT);
    }
  }
}

/* ----------------------------------------------------------------------
   delete rRESPA fix at end of run, so its atom arrays won't persist
------------------------------------------------------------------------- */

void Respa::cleanup()
{
  modify->post_run();
  modify->delete_fix("RESPA");
  domain->box_too_small_check();
  update->update_time();
}

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

void Respa::reset_dt()
{
  step[nlevels-1] = update->dt;
  for (int ilevel = nlevels-2; ilevel >= 0; ilevel--)
    step[ilevel] = step[ilevel+1]/loop[ilevel];
}

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

void Respa::recurse(int ilevel)
{
  copy_flevel_f(ilevel);

  for (int iloop = 0; iloop < loop[ilevel]; iloop++) {

    timer->stamp();
    modify->initial_integrate_respa(vflag,ilevel,iloop);
    if (modify->n_post_integrate_respa)
      modify->post_integrate_respa(ilevel,iloop);
    timer->stamp(Timer::MODIFY);

    // at outermost level, check on rebuilding neighbor list
    // at innermost level, communicate
    // at middle levels, do nothing

    if (ilevel == nlevels-1) {
      int nflag = neighbor->decide();
      if (nflag) {
        if (modify->n_pre_exchange) {
          timer->stamp();
          modify->pre_exchange();
          timer->stamp(Timer::MODIFY);
        }
        if (triclinic) domain->x2lamda(atom->nlocal);
        domain->pbc();
        if (domain->box_change) {
          domain->reset_box();
          comm->setup();
          if (neighbor->style) neighbor->setup_bins();
        }
        timer->stamp();
        comm->exchange();
        if (atom->sortfreq > 0 &&
            update->ntimestep >= atom->nextsort) atom->sort();
        comm->borders();
        if (triclinic) domain->lamda2x(atom->nlocal+atom->nghost);
        timer->stamp(Timer::COMM);
        if (modify->n_pre_neighbor) {
          modify->pre_neighbor();
          timer->stamp(Timer::MODIFY);
        }
        neighbor->build(1);
        timer->stamp(Timer::NEIGH);
        if (modify->n_post_neighbor) {
          modify->post_neighbor();
          timer->stamp(Timer::MODIFY);
        }

      } else if (ilevel == 0) {
        timer->stamp();
        comm->forward_comm();
        timer->stamp(Timer::COMM);
      }

    } else if (ilevel == 0) {
      timer->stamp();
      comm->forward_comm();
      timer->stamp(Timer::COMM);
    }

    // rRESPA recursion thru all levels
    // this used to be before neigh list build,
    // which prevented per-atom energy/stress being tallied correctly
    // b/c atoms migrated to new procs between short/long force calls
    // now they migrate at very start of rRESPA timestep, before all forces

    if (ilevel) recurse(ilevel-1);

    // force computations
    // important that ordering is same as Verlet
    // so that any order dependencies are the same
    // when potentials are invoked at same level

    force_clear(newton[ilevel]);
    if (modify->n_pre_force_respa) {
      timer->stamp();
      modify->pre_force_respa(vflag,ilevel,iloop);
      timer->stamp(Timer::MODIFY);
    }

    timer->stamp();
    if (nhybrid_styles > 0) {
      set_compute_flags(ilevel);
      force->pair->compute(eflag,vflag);
      timer->stamp(Timer::PAIR);
    }
    if (level_pair == ilevel && pair_compute_flag) {
      force->pair->compute(eflag,vflag);
      timer->stamp(Timer::PAIR);
    }
    if (level_inner == ilevel && pair_compute_flag) {
      force->pair->compute_inner();
      timer->stamp(Timer::PAIR);
    }
    if (level_middle == ilevel && pair_compute_flag) {
      force->pair->compute_middle();
      timer->stamp(Timer::PAIR);
    }
    if (level_outer == ilevel && pair_compute_flag) {
      force->pair->compute_outer(eflag,vflag);
      timer->stamp(Timer::PAIR);
    }
    if (level_bond == ilevel && force->bond) {
      force->bond->compute(eflag,vflag);
      timer->stamp(Timer::BOND);
    }
    if (level_angle == ilevel && force->angle) {
      force->angle->compute(eflag,vflag);
      timer->stamp(Timer::BOND);
    }
    if (level_dihedral == ilevel && force->dihedral) {
      force->dihedral->compute(eflag,vflag);
      timer->stamp(Timer::BOND);
    }
    if (level_improper == ilevel && force->improper) {
      force->improper->compute(eflag,vflag);
      timer->stamp(Timer::BOND);
    }
    if (level_kspace == ilevel && kspace_compute_flag) {
      force->kspace->compute(eflag,vflag);
      timer->stamp(Timer::KSPACE);
    }

    if (modify->n_pre_reverse) {
      modify->pre_reverse(eflag,vflag);
      timer->stamp(Timer::MODIFY);
    }

    if (newton[ilevel]) {
      comm->reverse_comm();
      timer->stamp(Timer::COMM);
    }
    timer->stamp();
    if (modify->n_post_force_respa)
      modify->post_force_respa(vflag,ilevel,iloop);
    modify->final_integrate_respa(ilevel,iloop);
    timer->stamp(Timer::MODIFY);
  }

  copy_f_flevel(ilevel);
}

/* ----------------------------------------------------------------------
   clear force on own & ghost atoms
   clear other arrays as needed
------------------------------------------------------------------------- */

void Respa::force_clear(int /*newtonflag*/)
{
  if (external_force_clear) return;

  // clear global force array
  // if either newton flag is set, also include ghosts

  size_t nbytes = sizeof(double) * atom->nlocal;
  if (force->newton) nbytes += sizeof(double) * atom->nghost;

  if (nbytes) {
    memset(&atom->f[0][0],0,3*nbytes);
    if (torqueflag) memset(&atom->torque[0][0],0,3*nbytes);
    if (extraflag) atom->avec->force_clear(0,nbytes);
  }
}

/* ----------------------------------------------------------------------
   copy force components from atom->f to FixRespa->f_level
------------------------------------------------------------------------- */

void Respa::copy_f_flevel(int ilevel)
{
  double ***f_level = fix_respa->f_level;
  double **f = atom->f;
  double ***t_level = fix_respa->t_level;
  double **t = atom->torque;
  int n = atom->nlocal;

  for (int i = 0; i < n; i++) {
    f_level[i][ilevel][0] = f[i][0];
    f_level[i][ilevel][1] = f[i][1];
    f_level[i][ilevel][2] = f[i][2];
    if (fix_respa->store_torque) {
      t_level[i][ilevel][0] = t[i][0];
      t_level[i][ilevel][1] = t[i][1];
      t_level[i][ilevel][2] = t[i][2];
    }
  }
}

/* ----------------------------------------------------------------------
   copy force components from FixRespa->f_level to atom->f
------------------------------------------------------------------------- */

void Respa::copy_flevel_f(int ilevel)
{
  double ***f_level = fix_respa->f_level;
  double **f = atom->f;
  double ***t_level = fix_respa->t_level;
  double **t = atom->torque;
  int n = atom->nlocal;

  for (int i = 0; i < n; i++) {
    f[i][0] = f_level[i][ilevel][0];
    f[i][1] = f_level[i][ilevel][1];
    f[i][2] = f_level[i][ilevel][2];
    if (fix_respa->store_torque) {
      t[i][0] = t_level[i][ilevel][0];
      t[i][1] = t_level[i][ilevel][1];
      t[i][2] = t_level[i][ilevel][2];
    }
  }
}

/* ----------------------------------------------------------------------
   sum all force components from FixRespa->f_level to create full atom->f
------------------------------------------------------------------------- */

void Respa::sum_flevel_f()
{
  copy_flevel_f(0);

  double ***f_level = fix_respa->f_level;
  double **f = atom->f;
  double ***t_level = fix_respa->t_level;
  double **t = atom->torque;
  int n = atom->nlocal;

  for (int ilevel = 1; ilevel < nlevels; ilevel++) {
    for (int i = 0; i < n; i++) {
      f[i][0] += f_level[i][ilevel][0];
      f[i][1] += f_level[i][ilevel][1];
      f[i][2] += f_level[i][ilevel][2];
      if (fix_respa->store_torque) {
        t[i][0] += t_level[i][ilevel][0];
        t[i][1] += t_level[i][ilevel][1];
        t[i][2] += t_level[i][ilevel][2];
      }
    }
  }
}

/*-----------------------------------------------------------------------
  set flags for when some hybrid forces should be computed
------------------------------------------------------------------------- */

void Respa::set_compute_flags(int ilevel)
{

  if (nhybrid_styles < 1) return;

  pair_compute = 0;
  for (int i=0; i<nhybrid_styles; ++i) {
    hybrid_compute[i] = (hybrid_level[i] == ilevel) ? 1 : 0;
    if (hybrid_compute[i]) pair_compute = 1;
  }
  tally_global = (ilevel == nlevels-1) ? 1 : 0;
}