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
/* ----------------------------------------------------------------------
   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: Julien Tranchida (SNL)

   Please cite the related publication:
   Bessarab, P. F., Uzdin, V. M., & Jónsson, H. (2015).
   Method for finding mechanism and activation energy of magnetic transitions,
   applied to skyrmion and antivortex annihilation.
   Computer Physics Communications, 196, 335-347.
------------------------------------------------------------------------- */

#include "neb_spin.h"
#include <mpi.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include "citeme.h"
#include "force.h"
#include "universe.h"
#include "atom.h"
#include "update.h"
#include "domain.h"
#include "comm.h"
#include "min.h"
#include "modify.h"
#include "fix.h"
#include "fix_neb_spin.h"
#include "output.h"
#include "thermo.h"
#include "finish.h"
#include "timer.h"
#include "memory.h"
#include "error.h"

using namespace LAMMPS_NS;

static const char cite_neb_spin[] =
  "neb/spin command:\n\n"
  "@article{bessarab2015method,\n"
  "title={Method for finding mechanism and activation energy of "
  "magnetic transitions, applied to skyrmion and antivortex "
  "annihilation},\n"
  "author={Bessarab, P.F. and Uzdin, V.M. and J{\'o}nsson, H.},\n"
  "journal={Computer Physics Communications},\n"
  "volume={196},\n"
  "pages={335--347},\n"
  "year={2015},\n"
  "publisher={Elsevier}\n"
  "doi={10.1016/j.cpc.2015.07.001}\n"
  "}\n\n";

#define MAXLINE 256
#define CHUNK 1024
// 8 attributes: tag, spin norm, position (3), spin direction (3)
#define ATTRIBUTE_PERLINE 8

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

NEBSpin::NEBSpin(LAMMPS *lmp) : Pointers(lmp) {
  if (lmp->citeme) lmp->citeme->add(cite_neb_spin);
}

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

NEBSpin::~NEBSpin()
{
  MPI_Comm_free(&roots);
  memory->destroy(all);
  delete [] rdist;
}

/* ----------------------------------------------------------------------
   perform NEBSpin on multiple replicas
------------------------------------------------------------------------- */

void NEBSpin::command(int narg, char **arg)
{
  if (domain->box_exist == 0)
    error->all(FLERR,"NEBSpin command before simulation box is defined");

  if (narg < 6) error->universe_all(FLERR,"Illegal NEBSpin command");

  etol = force->numeric(FLERR,arg[0]);
  ttol = force->numeric(FLERR,arg[1]);
  n1steps = force->inumeric(FLERR,arg[2]);
  n2steps = force->inumeric(FLERR,arg[3]);
  nevery = force->inumeric(FLERR,arg[4]);

  // error checks

  if (etol < 0.0) error->all(FLERR,"Illegal NEBSpin command");
  if (ttol < 0.0) error->all(FLERR,"Illegal NEBSpin command");
  if (nevery <= 0) error->universe_all(FLERR,"Illegal NEBSpin command");
  if (n1steps % nevery || n2steps % nevery)
    error->universe_all(FLERR,"Illegal NEBSpin command");

  // replica info

  nreplica = universe->nworlds;
  ireplica = universe->iworld;
  me_universe = universe->me;
  uworld = universe->uworld;
  MPI_Comm_rank(world,&me);

  // check metal units and spin atom/style

  if (!atom->sp_flag)
    error->all(FLERR,"neb/spin requires atom/spin style");
  if (strcmp(update->unit_style,"metal") != 0)
    error->all(FLERR,"neb/spin simulation requires metal unit style");

  // error checks

  if (nreplica == 1) error->all(FLERR,"Cannot use NEBSpin with a single replica");
  if (atom->map_style == 0)
    error->all(FLERR,"Cannot use NEBSpin unless atom map exists");

  // process file-style setting to setup initial configs for all replicas

  if (strcmp(arg[5],"final") == 0) {
    if (narg != 7 && narg !=8) error->universe_all(FLERR,"Illegal NEBSpin command");
    inpfile = arg[6];
    readfile(inpfile,0);
  } else if (strcmp(arg[5],"each") == 0) {
    if (narg != 7 && narg !=8) error->universe_all(FLERR,"Illegal NEBSpin command");
    inpfile = arg[6];
    readfile(inpfile,1);
  } else if (strcmp(arg[5],"none") == 0) {
    if (narg != 6 && narg !=7) error->universe_all(FLERR,"Illegal NEBSpin command");
  } else error->universe_all(FLERR,"Illegal NEBSpin command");

  verbose=false;
  if (strcmp(arg[narg-1],"verbose") == 0) verbose=true;

  run();
}

/* ----------------------------------------------------------------------
   run NEBSpin on multiple replicas
------------------------------------------------------------------------- */

void NEBSpin::run()
{
  // create MPI communicator for root proc from each world

  int color;
  if (me == 0) color = 0;
  else color = 1;
  MPI_Comm_split(uworld,color,0,&roots);

  // search for neb_spin fix, allocate it

  int ineb;
  for (ineb = 0; ineb < modify->nfix; ineb++)
    if (strcmp(modify->fix[ineb]->style,"neb/spin") == 0) break;
  if (ineb == modify->nfix) error->all(FLERR,"NEBSpin requires use of fix neb/spin");

  fneb = (FixNEBSpin *) modify->fix[ineb];
  if (verbose) numall =7;
  else  numall = 4;
  memory->create(all,nreplica,numall,"neb:all");
  rdist = new double[nreplica];

  // initialize LAMMPS

  update->whichflag = 2;
  update->etol = etol;
  update->ftol = ttol;		// update->ftol is a torque tolerance
  update->multireplica = 1;

  lmp->init();

  // check if correct minimizer is setup

  if (update->minimize->searchflag)
    error->all(FLERR,"NEBSpin requires damped dynamics minimizer");
  if (strcmp(update->minimize_style,"spin") != 0)
    error->all(FLERR,"NEBSpin requires spin minimizer");

  // setup regular NEBSpin minimization

  FILE *uscreen = universe->uscreen;
  FILE *ulogfile = universe->ulogfile;

  if (me_universe == 0 && uscreen)
    fprintf(uscreen,"Setting up regular NEBSpin ...\n");

  update->beginstep = update->firststep = update->ntimestep;
  update->endstep = update->laststep = update->firststep + n1steps;
  update->nsteps = n1steps;
  update->max_eval = n1steps;
  if (update->laststep < 0)
    error->all(FLERR,"Too many timesteps for NEBSpin");

  update->minimize->setup();

  if (me_universe == 0) {
    if (uscreen) {
      if (verbose) {
        fprintf(uscreen,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc EBF EBR RDT "
                "RD1 PE1 RD2 PE2 ... RDN PEN "
		"GradV0dottan DN0 ... GradVNdottan DNN\n");
      } else {
        fprintf(uscreen,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc EBF EBR RDT RD1 PE1 RD2 PE2 ... "
                "RDN PEN\n");
      }
    }

    if (ulogfile) {
      if (verbose) {
	fprintf(ulogfile,"Step MaxReplicaTorque MaxAtomTorque "
	    "GradV0 GradV1 GradVc EBF EBR RDT "
	    "RD1 PE1 RD2 PE2 ... RDN PEN "
	    "GradV0dottan DN0 ... GradVNdottan DNN\n");
      } else {
        fprintf(ulogfile,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc EBF EBR RDT RD1 PE1 RD2 PE2 ... "
                "RDN PEN\n");
      }
    }
  }
  print_status();

  // perform regular NEBSpin for n1steps or until replicas converge
  // retrieve PE values from fix NEBSpin and print every nevery iterations
  // break out of while loop early if converged
  // damped dynamic min styles insure all replicas converge together

  timer->init();
  timer->barrier_start();

  while (update->minimize->niter < n1steps) {
    update->minimize->run(nevery);
    print_status();
    if (update->minimize->stop_condition) break;
  }

  timer->barrier_stop();

  update->minimize->cleanup();

  Finish finish(lmp);
  finish.end(1);

  // switch fix NEBSpin to climbing mode
  // top = replica that becomes hill climber

  double vmax = all[0][0];
  int top = 0;
  for (int m = 1; m < nreplica; m++)
    if (vmax < all[m][0]) {
      vmax = all[m][0];
      top = m;
    }

  // setup climbing NEBSpin minimization
  // must reinitialize minimizer so it re-creates its fix MINIMIZE

  if (me_universe == 0 && uscreen)
    fprintf(uscreen,"Setting up climbing ...\n");

  if (me_universe == 0) {
    if (uscreen)
      fprintf(uscreen,"Climbing replica = %d\n",top+1);
    if (ulogfile)
      fprintf(ulogfile,"Climbing replica = %d\n",top+1);
  }

  update->beginstep = update->firststep = update->ntimestep;
  update->endstep = update->laststep = update->firststep + n2steps;
  update->nsteps = n2steps;
  update->max_eval = n2steps;
  if (update->laststep < 0)
    error->all(FLERR,"Too many timesteps");

  update->minimize->init();
  fneb->rclimber = top;
  update->minimize->setup();

  if (me_universe == 0) {
    if (uscreen) {
      if (verbose) {
        fprintf(uscreen,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc EBF EBR RDT "
                "RD1 PE1 RD2 PE2 ... RDN PEN "
		"GradV0dottan DN0... GradVNdottan DNN\n");
      } else {
        fprintf(uscreen,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc "
                "EBF EBR RDT "
                "RD1 PE1 RD2 PE2 ... RDN PEN\n");
      }
    }
    if (ulogfile) {
      if (verbose) {
	fprintf(ulogfile,"Step MaxReplicaTorque MaxAtomTorque "
	    "GradV0 GradV1 GradVc EBF EBR RDT "
	    "RD1 PE1 RD2 PE2 ... RDN PEN "
	    "GradV0dottan DN0 ... GradVNdottan DNN\n");
      } else {
        fprintf(ulogfile,"Step MaxReplicaTorque MaxAtomTorque "
                "GradV0 GradV1 GradVc "
                "EBF EBR RDT "
                "RD1 PE1 RD2 PE2 ... RDN PEN\n");
      }
    }
  }
  print_status();

  // perform climbing NEBSpin for n2steps or until replicas converge
  // retrieve PE values from fix NEBSpin and print every nevery iterations
  // break induced if converged
  // damped dynamic min styles insure all replicas converge together

  timer->init();
  timer->barrier_start();

  while (update->minimize->niter < n2steps) {
    update->minimize->run(nevery);
    print_status();
    if (update->minimize->stop_condition) break;
  }

  timer->barrier_stop();

  update->minimize->cleanup();

  finish.end(1);

  update->whichflag = 0;
  update->multireplica = 0;
  update->firststep = update->laststep = 0;
  update->beginstep = update->endstep = 0;
}

/* ----------------------------------------------------------------------
   read initial config atom coords from file
   flag = 0
   only first replica opens file and reads it
   first replica bcasts lines to all replicas
   final replica stores coords
   intermediate replicas interpolate from coords
   new coord = replica fraction between current and final state
   initial replica does nothing
   flag = 1
   each replica (except first) opens file and reads it
   each replica stores coords
   initial replica does nothing
------------------------------------------------------------------------- */

void NEBSpin::readfile(char *file, int flag)
{
  int i,j,m,nchunk,eofflag,nlines;
  tagint tag;
  char *eof,*start,*next,*buf;
  char line[MAXLINE];
  double xx,yy,zz;
  double musp,spx,spy,spz;

  if (me_universe == 0 && screen)
    fprintf(screen,"Reading NEBSpin coordinate file(s) ...\n");

  // flag = 0, universe root reads header of file, bcast to universe
  // flag = 1, each replica's root reads header of file, bcast to world
  //   but explicitly skip first replica

  if (flag == 0) {
    if (me_universe == 0) {
      open(file);
      while (1) {
        eof = fgets(line,MAXLINE,fp);
        if (eof == NULL) error->one(FLERR,"Unexpected end of neb/spin file");
        start = &line[strspn(line," \t\n\v\f\r")];
        if (*start != '\0' && *start != '#') break;
      }
      sscanf(line,"%d",&nlines);
    }
    MPI_Bcast(&nlines,1,MPI_INT,0,uworld);

  } else {
    if (me == 0) {
      if (ireplica) {
        open(file);
        while (1) {
          eof = fgets(line,MAXLINE,fp);
          if (eof == NULL) error->one(FLERR,"Unexpected end of neb/spin file");
          start = &line[strspn(line," \t\n\v\f\r")];
          if (*start != '\0' && *start != '#') break;
        }
        sscanf(line,"%d",&nlines);
      } else nlines = 0;
    }
    MPI_Bcast(&nlines,1,MPI_INT,0,world);
  }

  char *buffer = new char[CHUNK*MAXLINE];
  char **values = new char*[ATTRIBUTE_PERLINE];

  double fraction = ireplica/(nreplica-1.0);

  double **x = atom->x;
  double **sp = atom->sp;
  double spinit[3],spfinal[3];
  int nlocal = atom->nlocal;

  // loop over chunks of lines read from file
  // two versions of read_lines_from_file() for world vs universe bcast
  // count # of atom coords changed so can check for invalid atom IDs in file

  int ncount = 0;

  int temp_flag,rot_flag;
  temp_flag = rot_flag = 0;
  int nread = 0;
  while (nread < nlines) {
    nchunk = MIN(nlines-nread,CHUNK);
    if (flag == 0)
      eofflag = comm->read_lines_from_file_universe(fp,nchunk,MAXLINE,buffer);
    else
      eofflag = comm->read_lines_from_file(fp,nchunk,MAXLINE,buffer);
    if (eofflag) error->all(FLERR,"Unexpected end of neb/spin file");

    buf = buffer;
    next = strchr(buf,'\n');
    *next = '\0';
    int nwords = atom->count_words(buf);
    *next = '\n';

    if (nwords != ATTRIBUTE_PERLINE)
      error->all(FLERR,"Incorrect atom format in neb/spin file");

    // loop over lines of atom coords
    // tokenize the line into values

    for (i = 0; i < nchunk; i++) {
      next = strchr(buf,'\n');

      values[0] = strtok(buf," \t\n\r\f");
      for (j = 1; j < nwords; j++)
        values[j] = strtok(NULL," \t\n\r\f");

      // adjust spin coord based on replica fraction
      // for flag = 0, interpolate for intermediate and final replicas
      // for flag = 1, replace existing coord with new coord
      // ignore image flags of final x
      // for interpolation:
      //   new x is displacement from old x via minimum image convention
      //   if final x is across periodic boundary:
      //     new x may be outside box
      //     will be remapped back into box when simulation starts
      //     its image flags will then be adjusted

      tag = ATOTAGINT(values[0]);
      m = atom->map(tag);
      if (m >= 0 && m < nlocal) {
        ncount++;
	musp = atof(values[1]);
	xx = atof(values[2]);
        yy = atof(values[3]);
        zz = atof(values[4]);
        spx = atof(values[5]);
        spy = atof(values[6]);
        spz = atof(values[7]);

        if (flag == 0) {

	  spinit[0] = sp[m][0];
	  spinit[1] = sp[m][1];
	  spinit[2] = sp[m][2];
	  spfinal[0] = spx;
	  spfinal[1] = spy;
	  spfinal[2] = spz;

	  // interpolate intermediate spin states

	  sp[m][3] = musp;
	  if (fraction == 0.0) {
	    sp[m][0] = spinit[0];
	    sp[m][1] = spinit[1];
	    sp[m][2] = spinit[2];
	  } else if (fraction == 1.0) {
	    sp[m][0] = spfinal[0];
	    sp[m][1] = spfinal[1];
	    sp[m][2] = spfinal[2];
	  } else {
            temp_flag = initial_rotation(spinit,spfinal,fraction);
            rot_flag = MAX(temp_flag,rot_flag);
	    sp[m][0] = spfinal[0];
	    sp[m][1] = spfinal[1];
	    sp[m][2] = spfinal[2];
	  }
        } else {
          sp[m][3] = musp;
	  x[m][0] = xx;
          x[m][1] = yy;
          x[m][2] = zz;
	  sp[m][0] = spx;
	  sp[m][1] = spy;
	  sp[m][2] = spz;
        }
      }

      buf = next + 1;
    }

    nread += nchunk;
  }

  // warning message if one or more couples (spi,spf) were aligned
  // this breaks Rodrigues' formula, and an arbitrary rotation
  // vector has to be chosen

  if ((rot_flag > 0) && (comm->me == 0))
    error->warning(FLERR,"arbitrary initial rotation of one or more spin(s)");

  // check that all atom IDs in file were found by a proc

  if (flag == 0) {
    int ntotal;
    MPI_Allreduce(&ncount,&ntotal,1,MPI_INT,MPI_SUM,uworld);
    if (ntotal != nreplica*nlines)
      error->universe_all(FLERR,"Invalid atom IDs in neb/spin file");
  } else {
    int ntotal;
    MPI_Allreduce(&ncount,&ntotal,1,MPI_INT,MPI_SUM,world);
    if (ntotal != nlines)
      error->all(FLERR,"Invalid atom IDs in neb/spin file");
  }

  // clean up

  delete [] buffer;
  delete [] values;

  if (flag == 0) {
    if (me_universe == 0) {
      if (compressed) pclose(fp);
      else fclose(fp);
    }
  } else {
    if (me == 0 && ireplica) {
      if (compressed) pclose(fp);
      else fclose(fp);
    }
  }
}

/* ----------------------------------------------------------------------
   initial configuration of intermediate spins using Rodrigues' formula
   interpolates between initial (spi) and final (stored in sploc)
------------------------------------------------------------------------- */

int NEBSpin::initial_rotation(double *spi, double *sploc, double fraction)
{

  // no interpolation for initial and final replica

  if (fraction == 0.0 || fraction == 1.0) return 0;

  int rot_flag = 0;
  double kx,ky,kz;
  double spix,spiy,spiz,spfx,spfy,spfz;
  double kcrossx,kcrossy,kcrossz,knormsq;
  double kdots;
  double spkx,spky,spkz;
  double sidotsf,omega,iknorm,isnorm;

  spix = spi[0];
  spiy = spi[1];
  spiz = spi[2];

  spfx = sploc[0];
  spfy = sploc[1];
  spfz = sploc[2];

  kx = spiy*spfz - spiz*spfy;
  ky = spiz*spfx - spix*spfz;
  kz = spix*spfy - spiy*spfx;

  knormsq = kx*kx+ky*ky+kz*kz;
  sidotsf = spix*spfx + spiy*spfy + spiz*spfz;

  // if knormsq == 0.0, init and final spins are aligned
  // Rodrigues' formula breaks, needs to define another axis k

  if (knormsq == 0.0) {
    if (sidotsf > 0.0) { 	// spins aligned and in same direction
      return 0;
    } else if (sidotsf < 0.0) {	// spins aligned and in opposite directions

      // defining a rotation axis
      // first guess, k = spi x [100]
      // second guess, k = spi x [010]

      if (spiy*spiy + spiz*spiz != 0.0) { // spin not along [100]
	kx = 0.0;
	ky = spiz;
	kz = -spiy;
	knormsq = ky*ky + kz*kz;
      } else if (spix*spix + spiz*spiz != 0.0) { // spin not along [010]
	kx = -spiz;
	ky = 0.0;
	kz = spix;
	knormsq = kx*kx + kz*kz;
      } else error->all(FLERR,"Incorrect initial rotation operation");
      rot_flag = 1;
    }
  }

  // knormsq should not be 0

  if (knormsq == 0.0)
    error->all(FLERR,"Incorrect initial rotation operation");

  // normalize k vector

  iknorm = 1.0/sqrt(knormsq);
  kx *= iknorm;
  ky *= iknorm;
  kz *= iknorm;

  // calc. k x spi and total rotation angle

  kcrossx = ky*spiz - kz*spiy;
  kcrossy = kz*spix - kx*spiz;
  kcrossz = kx*spiy - ky*spix;

  kdots = kx*spix + ky*spiz + kz*spiz;

  omega = acos(sidotsf);
  omega *= fraction;

  // apply Rodrigues' formula

  spkx = spix*cos(omega);
  spky = spiy*cos(omega);
  spkz = spiz*cos(omega);

  spkx += kcrossx*sin(omega);
  spky += kcrossy*sin(omega);
  spkz += kcrossz*sin(omega);

  spkx += kx*kdots*(1.0-cos(omega));
  spky += ky*kdots*(1.0-cos(omega));
  spkz += kz*kdots*(1.0-cos(omega));

  // normalizing resulting spin vector

  isnorm = 1.0/sqrt(spkx*spkx+spky*spky+spkz*spkz);
  if (isnorm == 0.0)
    error->all(FLERR,"Incorrect initial rotation operation");

  spkx *= isnorm;
  spky *= isnorm;
  spkz *= isnorm;

  // returns rotated spin

  sploc[0] = spkx;
  sploc[1] = spky;
  sploc[2] = spkz;

  return rot_flag;
}

/* ----------------------------------------------------------------------
   universe proc 0 opens NEBSpin data file
   test if gzipped
------------------------------------------------------------------------- */

void NEBSpin::open(char *file)
{
  compressed = 0;
  char *suffix = file + strlen(file) - 3;
  if (suffix > file && strcmp(suffix,".gz") == 0) compressed = 1;
  if (!compressed) fp = fopen(file,"r");
  else {
#ifdef LAMMPS_GZIP
    char gunzip[128];
    snprintf(gunzip,128,"gzip -c -d %s",file);

#ifdef _WIN32
    fp = _popen(gunzip,"rb");
#else
    fp = popen(gunzip,"r");
#endif

#else
    error->one(FLERR,"Cannot open gzipped file");
#endif
  }

  if (fp == NULL) {
    char str[128];
    snprintf(str,128,"Cannot open file %s",file);
    error->one(FLERR,str);
  }
}

/* ----------------------------------------------------------------------
   query fix NEBSpin for info on each replica
   universe proc 0 prints current NEBSpin status
------------------------------------------------------------------------- */

void NEBSpin::print_status()
{
  int nlocal = atom->nlocal;
  double tx,ty,tz;
  double tnorm2,local_norm_inf,temp_inf;
  double **sp = atom->sp;
  double **fm = atom->fm;

  // calc. magnetic torques

  tnorm2 = local_norm_inf = temp_inf = 0.0;
  for (int i = 0; i < nlocal; i++) {
    tx = (fm[i][1]*sp[i][2] - fm[i][2]*sp[i][1]);
    ty = (fm[i][2]*sp[i][0] - fm[i][0]*sp[i][2]);
    tz = (fm[i][0]*sp[i][1] - fm[i][1]*sp[i][0]);
    tnorm2 += tx*tx + ty*ty + tz*tz;
    temp_inf = MAX(fabs(tx),fabs(ty));
    temp_inf = MAX(fabs(tz),temp_inf);
    local_norm_inf = MAX(temp_inf,local_norm_inf);
  }

  double fmaxreplica;
  MPI_Allreduce(&tnorm2,&fmaxreplica,1,MPI_DOUBLE,MPI_MAX,roots);

  double fnorminf = 0.0;
  MPI_Allreduce(&local_norm_inf,&fnorminf,1,MPI_DOUBLE,MPI_MAX,world);
  double fmaxatom;
  MPI_Allreduce(&fnorminf,&fmaxatom,1,MPI_DOUBLE,MPI_MAX,roots);

  if (verbose) {
    freplica = new double[nreplica];
    MPI_Allgather(&tnorm2,1,MPI_DOUBLE,&freplica[0],1,MPI_DOUBLE,roots);
    fmaxatomInRepl = new double[nreplica];
    MPI_Allgather(&fnorminf,1,MPI_DOUBLE,&fmaxatomInRepl[0],1,MPI_DOUBLE,roots);
  }

  double one[7];
  one[0] = fneb->veng;
  one[1] = fneb->plen;
  one[2] = fneb->nlen;
  one[3] = fneb->gradlen;

  if (verbose) {
    one[4] = fneb->dotpath;
    one[5] = fneb->dottangrad;
    one[6] = fneb->dotgrad;
  }

  if (output->thermo->normflag) one[0] /= atom->natoms;
  if (me == 0)
    MPI_Allgather(one,numall,MPI_DOUBLE,&all[0][0],numall,MPI_DOUBLE,roots);
  MPI_Bcast(&all[0][0],numall*nreplica,MPI_DOUBLE,0,world);

  rdist[0] = 0.0;
  for (int i = 1; i < nreplica; i++)
    rdist[i] = rdist[i-1] + all[i][1];
  double endpt = rdist[nreplica-1] = rdist[nreplica-2] + all[nreplica-2][2];
  for (int i = 1; i < nreplica; i++)
    rdist[i] /= endpt;

  // look up GradV for the initial, final, and climbing replicas
  // these are identical to fnorm2, but to be safe we
  // take them straight from fix_neb

  double gradvnorm0, gradvnorm1, gradvnormc;

  int irep;
  irep = 0;
  gradvnorm0 = all[irep][3];
  irep = nreplica-1;
  gradvnorm1 = all[irep][3];
  irep = fneb->rclimber;
  if (irep > -1) {
    gradvnormc = all[irep][3];
    ebf = all[irep][0]-all[0][0];
    ebr = all[irep][0]-all[nreplica-1][0];
  } else {
    double vmax = all[0][0];
    int top = 0;
    for (int m = 1; m < nreplica; m++)
      if (vmax < all[m][0]) {
        vmax = all[m][0];
        top = m;
      }
    irep = top;
    gradvnormc = all[irep][3];
    ebf = all[irep][0]-all[0][0];
    ebr = all[irep][0]-all[nreplica-1][0];
  }

  if (me_universe == 0) {
    FILE *uscreen = universe->uscreen;
    FILE *ulogfile = universe->ulogfile;
    if (uscreen) {
      fprintf(uscreen,BIGINT_FORMAT " %12.8g %12.8g ",
              update->ntimestep,fmaxreplica,fmaxatom);
      fprintf(uscreen,"%12.8g %12.8g %12.8g ",
              gradvnorm0,gradvnorm1,gradvnormc);
      fprintf(uscreen,"%12.8g %12.8g %12.8g ",ebf,ebr,endpt);
      for (int i = 0; i < nreplica; i++)
        fprintf(uscreen,"%12.8g %12.8g ",rdist[i],all[i][0]);
      if (verbose) {
	for (int i = 0; i < nreplica-1; i++)
	  fprintf(uscreen,"%12.8g %12.8g ",all[i][2],all[i][5]);
	fprintf(uscreen,"%12.8g %12.8g ",NAN,all[nreplica-1][5]);
      }
      fprintf(uscreen,"\n");
    }

    if (ulogfile) {
      fprintf(ulogfile,BIGINT_FORMAT " %12.8g %12.8g ",
              update->ntimestep,fmaxreplica,fmaxatom);
      fprintf(ulogfile,"%12.8g %12.8g %12.8g ",
              gradvnorm0,gradvnorm1,gradvnormc);
      fprintf(ulogfile,"%12.8g %12.8g %12.8g ",ebf,ebr,endpt);
      for (int i = 0; i < nreplica; i++)
        fprintf(ulogfile,"%12.8g %12.8g ",rdist[i],all[i][0]);
      if (verbose) {
	for (int i = 0; i < nreplica-1; i++)
	  fprintf(ulogfile,"%12.8g %12.8g ",all[i][2],all[i][5]);
	fprintf(ulogfile,"%12.8g %12.8g ",NAN,all[nreplica-1][5]);
      }
      fprintf(ulogfile,"\n");
      fflush(ulogfile);
    }
  }
}