File: split.py

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (80 lines) | stat: -rwxr-xr-x 1,823 bytes parent folder | download
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
#!/usr/bin/env python -i
# preceding line should have path for Python on your machine

# split.py
# Purpose: similar to simple.py, but first the world communicator
#   is split in two halves and LAMMPS is run only on one partition
# Syntax:  split.py in.lammps
#          in.lammps = LAMMPS input script

from __future__ import print_function
import sys

# parse command line

argv = sys.argv
if len(argv) != 2:
  print("Syntax: simple.py in.lammps")
  sys.exit()

infile = sys.argv[1]

me = 0

# this example *only* works with mpi4py version 2.0.0 or later

from mpi4py import MPI
comm = MPI.COMM_WORLD
me = comm.Get_rank()
nprocs = comm.Get_size()

# create two subcommunicators

if me < nprocs // 2:  color = 0
else: color = 1

split = comm.Split(color,key=0)

if color == 0:
  from lammps import lammps
  lmp = lammps(comm=split)

  # run infile one line at a time

  lines = open(infile,'r').readlines()
  for line in lines: lmp.command(line)

  # run 10 more steps
  # get coords from LAMMPS
  # change coords of 1st atom
  # put coords back into LAMMPS
  # run a single step with changed coords

  lmp.command("run 10")
  x = lmp.gather_atoms("x",1,3)
  epsilon = 0.1
  x[0] += epsilon
  lmp.scatter_atoms("x",1,3,x)
  lmp.command("run 1");

  f = lmp.extract_atom("f")
  print("Force on 1 atom via extract_atom: ",f[0][0])

  fx = lmp.extract_variable("fx","all",1)
  print("Force on 1 atom via extract_variable:",fx[0])
  print("Proc %d out of %d procs has" % (me,nprocs), lmp)
  print("Calculation on partition 0 complete")

else:
  # could run a 2nd calculation on second partition
  #   with different LAMMPS instance or another code
  # in this case, just sleep on second partition

  import time
  time.sleep(2)
  print("Calculation on partition 1 complete")

# shutdown mpi4py

comm.Barrier()
MPI.Finalize()