/
constraints.py
604 lines (450 loc) · 16.4 KB
/
constraints.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
# Copyright (C) 2010-2022 The ESPResSo project
#
# This file is part of ESPResSo.
#
# ESPResSo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ESPResSo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .script_interface import ScriptObjectList, ScriptInterfaceHelper, script_interface_register
import numpy as np
import itertools
@script_interface_register
class Constraints(ScriptObjectList):
"""
List of active constraints. Add a :class:`espressomd.constraints.Constraint`
to make it active in the system, or remove it to make it inactive.
"""
_so_name = "Constraints::Constraints"
def add(self, *args, **kwargs):
"""
Add a constraint to the list.
Parameters
----------
constraint: :class:`espressomd.constraints.Constraint`
Either a constraint object...
**kwargs
... or parameters to construct an
:class:`espressomd.constraints.ShapeBasedConstraint`
Returns
----------
constraint : :class:`espressomd.constraints.Constraint`
The added constraint
"""
if len(args) == 1:
if isinstance(args[0], Constraint):
constraint = args[0]
else:
raise TypeError(
"Either a Constraint object or key-value pairs for the parameters of a ShapeBasedConstraint object need to be passed.")
else:
constraint = ShapeBasedConstraint(**kwargs)
self.call_method("add", object=constraint)
return constraint
def remove(self, constraint):
"""
Remove a constraint from the list.
Parameters
----------
constraint : :obj:`espressomd.constraints.Constraint`
"""
self.call_method("remove", object=constraint)
def clear(self):
"""
Remove all constraints.
"""
self.call_method("clear")
class Constraint(ScriptInterfaceHelper):
"""
Base class for constraints. A constraint provides a force and
an energy contribution for a single particle.
"""
_so_name = "Constraints::Constraint"
@script_interface_register
class ShapeBasedConstraint(Constraint):
"""
Attributes
----------
only_positive : :obj:`bool`
Act only in the direction of positive normal,
only useful if penetrable is ``True``.
particle_type : :obj:`int`
Interaction type of the constraint.
particle_velocity : array_like of :obj:`float`
Interaction velocity of the boundary
penetrable : :obj:`bool`
Whether particles are allowed to penetrate the constraint.
shape : :class:`espressomd.shapes.Shape`
One of the shapes from :mod:`espressomd.shapes`
See Also
----------
espressomd.shapes : shape module that defines mathematical surfaces
Examples
----------
>>> import espressomd
>>> import espressomd.shapes
>>> system = espressomd.System(box_l=3 * [10.])
>>>
>>> # create first a shape-object to define the constraint surface
>>> spherical_cavity = espressomd.shapes.Sphere(center=system.box_l / 2, radius=2.0, direction=-1.0)
>>>
>>> # now create an un-penetrable shape-based constraint of type 0
>>> spherical_constraint = system.constraints.add(particle_type=0, penetrable=False, shape=spherical_cavity)
>>>
>>> # place a trapped particle inside this sphere
>>> system.part.add(pos=0.51 * system.box_l, type=1)
"""
_so_name = "Constraints::ShapeBasedConstraint"
def min_dist(self):
"""
Calculates the minimum distance to all interacting particles.
Returns
----------
:obj:`float` :
The minimum distance
"""
return self.call_method("min_dist", object=self)
def total_force(self):
"""
Get total force acting on this constraint.
Examples
----------
>>> import espressomd
>>> import espressomd.shapes
>>> system = espressomd.System(box_l=[50., 50., 50.])
>>> system.time_step = 0.01
>>> system.thermostat.set_langevin(kT=0.0, gamma=1.0)
>>> system.cell_system.set_n_square(use_verlet_lists=False)
>>> system.non_bonded_inter[0, 0].lennard_jones.set_params(
... epsilon=1, sigma=1, cutoff=2**(1. / 6), shift="auto")
>>>
>>> floor = system.constraints.add(
... shape=espressomd.shapes.Wall(normal=[0, 0, 1], dist=0.0),
... particle_type=0, penetrable=False, only_positive=False)
>>>
>>> p = system.part.add(pos=[0,0,1.5], type=0, ext_force=[0, 0, -.1])
>>> # print the particle position as it falls
>>> # and print the force it applies on the floor
>>> for t in range(10):
... system.integrator.run(100)
... print(p.pos, floor.total_force())
"""
return self.call_method("total_force", constraint=self)
def total_normal_force(self):
"""
Get the total summed normal force acting on this constraint.
"""
return self.call_method("total_normal_force", constraint=self)
@script_interface_register
class HomogeneousMagneticField(Constraint):
"""
Attributes
----------
H : (3,) array_like of :obj:`float`
Magnetic field vector. Describes both field direction and
strength of the magnetic field (via length of the vector).
"""
_so_name = "Constraints::HomogeneousMagneticField"
class _Interpolated(Constraint):
"""
Tabulated field data.
The actual field value is calculated by linear
interpolation (force fields) or gradient linear
interpolation.
The data has to have one point of halo in each direction,
and is shifted by half a grid spacing in the +xyz direction,
so that the element (0,0,0) has coordinates -0.5 * grid_spacing.
The number of points has to be such that the data spans the whole
box, e.g. the most up right back point has to be at least at
box + 0.5 * grid_spacing. There are convenience functions on this
class that can calculate the required grid dimensions and the coordinates.
Arguments
----------
field : (M, N, O, P) array_like of :obj:`float`
The actual field on a grid of size (M, N, O) with dimension P.
grid_spacing : (3,) array_like of :obj:`float`
Spacing of the grid points.
Attributes
----------
field : (M, N, O, P) array_like of :obj:`float`
The actual field on a grid of size (M, N, O) with dimension P.
Please be aware that depending on the interpolation
order additional points are used on the boundaries.
grid_spacing : array_like of :obj:`float`
Spacing of the grid points.
origin : (3,) array_like of :obj:`float`
Coordinates of the grid origin.
"""
def __init__(self, **kwargs):
if "sip" not in kwargs:
field = kwargs.pop("field")
shape, codim = self._unpack_dims(field)
super().__init__(_field_shape=shape, _field_codim=codim,
_field_data=field.flatten(), **kwargs)
else:
super().__init__(**kwargs)
@classmethod
def required_dims(cls, box_size, grid_spacing):
"""
Calculate the grid size and origin needed for specified box size and
grid spacing. Returns the shape and origin (coordinates of [0][0][0])
needed.
Arguments
---------
box_size : (3,) array_like of obj:`float`
The box the field should be used.
grid_spacing : array_like obj:`float`
The desired grid spacing.
"""
shape = np.array(np.ceil(box_size / grid_spacing), dtype=int) + 2
origin = -0.5 * grid_spacing
return shape, origin
@classmethod
def field_from_fn(cls, box_size, grid_spacing, f, codim=None):
"""Generate field data for a desired box size and grid spacing
by evaluating a function at the coordinates.
Arguments
---------
box_size : (3,) array_like of obj:`float`
The box the field should be used.
grid_spacing : array_like obj:`float`
The desired grid spacing.
f : callable
A function that is called with the coordinates of every
grid point to populate the grid.
"""
shape, origin = cls.required_dims(box_size, grid_spacing)
if not codim:
codim = cls._codim
field = np.zeros((shape[0], shape[1], shape[2], codim))
for i in itertools.product(*map(range, shape)):
x = origin + np.array(i) * grid_spacing
field[i] = f(x)
return field
@classmethod
def field_coordinates(cls, box_size, grid_spacing):
"""Returns an array of the coordinates of the grid points required.
Arguments
---------
box_size : (3,) array_like of obj:`float`
The box the field should be used.
grid_spacing : array_like obj:`float`
The desired grid spacing.
"""
return cls.field_from_fn(box_size, grid_spacing, lambda x: x, 3)
def _unpack_dims(self, a):
s = a.shape
shape = s[:3]
codim = s[3]
return (shape, codim)
@property
def field(self):
shape = self._field_shape
return np.reshape(self._field_data,
(shape[0], shape[1], shape[2], self._field_codim))
@script_interface_register
class ForceField(_Interpolated):
"""
A generic tabulated force field that applies a per-particle scaling factor.
Arguments
----------
field : (M, N, O, 3) array_like of :obj:`float`
Forcefield amplitude on a grid of size (M, N, O).
grid_spacing : (3,) array_like of :obj:`float`
Spacing of the grid points.
default_scale : :obj:`float`
Scaling factor for particles that have no individual scaling factor.
particle_scales : :obj:`dict`
A dictionary mapping particle ids to scaling factors.
For these particles, the interaction is scaled with
their individual scaling factor. Other particles are
scaled with the default scaling factor.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
_codim = 3
_so_name = "Constraints::ForceField"
@script_interface_register
class PotentialField(_Interpolated):
"""
A generic tabulated force field that applies a per-particle
scaling factor. The forces are calculated numerically from
the data by finite differences. The potential is interpolated
from the provided data.
Arguments
----------
field : (M, N, O, 1) array_like of :obj:`float`
Potential on a grid of size (M, N, O).
grid_spacing : (3,) array_like of :obj:`float`
Spacing of the grid points.
default_scale : :obj:`float`
Scaling factor for particles that have no individual scaling factor.
particle_scales : :obj:`dict`
A dictionary mapping particle ids to scaling factors.
For these particles, the interaction is scaled with
their individual scaling factor. Other particles are
scaled with the default scaling factor.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
_codim = 1
_so_name = "Constraints::PotentialField"
@script_interface_register
class Gravity(Constraint):
"""
Gravity force
:math:`F = m \\cdot g`
Arguments
----------
g : (3,) array_like of :obj:`float`
The gravitational acceleration.
"""
def __init__(self, **kwargs):
if "sip" not in kwargs:
kwargs["value"] = kwargs.pop("g")
super().__init__(**kwargs)
@property
def g(self):
return self.value
_so_name = "Constraints::Gravity"
@script_interface_register
class LinearElectricPotential(Constraint):
"""
Electric potential of the form
:math:`\\phi = -E \\cdot x + \\phi_0`,
resulting in the electric field E
everywhere. (E.g. in a plate capacitor).
The resulting force on the particles are then
:math:`F = q \\cdot E`
where :math:`q` is the charge of the particle.
Arguments
----------
E : array_like of :obj:`float`
The electric field.
phi0 : :obj:`float`
The potential at the origin
"""
def __init__(self, phi0=0, **kwargs):
if "sip" not in kwargs:
kwargs["A"] = -np.array(kwargs.pop("E"))
kwargs["b"] = phi0
super().__init__(**kwargs)
@property
def E(self):
return -np.array(self.A)
@property
def phi0(self):
return np.array(self.b)
_so_name = "Constraints::LinearElectricPotential"
@script_interface_register
class ElectricPlaneWave(Constraint):
"""
Electric field of the form
:math:`E = E0 \\cdot \\sin(k \\cdot x + \\omega \\cdot t + \\phi)`
The resulting force on the particles are then
:math:`F = q \\cdot E`
where :math:`q` is the charge of the particle.
This can be used to generate a homogeneous AC
field by setting k to zero.
Arguments
----------
E0 : array_like of :obj:`float`
Amplitude of the electric field.
k : array_like of :obj:`float`
Wave vector of the wave
omega : :obj:`float`
Frequency of the wave
phi : :obj:`float`, optional
Phase shift
"""
_so_name = "Constraints::ElectricPlaneWave"
def __init__(self, phi=0, **kwargs):
if "sip" not in kwargs:
kwargs["amplitude"] = kwargs.pop("E0")
kwargs["wave_vector"] = kwargs.pop("k")
kwargs["frequency"] = kwargs.pop("omega")
kwargs["phase"] = phi
super().__init__(**kwargs)
@property
def E0(self):
return np.array(self.amplitude)
@property
def k(self):
return np.array(self.wave_vector)
@property
def omega(self):
return self.frequency
@property
def phi(self):
return self.phase
@script_interface_register
class FlowField(_Interpolated):
"""
Viscous coupling to a flow field that is
interpolated from tabulated data like
:math:`F = -\\gamma \\cdot \\left( u(r) - v \\right)`
where :math:`v` is the velocity of the particle.
Arguments
----------
field : (M, N, O, 3) array_like of :obj:`float`
Field velocity on a grid of size (M, N, O)
grid_spacing : (3,) array_like of :obj:`float`
Spacing of the grid points.
gamma : :obj:`float`
Coupling constant
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
_codim = 3
_so_name = "Constraints::FlowField"
@script_interface_register
class HomogeneousFlowField(Constraint):
"""
Viscous coupling to a flow field that is
constant in space with the force
:math:`F = -\\gamma \\cdot (u - v)`
where :math:`v` is the velocity of the particle.
Attributes
----------
gamma : :obj:`float`
Coupling constant
"""
def __init__(self, **kwargs):
if "sip" not in kwargs:
kwargs["value"] = kwargs.pop("u")
super().__init__(**kwargs)
@property
def u(self):
"""
Field velocity ((3,) array_like of :obj:`float`).
"""
return self.value
_so_name = "Constraints::HomogeneousFlowField"
@script_interface_register
class ElectricPotential(_Interpolated):
"""
Electric potential interpolated from
provided data. The electric field E is
calculated numerically from the potential,
and the resulting force on the particles are
:math:`F = q \\cdot E`
where :math:`q` is the charge of the particle.
Arguments
----------
field : (M, N, O, 1) array_like of :obj:`float`
Potential on a grid of size (M, N, O)
grid_spacing : (3,) array_like of :obj:`float`
Spacing of the grid points.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
_codim = 1
_so_name = "Constraints::ElectricPotential"