-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkshop1.py
623 lines (493 loc) · 21.4 KB
/
workshop1.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Workshop 1 - main file
Write a LAMMPS code directy from Python from codelets (Python script templates)
Typical section LAMMPS code with variables (strings, expressions, vectors, lists)
are coded with "codelets" (dynamics scripts of the class pizza.script())
Variables values can be defined from placeholders and subsequently overidden.
Each codelet (pizza.script object) sets two main variables
DEFINITIONS coding for the variables, some examples follows:
strings: "$ this is a string # comment"
vector: [1,0,1]
list: ["p","f","p"] or [500, 0.9, "rcb"]
tupple: (-10,2)
expression: '50*${c0}^2*${rho}' with variables and mathematical operators or functions
TEMPLATE is a string with placeholders/variables:
${variable} will be replaced by its value (possibly after evaluation)
${expression}
NOTE1: Indexing is authorized in DEFINITIONS but not in TEMPLATES
Vectors and lists are expanded/flattened as strings when evaluated
run = [500,1000]
run0 = "${run[0]}"
run1 = "${run[1]}"
NOTE2: Vectors and lists are expanded with " " as separator
Tuples are expanded with "," as separator
Values of definitions and new variables can be set at runtime
mycodelet = codelet()
mycodelet.USER.var1 = 1
mycodelet.USER.var2 = 2
Several instances may be run with different values
mycodelet1 = codelet(var=1)
mycodelet2 = codelet(var=2)
mycodelet1.USER.var1 = 10
mycodelet1.USER.var2 = 20
Codelets can be multievaluated with the operator "&" (and)
mycodelet = codelet(var=1) & codelet(var=2) & ...
with all variables defined in each instance of codelet
Codelets can be combined with the operator "+" (plus)
mycodelet = codelet_1(var1=1) + codelet_2(var2=2)
with the definition of variable var1 passed to codelet_2
note: in the example above, codelet_2 can a function of var1, var2
note: if the same variable is defined in two codelets, the last
definition is used
note: mycodelet can be also multievaluated:
mycodelet(var1=1,var2=2) & mycodelet(var1=10,var2=20)
Codelets can be displayed, converted to stings, written to a file
mycodelet + ENTER to display it
mycodelet.DEFINITIONS to see the definitions
mycodelet.USER to see user definitions
mycodelet.TEMPLATE shows the TEMPLATE
note: It is not recommended to change TEMPLATE in instances
(all instances will be affected)
mycodelet.do() evaluates the codelet as string
mycodelet.write("inp.mylammpsscript") write a LAMMPS script
Creating your codelets (beyond the scope of the workshop).
All codelets should be of or derived from the class pizza.script()
class newcodelet(oldcodelet)
description = "blah, blah"
useid = "newcodelet()"
version = 0
DEFINTIONS = scriptdata(
var1 = 1,
var2 = 2
)
TEMPLATE = "" "
# LAMMPS code
"" "
USAGE:
from workshop0 import *
use name_of_the_codelet.description to see the syntax
codelet = name_of_the_codelet(var1,var2)
fullscript = codelet1 + codelet2 + codelet3
fullscript.write("mylammpsscript.inp")
LIST OF CODELETS:
-- initialization --
initialization() ---> initialize the framework
-- thermo --
integration() ---> set integration parameters
thermo_print() ---> pseudo thermostat initialization, and computes
-- equilibration --
equilibration() ---> equilibration steps
note: equilibration() is designed to be multievaluated
-- dump --
smddump()
note: dump are framework depended, set all outputs for SMD
-- displacements --
translation() ---> translation of rigid objects
force() ---> set forces
Created on Fri Feb 25 13:52:08 2022 - revised on 2022-03-18
@author: olivi,billy
"""
# Revision history
# 2022-02-28 early version, almost functional (dump and run missing)
# 2022-03-01 release candidate, full documentation, better style
# 2022-03-02 full documentation for the workshop
# 2022-03-02 fix neighbor in initialization() and example
# 2022-03-02 first post-workshop0 fixes (others are coming before forking as workshop1)
# 2022-03-17 [FORK] workshop1: load, group, and interactions classes removed from
# workshop (replaced with script.scriptobject)
# thermo class broken into integrate and thermo_print
# integration class created with time integration, gravity and balancing
# thermo_printclass created with print/log screen options
# equilibration class streamlined
# dump class upgraded, option to chose dump information by catagories
# translation and force classes created, can now apply equations directly
# run class created
#
# generic dependencies
import datetime, os, socket, getpass
# import script, forcefield and struct classes
from pizza.script import *
from pizza.forcefield import *
from pizza.private.struct import struct
# %% INITIALIZATION
class initialization(globalsection):
"""
workshop1.initialization()
workshop1.initialization(param1=value1,param2=value2)
Initialize LAMMPS script of workshop1
Example
-------
init = initialization(boundary=["p","f","p"],dimension=2)
init.USER.boundary=["p","f","p"] # works also
init.do() # shows the script
bead_kernel_radius = put the value of the kernel radius of the bead
init.USER.neighbor = [bead_kernel_radius,"bin"]
Comments:
Set parameters at during the first call or use the USER atrribute to do it
The method do() shows the content of the script
type init +ENTER to see all accessible DEFINITIONS and TEMPLATE
How to generate a complex script
---------------------------------
fullscript = init+geom+groups+physgravity+forcefield+\
initthermo+equilsteps+dump+moves
fullscript.write("tmp/myscript.inp")
Comments:
Several scripts can be combined with the operator +
As a result, values set in DEFINITIONS are shared between all scripts
Use: fullscript.write("tmp/myscript.inp") to generate a LAMMPS file
Use: fullscript.do() to show the script
type fullscript +ENTER to see all details
Previous step/class: none
Next step/class: integration()
"""
description = "initialization(var1=val1,var2=val2,...)"
userid = "initialization" # user name
version = 1.0 # version
# SMD Scheme (these variables are available everywhere)
DEFINITIONS = scriptdata(
units= "$ si",
dimension= 2,
boundary= ["p","f","p"], # simulation box boundaries
comm_modify= ["vel","yes"],
comm_style= "$ tiled",
atom_modify= ["map","array"],
newton= "$ off",
neighbor= [1,"bin"],
neigh_modify_every= 5,
neigh_modify_delay= 0,
neigh_modify_check= "$ yes",
atom_style= "$ smd"
)
# Template
TEMPLATE = "\n\n# " + "\n# ".join(script._contact) + "\n"*3 + """
# Scheme initialization
units ${units}
dimension ${dimension}
boundary ${boundary} # f = fixed, p = periodic
comm_modify ${comm_modify}
comm_style ${comm_style}
atom_modify ${atom_modify}
newton ${newton}
neighbor ${neighbor} # search radius for neighbor list (=kernel radius)
neigh_modify every ${neigh_modify_every} delay ${neigh_modify_delay} check ${neigh_modify_check}
atom_style ${atom_style}
"""
# %% THERMO
class integration(integrationsection):
"""
workshop1.integration( g = 9.81,g_vector = [0,1,0],dt = 0.1,adjust_radius = [1.01,10,15],
balance = [500, 0.9, "rcb"])
Set default parameters for time integration
Example
-------
inte = integration()
inte = integration(g_vector = [1,-1,0]) # change direction of gravity
Comments:
refer to TEMPLATE for details
gravity is intuitive to adjust
integration parameters:
dt sets the initial timestep, smd then dynamically modifies dt
adjust_radius lets smd dynamically modify the kernel radius to optimize num. neighbors
balance allows LAMMPS to dynamically modify the domain and processor loading
Previous step/class: initialization()
Next step/class: thermo_print()
"""
description = 'thermo(param=value...)'
userid = "thermo()"
version = 1.0
DEFINITIONS = scriptdata(
g = 9.81,
g_vector = [0,1,0],
dt = 0.1,
adjust_radius = [1.01,10,15],
balance = [500, 0.9, "rcb"] # load balancing for MPI
)
TEMPLATE = """
# Gravity
fix gfix all gravity ${g} vector ${g_vector}
# Time integration conditions
fix dtfix fluid smd/adjust_dt ${dt} # dynamically adjust time increment every step
fix integration_fix_ulsph ulsph smd/integrate_ulsph adjust_radius ${adjust_radius}
fix integration_fix_tlsph tlsph smd/integrate_tlsph
# balancing
fix balance_fix all balance ${balance}
"""
class thermo_print(integrationsection):
"""
workshop1.thermo_print()
Set frequency and parameters of terminal output
Example
-------
thermo = thermo_print()
thermo = thermo_print(outstep = 1000) # less terminal output
Comments:
refer to TEMPLATE for details
default thermo_style prints number of steps and timestep (which is dynamic)
Previous step/class: interactions()
Next step/class: equilibration()
"""
description = 'thermo(param=value...)'
userid = "thermo_print()"
version = 1.0
DEFINITIONS = scriptdata(
outstep = 100,
thermo_modify = ["lost","ignore"],
thermo_style = ["custom","step","dt"]
)
TEMPLATE = """
# thermodynamic information
thermo ${outstep}
thermo_modify ${thermo_modify}
thermo_style ${thermo_style}
"""
# %% EQUILIBRATION
class equilibration(integrationsection):
"""
workshop1.equilibration()
Relax and equilibrate the simulation with limit_velocities
It performs 50 iterations where limit_velocities(n) = 0.9*maxvel(n-1)
where n is the iteration step: 0.9^50 = 0.005
i.e. final_velocity =< 0.005*inital_velocity
Example
-------
equilsteps = equilibration(iterations=10)
Comments:
Heuristic, based on rapidly destroying kinetic energy in the system
iterations can be modified
rule of thumb: velocity halved every 7 iterations
Previous step/class: thermo_print()
Next step/class: smddump()
"""
description = 'equilibration()'
userid = "equilibration()"
version = 1.0
DEFINITIONS = scriptdata(
it = 50,
re = 0.9
)
TEMPLATE = """
# Equilibration
dump dump_equilibrate all custom 100 dump.equilibrate id type x y z &
fx fy fz vx vy vz radius
dump_modify dump_equilibrate first yes
variable vmag atom sqrt(vx^2+vy^2+vz^2)
compute maxvel all reduce max v_vmag
variable maxvelre equal ${re}*c_maxvel
run ${it}000 every 1000 &
"print \${maxvelre}" &
"fix ulsph_equilibration ulsph smd/integrate_ulsph limit_velocity \${maxvelre}" &
"fix tlsph_equilibration tlsph smd/integrate_tlsph limit_velocity \${maxvelre}"
dump_modify dump_equilibrate every 1000000
fix ulsph_equilibration ulsph smd/integrate_ulsph
fix tlsph_equilibration tlsph smd/integrate_tlsph
"""
# %% DUMP SECTION
class smddump(dumpsection):
"""
workshop1.smddump(outstep=1000,outputfile="$myfile")
Dump file format for SMD
Example
-------
dump = smddump(outstep=5000,outputfile="$ dump.workshop0")
dump = smddump(machanical = True) # computes and dumps mechanical data
Comments:
lots of flexibility to manage dumpfiles so several options:
outstep = dump every outstep
outputfile = dump filename
set these to True or False to choose what to dump [DefaultValue]:
particle_data [True] = id,type,x,y,z,mol,mass,volume,radius,contact_radius,density
velocity_force [True] = vx,vy,vz,fx,fy,fz
mechanical [False] = stress, strain and strain rate tensors
thermo [False] = internal energy
calc_data [False] = nn number of neighbors, processor number, hourglass error
Previous step/class: equilibration()
Next step/class: translation()
"""
description = 'smddump(outstep=1000,outputfile="$myfile")'
userid = "smddump()"
version = 1.0
DEFINITIONS = scriptdata(
outstep = 1000,
outputfile = "$ dump.file",
particle_data = ["id","type","x","y","z","mol","mass","c_rho","c_vol","radius","c_contact_radius"],
v_xyz = ["vx","vy","vz"],
f_xyz = ["fx","fy","fz"],
stress_tensor = ["c_S[1]","c_S[2]"],
strain_tensor = ["c_E[1]","c_E[2]"],
strain_rate_tensor = ["c_L[1]","c_L[2]"],
thermo_properties = ["c_epl"],
calc_data = ["c_nn","proc","c_HGe"]
)
def __init__(self,particle_data=True,velocity_force=True,mechanical=False,thermo=False,calc_data=False,**args):
COMPUTE = """
# Additional computed values
"""
DUMP = """
# dump command
dump dump_id all custom ${outstep} ${outputfile} """
if particle_data:
COMPUTE += """
# Compute supp particle data
compute contact_radius all smd/contact/radius
compute vol all smd/vol
compute rho all smd/rho
"""
DUMP += """&
${particle_data} """
if velocity_force:
DUMP += """&
${v_xyz} ${f_xyz} """
if mechanical:
COMPUTE += """
# Compute mechanical data
compute S tlsph smd/tlsph/stress
compute E tlsph smd/tlsph/stress
compute L tlsph smd/tlsph/stress
compute P ulpsh smd/ulsph/stress
"""
DUMP += """&
${stress_tensor} ${strain_tensor} ${strain_rate_tensor}"""
if thermo:
COMPUTE += """
# Compute thermo data
compute e_int all smd/internal_energy"""
DUMP += """&
${thermo_properties} """
if calc_data:
COMPUTE += """
# Compute calculation data
compute nn ulsph smd/ulsph/num/neighs
computer HGE all smd/hourglass_error"""
DUMP += """&
${calc_data}"""
self.TEMPLATE = COMPUTE + DUMP + """
dump_modify dump_id first yes #every/time 0.001
"""
self.USER = scriptdata(**args)
# %% DISPLACEMENTS AND FORCES
class translation(runsection):
"""
workshop1.translation(eq_vx = ["NULL"],eq_vy = ["NULL"],eq_vz = ["NULL"],
fix_ID = ["setvelocities"],group = "all")
Translates top and bottom according to set velocities or forces
Example
-------
moves = translation(eq_vx = ["NULL"],eq_vy = ["0"],eq_vz = ["sin(temp)",group="object1"])
Comments:
Refer to TEMPLATE for details
=0 sets velocity to 0 whereas =NULL means do not alter
Previous step/class: smddump()
Next step/class: force()
"""
description = 'translation(velocity1=[],velocity2=[],force=[],run=5000)'
userid = "smddump()"
version = 1.0
DEFINITIONS = scriptdata(
eqvx = ["0"],
eqvy = ["0"],
eqvz = ["0"],
fixIDv = ["setvelocities"],
group = ["all"]
)
TEMPLATE = """
# Translation
variable vx equal ${eqvx}
variable vy equal ${eqvy}
variable vz equal ${eqvz}
fix ${fixIDv} ${group} smd/setvel v_vx v_vy v_vz
"""
class force(runsection):
"""
workshop0.translation(velocity1=[],velocity2=[],force=[],run=5000)
Translates top and bottom according to set velocities or forces
Example
-------
moves = translation(velocity1 = [0,-1,0], velocity2 = [0,1,0],run=5000) & \
translation(velocity1 = [0,-0.1,0], velocity2 = [0,0.1,0],run=2000) & \
translation(force=[0,-1,0], velocity1 = [0,0,0], velocity2 = [0,0,0],run=21000) & \
rampforce(ramp=(-1,-10), velocity1 = [0,0,0], velocity2 = [0,0,0],run=21000)
Comments:
Refer to TEMPLATE for details
Previous step/class: smddump()
Next step/class: rampforce()
"""
description = 'translation(velocity1=[],velocity2=[],force=[],run=5000)'
userid = "smddump()"
version = 1.0
DEFINITIONS = scriptdata(
eqfx = ["0"],
eqfy = ["0"],
eqfz = ["0"],
fixIDf = ["setforces"],
group = ["all"]
)
TEMPLATE = """
# Force
variable fx equal ${eqfx}/count(${group})
variable fy equal ${eqfy}/count(${group})
variable fz equal ${eqfz}/count(${group})
fix ${fixIDf} ${group} smd/setvel v_fx v_fy v_fz
"""
# %% RUN
class run(runsection):
"""
workshop1.run(runs=50000)
Performs a run of N steps
Example
-------
moves = translation(velocity1 = [0,-1,0], velocity2 = [0,1,0],run=5000) & \
translation(velocity1 = [0,-0.1,0], velocity2 = [0,0.1,0],run=2000) & \
translation(force=[0,-1,0], velocity1 = [0,0,0], velocity2 = [0,0,0],run=21000) & \
rampforce(ramp=(-1,-10), velocity1 = [0,0,0], velocity2 = [0,0,0],run=21000)
Comments:
Refer to TEMPLATE for details
Previous step/class: smddump()
Next step/class: rampforce()
"""
description = 'number of runs'
userid = "run()"
version = 1.0
DEFINITIONS = scriptdata(
runs = 50000
)
TEMPLATE = """
# run section
run ${runs}
"""
# %% DEBUG
# ===================================================
# main()
# ===================================================
# for debugging purposes (code called as a script)
# the code is called from here
# ===================================================
if __name__ == '__main__':
# initizalization of the scheme (note that c0 is note used in DEFINITIONS)
bead_kernel_radius = 0.0015
init = initialization(neighbor =[bead_kernel_radius,"bin"])
init.do() # shows the script
# scriptobject handles bead interactions and sets material properties from library (rigidwall, water, solid food)
b1 = scriptobject(name="bead 1",group = ["rigid", "solid"],filename='./raster_4_types.lmp',forcefield=rigidwall())
b2 = scriptobject(name="bead 2", group = ["fluid", "ulsph"],filename = './raster_4_types.lmp',forcefield=water())
b3 = scriptobject(name="bead 3", group = ["oscillating", "solid","tlsph"],filename = './raster_4_types.lmp',forcefield=solidfood())
b4 = scriptobject(name="bead 4", group = ["solid", "tlsph"],filename = './raster_4_types.lmp',forcefield=solidfood())
# set gravity, timestep, adjusted kernel radius
inte = integration()
# modify which data is returned to the terminal
thermo = thermo_print()
# equilibration/relaxation protocol, iterations can be reduced or augmented
equilsteps = equilibration(it=15)
# modify the dump settings, outstep, properties dumped etc.
dmp = smddump(outstep=2000,outputfile=["dump.workshop1"],)
# use equations to move specific objects or groups of atoms
moves = translation(vx = ["0.1*exp(-step/100)"],vy = ["0"],vz = ["0"]) & \
run() & \
translation() & \
force() & \
run()
#combine everything into a full script and write file
collection = b1+b2+b3+b4
fullscript = init + collection.script + inte + thermo + equilsteps + dmp + moves
fullscript.write("./tmp/in.swimmingpool")