/usr/share/pyshared/MMTK/Universe.py is in python-mmtk 2.7.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 | # This module implements the various types of universes
# (infinite, periodic etc.). A universe defines the
# geometry of space, the force field, and external interactions
# (boundary conditions, external fields, etc.)
#
# Written by Konrad Hinsen
#
"""
Universes
"""
__docformat__ = 'restructuredtext'
from MMTK import Bonds, ChemicalObjects, Collections, Environment, \
Random, Utility, ParticleProperties, Visualization
from Scientific.Geometry import Transformation
from Scientific.Geometry import Vector, isVector
from Scientific import N
import copy
try:
import threading
if not hasattr(threading, 'Thread'):
threading = None
except ImportError:
threading = None
#
# The base class for all universes.
#
class Universe(Collections.GroupOfAtoms, Visualization.Viewable):
"""
Universe
A universe represents a complete model of a chemical system, i.e.
the molecules, their environment (topology, boundary conditions,
thermostats, etc.), and optionally a force field.
The class Universe is an abstract base class that defines
properties common to all kinds of universes. To create universe
objects, use one of its subclasses.
In addition to the methods listed below, universe objects support
the following operations (u is any universe object, o is any
chemical object):
* len(u) yields the number of chemical objects in the universe
* u[i] returns object number i
* u.name = o adds o to the universe and also makes it accessible as
an attribute
* del u.name removes the object that was assigned to u.name from
the universe
"""
def __init__(self, forcefield, properties):
self._forcefield = forcefield
self._evaluator = {}
self.name = ''
if properties.has_key('name'):
self.name = properties['name']
del properties['name']
self._objects = Collections.Collection()
self._environment = []
self._configuration = None
self._masses = None
self._atom_properties = {}
self._atoms = None
self._bond_database = None
self._bond_pairs = None
self._version = 0
self._np = None
is_universe = True
is_periodic = False
is_orthogonal = False
def __getstate__(self):
state = copy.copy(self.__dict__)
state['_evaluator'] = {}
state['_configuration'] = None
del state['_masses']
del state['_bond_database']
del state['_bond_pairs']
del state['_np']
del state['_spec']
return state
def __setstate__(self, state):
state['_np'] = None
state['_atoms'] = None
state['_bond_database'] = None
state['_bond_pairs'] = None
self.__dict__['_environment'] = []
if state.has_key('atom_properties'):
self.__dict__['_atom_properties'] = state['atom_properties']
del state['atom_properties']
for attr, value in state.items():
self.__dict__[attr] = value
self._evaluator = {}
self._masses = None
self._createSpec()
def __len__(self):
return len(self._objects)
def __getitem__(self, item):
return self._objects[item]
def __setattr__(self, attr, value):
if attr[0] != '_' and self.__dict__.has_key(attr):
try:
self.removeObject(self.__dict__[attr])
except ValueError:
pass
self.__dict__[attr] = value
if attr[0] != '_' and (ChemicalObjects.isChemicalObject(value)
or Environment.isEnvironmentObject(value)):
self.addObject(value)
def __delattr__(self, attr):
try:
self.removeObject(self.__dict__[attr])
except ValueError:
pass
del self.__dict__[attr]
def __repr__(self):
return self.__class__.__name__ + ' ' + self.name + ' containing ' + \
`len(self._objects)` + ' objects.'
__str__ = __repr__
def __copy__(self):
return copy.deepcopy(self)
def objectList(self, klass = None):
"""
:param klass: an optional class argument
:type klass: class
:returns: a list of all chemical objects in the universe.
If klass is given, only objects that are instances
of klass are returned.
:rtype: list
"""
return self._objects.objectList(klass)
def environmentObjectList(self, klass = None):
"""
:param klass: an optional class argument
:type klass: class
:returns: a list of all environment objects in the universe.
If klass is given, only objects that are instances
of klass are returned.
:rtype: list
"""
if klass is None:
return self._environment
else:
return filter(lambda o, k=klass: o.__class__ is k,
self._environment)
def atomList(self):
"""
:returns: a list of all atoms in the universe
:rtype: list
"""
if self._atoms is None:
self._atoms = self._objects.atomList()
return self._atoms
def atomIterator(self):
return self._objects.atomIterator()
def bondedUnits(self):
return self._objects.bondedUnits()
def universe(self):
"""
:returns: the universe itself
"""
return self
def addObject(self, object, steal = False):
"""
Adds object to the universe. If object is a Collection,
all elements of the Collection are added to the universe.
:param object: the object (chemical or environment) to be added
:param steal: if True, permit stealing the object from another
universe, otherwise the object must not yet be
attached to any universe.
:type steal: bool
"""
if ChemicalObjects.isChemicalObject(object):
if (not steal) and object.parent is not None:
if isUniverse(object.parent):
raise ValueError(`object` +
' is already in another universe')
else:
raise ValueError(`object` + ' is part of another object')
object.parent = self
self._objects.addObject(object)
self._changed(True)
elif Environment.isEnvironmentObject(object):
for o in self._environment:
o.checkCompatibilityWith(object)
self._environment.append(object)
self._changed(False)
elif Collections.isCollection(object) \
or Utility.isSequenceObject(object):
for o in object:
self.addObject(o, steal)
else:
raise TypeError(repr(object) + ' cannot be added to a universe')
def removeObject(self, object):
"""
Removes object from the universe. If object is a Collection,
each of its elements is removed. The object to be removed must
be in the universe.
:param object: the object (chemical or environment) to be removed
"""
if ChemicalObjects.isChemicalObject(object):
if object.parent != self:
raise ValueError(`object` + ' is not in this universe.')
object.parent = None
self._objects.removeObject(object)
self._changed(True)
elif Collections.isCollection(object) \
or (Utility.isSequenceObject(object)
# Strings are nasty because their elements are strings
# as well. This creates infinite recursion without
# this special-case handling.
and not isinstance(object, basestring)):
for o in object:
self.removeObject(o)
elif Environment.isEnvironmentObject(object):
self._environment.remove(object)
self._changed(False)
else:
raise ValueError(`object` + ' is not in this universe.')
def selectShell(self, point, r1, r2=0.):
"""
:param point: a point in space
:type point: Scientific.Geometry.Vector
:param r1: one of the radii of a spherical shell
:type r1: float
:param r2: the other of the two radii of a spherical shell
:type r2: float
:returns: a Collection of all objects in the universe whose
distance from point lies between r1 and r2.
"""
return self._objects.selectShell(point, r1, r2)
def selectBox(self, p1, p2):
"""
:param p1: one corner of a box in space
:type p1: Scientific.Geometry.Vector
:param p2: the other corner of a box in space
:type p2: Scientific.Geometry.Vector
:returns: a Collection of all objects in the universe that lie
within the box whose diagonally opposite corners are
given by p1 and p2.
"""
return self._objects.selectBox(p1, p2)
def _changed(self, system_size_changed):
self._evaluator = {}
self._bond_database = None
self._version += 1
if system_size_changed:
if self._configuration is not None:
for a in self.atomList():
a.unsetArray()
self._configuration = None
self._masses = None
self._atom_properties = {}
self._atoms = None
self._np = None
self._bond_pairs = None
else:
if self._configuration is not None:
self._configuration.version = self._version
if self._masses is not None:
self._masses.version = self._version
def acquireReadStateLock(self):
"""
Acquire the universe read state lock. Any application that
uses threading must acquire this lock prior to accessing the
current state of the universe, in particular its configuration
(particle positions). This guarantees the consistency of the
data; while any thread holds the read state lock, no other
thread can obtain the write state lock that permits modifying
the state. The read state lock should be released as soon as
possible.
The read state lock can be acquired only if no thread holds
the write state lock. If the read state lock cannot be
acquired immediately, the thread will be blocked until
it becomes available. Any number of threads can acquire
the read state lock simultaneously.
"""
return self._spec.stateLock(1)
def acquireWriteStateLock(self):
"""
Acquire the universe write state lock. Any application that
uses threading must acquire this lock prior to modifying the
current state of the universe, in particular its configuration
(particle positions). This guarantees the consistency of the
data; while any thread holds the write state lock, no other
thread can obtain the read state lock that permits accessing
the state. The write state lock should be released as soon as
possible.
The write state lock can be acquired only if no other thread
holds either the read state lock or the write state lock. If
the write state lock cannot be acquired immediately, the
thread will be blocked until it becomes available.
"""
return self._spec.stateLock(-1)
def releaseReadStateLock(self, write=False):
"""
Release the universe read state lock.
"""
return self._spec.stateLock(2)
def releaseWriteStateLock(self, write=False):
"""
Release the universe write state lock.
"""
return self._spec.stateLock(-2)
def acquireConfigurationChangeLock(self, waitflag=True):
"""
Acquire the configuration change lock. This lock should be
acquired before starting an algorithm that changes the
configuration continuously, e.g. minimization or molecular dynamics
algorithms. This guarantees the proper order of execution when
several such operations are started in succession. For example,
when a minimization should be followed by a dynamics run,
the use of this flag permits both operations to be started
as background tasks which will be executed one after the other,
permitting other threads to run in parallel.
The configuration change lock should not be confused with
the universe state lock. The former guarantees the proper
sequence of long-running algorithms, whereas the latter
guarantees the consistency of the data. A dynamics algorithm,
for example, keeps the configuration change lock from the
beginning to the end, but acquires the universe state lock
only immediately before modifying configuration and velocities,
and releases it immediately afterwards.
:param waitflag: if true, the method waits until the lock
becomes available; this is the most common mode.
If false, the method returns immediately even
if another thread holds the lock.
:type waitflag: bool
:returns: a flag indicating if the lock was successfully
acquired (1) or not (0).
:rtype: int
"""
if waitflag:
return self._spec.configurationChangeLock(1)
else:
return self._spec.configurationChangeLock(0)
def releaseConfigurationChangeLock(self):
"""
Releases the configuration change lock.
"""
self._spec.configurationChangeLock(2)
def setForceField(self, forcefield):
"""
:param forcefield: the new forcefield for this universe
:type forcefield: :class:`~MMTK.ForceFields.ForceField.ForceField`
"""
self._forcefield = forcefield
self._evaluator = {}
self._bond_database = None
def position(self, object, conf):
if ChemicalObjects.isChemicalObject(object) \
or Collections.isCollection(object):
return object.position(conf)
elif isVector(object):
return object
else:
return Vector(object)
def numberOfAtoms(self):
return self._objects.numberOfAtoms()
def numberOfPoints(self):
if self._np is None:
self._np = Collections.GroupOfAtoms.numberOfPoints(self)
return self._np
numberOfCartesianCoordinates = numberOfPoints
def configuration(self):
"""
:returns: the configuration object describing the current
configuration of the universe. Note that this is not a
copy of the current state, but a reference: the positions
in the configuration object will change when coordinate
changes are applied to the universe in whatever way.
:rtype: :class:`~MMTK.ParticleProperties.Configuration`
"""
if self._configuration is None:
np = self.numberOfAtoms()
coordinates = N.zeros((np, 3), N.Float)
index_map = {}
redef = []
for a in self.atomList():
if a.index is None or a.index >= np:
redef.append(a)
else:
if index_map.get(a.index, None) is None:
index_map[a.index] = a
else:
redef.append(a)
free_indices = [i for i in xrange(np)
if index_map.get(i, None) is None]
assert len(free_indices) == len(redef)
for a, i in zip(redef, free_indices):
a.index = i
# At this point a.index runs from 0 to np-1 in the universe.
for a in self.atomList():
if a.array is None:
try:
coordinates[a.index, :] = a.pos.array
del a.pos
except AttributeError:
coordinates[a.index, :] = Utility.undefined
else:
coordinates[a.index, :] = a.array[a.index, :]
a.array = coordinates
# Define configuration object.
self._configuration = 1 # a hack to prevent endless recursion
self._configuration = \
ParticleProperties.Configuration(self, coordinates)
return self._configuration
def copyConfiguration(self):
"""
This operation is thread-safe; it won't return inconsistent
data even when another thread is modifying the configuration.
:returns: a copy of the current configuration
:rtype: :class:`~MMTK.ParticleProperties.Configuration`
"""
self.acquireReadStateLock()
try:
conf = copy.copy(self.configuration())
finally:
self.releaseReadStateLock()
return conf
def atomNames(self):
self.configuration()
names = self.numberOfAtoms()*[None]
for a in self.atomList():
names[a.index] = a.fullName()
return names
def setConfiguration(self, configuration, block=True):
"""
Update the current configuration of the universe by copying
the given input configuration.
:param configuration: the new configuration
:type configuration: :class:`~MMTK.ParticleProperties.Configuration`
:param block: if True, the operation blocks other threads
from accessing the configuration before the update
is completed. If False, it is assumed that the
caller takes care of locking.
:type block: bool
"""
if not ParticleProperties.isConfiguration(configuration):
raise TypeError('not a universe configuration')
conf = self.configuration()
if block:
self.acquireWriteStateLock()
try:
conf.assign(configuration)
self.setCellParameters(configuration.cell_parameters)
finally:
if block:
self.releaseWriteStateLock()
def addToConfiguration(self, displacement, block=True):
"""
Update the current configuration of the universe by adding
the given displacement vector.
:param displacement: the displacement vector for each atom
:type displacement: :class:`~MMTK.ParticleProperties.ParticleVector`
:param block: if True, the operation blocks other threads
from accessing the configuration before the update
is completed. If False, it is assumed that the
caller takes care of locking.
:type block: bool
"""
conf = self.configuration()
if block:
self.acquireWriteStateLock()
try:
conf.assign(conf+displacement)
finally:
if block:
self.releaseWriteStateLock()
def getParticleScalar(self, name, datatype = N.Float):
"""
:param name: the name of an atom attribute
:type name: str
:param datatype: the datatype of the array allocated to hold the data
:returns: the values of the attribute 'name' for each atom
in the universe.
:rtype: :class:`~MMTK.ParticleProperties.ParticleScalar`
"""
conf = self.configuration()
array = N.zeros((len(conf),), datatype)
for a in self.atomList():
array[a.index] = getattr(a, name)
return ParticleProperties.ParticleScalar(self, array)
getAtomScalarArray = getParticleScalar
def getParticleBoolean(self, name):
"""
:param name: the name of an atom attribute
:type name: str
:returns: the values of the boolean attribute 'name' for each atom
in the universe, or False for atoms that do not have
the attribute.
:rtype: :class:`~MMTK.ParticleProperties.ParticleScalar`
"""
conf = self.configuration()
array = N.zeros((len(conf),), N.Int)
for a in self.atomList():
try:
array[a.index] = getattr(a, name)
except AttributeError: pass
return ParticleProperties.ParticleScalar(self, array)
getAtomBooleanArray = getParticleBoolean
def masses(self):
"""
:returns: the masses of all atoms in the universe
:rtype: :class:`~MMTK.ParticleProperties.ParticleScalar`
"""
if self._masses is None:
self._masses = self.getParticleScalar('_mass')
return self._masses
def charges(self):
"""
Return the atomic charges defined by the universe's
force field.
:returns: the charges of all atoms in the universe
:rtype: :class:`~MMTK.ParticleProperties.ParticleScalar`
"""
ff = self._forcefield
if ff is None:
raise ValueError("no force field defined")
return ff.charges(self)
def velocities(self):
"""
:returns: the current velocities of all atoms, or None if
no velocities are defined. Note that this is not a
copy of the current state but a reference to it;
its data will change whenever any changes are made
to the current velocities.
:rtype: :class:`~MMTK.ParticleProperties.ParticleVector`
"""
try:
return self._atom_properties['velocity']
except KeyError:
return None
def setVelocities(self, velocities, block=True):
"""
Update the current velocities of the universe by copying
the given input velocities.
:param velocities: the new velocities, or None to remove
the velocity definition from the universe
:type velocities: :class:`~MMTK.ParticleProperties.ParticleVector`
:param block: if True, the operation blocks other threads
from accessing the configuration before the update
is completed. If False, it is assumed that the
caller takes care of locking.
:type block: bool
"""
if velocities is None:
try:
del self._atom_properties['velocity']
except KeyError:
pass
else:
try:
v = self._atom_properties['velocity']
except KeyError:
v = ParticleProperties.ParticleVector(self)
self._atom_properties['velocity'] = v
if block:
self.acquireWriteStateLock()
try:
v.assign(velocities)
finally:
if block:
self.releaseWriteStateLock()
def initializeVelocitiesToTemperature(self, temperature):
"""
Generate random velocities for all atoms from a Boltzmann
distribution.
:param temperature: the reference temperature for the Boltzmann
distribution
:type temperature: float
"""
self.configuration()
masses = self.masses()
if self._atom_properties.has_key('velocity'):
del self._atom_properties['velocity']
fixed = self.getParticleBoolean('fixed')
np = self.numberOfPoints()
velocities = N.zeros((np, 3), N.Float)
for i in xrange(np):
m = masses[i]
if m > 0. and not fixed[i]:
velocities[i] = Random.randomVelocity(temperature,
m).array
self._atom_properties['velocity'] = \
ParticleProperties.ParticleVector(self, velocities)
self.adjustVelocitiesToConstraints()
def scaleVelocitiesToTemperature(self, temperature, block=True):
"""
Scale all velocities by a common factor in order to obtain
the specified temperature.
:param temperature: the reference temperature
:type temperature: float
:param block: if True, the operation blocks other threads
from accessing the configuration before the update
is completed. If False, it is assumed that the
caller takes care of locking.
:type block: bool
"""
velocities = self.velocities()
factor = N.sqrt(temperature/self.temperature())
if block:
self.acquireWriteStateLock()
try:
velocities.scaleBy(factor)
finally:
if block:
self.releaseWriteStateLock()
def degreesOfFreedom(self):
return GroupOfAtoms.degreesOfFreedom(self) \
- self.numberOfDistanceConstraints()
def distanceConstraintList(self):
"""
:returns: the list of distance constraints
:rtype: list
"""
return self._objects.distanceConstraintList()
def numberOfDistanceConstraints(self):
"""
:returns: the number of distance constraints
:rtype: int
"""
return self._objects.numberOfDistanceConstraints()
def setBondConstraints(self):
"""
Sets distance constraints for all bonds.
"""
self.configuration()
self._objects.setBondConstraints(self)
self.enforceConstraints()
def removeDistanceConstraints(self):
"""
Removes all distance constraints.
"""
self._objects.removeDistanceConstraints(self)
def enforceConstraints(self, configuration=None, velocities=None):
"""
Enforces the previously defined distance constraints
by modifying the configuration and velocities.
:param configuration: the configuration in which the
constraints are enforced
(None for current configuration)
:type configuration: :class:`~MMTK.ParticleProperties.Configuration`
:param velocities: the velocities in which the
constraints are enforced
(None for current velocities)
:type velocities: :class:`~MMTK.ParticleProperties.ParticleVector`
"""
from MMTK import Dynamics
Dynamics.enforceConstraints(self, configuration)
self.adjustVelocitiesToConstraints(velocities)
def adjustVelocitiesToConstraints(self, velocities=None, block=True):
"""
Modifies the velocities to be compatible with
the distance constraints, i.e. projects out the velocity
components along the constrained distances.
:param velocities: the velocities in which the
constraints are enforced
(None for current velocities)
:type velocities: :class:`~MMTK.ParticleProperties.ParticleVector`
:param block: if True, the operation blocks other threads
from accessing the configuration before the update
is completed. If False, it is assumed that the
caller takes care of locking.
:type block: bool
"""
from MMTK import Dynamics
if velocities is None:
velocities = self.velocities()
if velocities is not None:
if block:
self.acquireWriteStateLock()
try:
Dynamics.projectVelocities(self, velocities)
finally:
if block:
self.releaseWriteStateLock()
def bondLengthDatabase(self):
if self._bond_database is None:
self._bond_database = None
if self._bond_database is None:
ff = self._forcefield
try:
self._bond_database = ff.bondLengthDatabase(self)
except AttributeError:
pass
if self._bond_database is None:
self._bond_database = Bonds.DummyBondLengthDatabase(self)
return self._bond_database
def forcefield(self):
"""
:returns: the force field
:rtype: :class:`~MMTK.ForceFields.ForceField.ForceField`
"""
return self._forcefield
def energyEvaluatorParameters(self, subset1 = None, subset2 = None):
self.configuration()
from MMTK.ForceFields import ForceField
ffdata = ForceField.ForceFieldData()
return self._forcefield.evaluatorParameters(self, subset1, subset2,
ffdata)
def energyEvaluator(self, subset1 = None, subset2 = None,
threads=None, mpi_communicator=None):
if self._forcefield is None:
raise ValueError("no force field defined")
try:
eval = self._evaluator[(subset1, subset2, threads)]
except KeyError:
from MMTK.ForceFields import ForceField
eval = ForceField.EnergyEvaluator(self, self._forcefield,
subset1, subset2,
threads, mpi_communicator)
self._evaluator[(subset1, subset2, threads)] = eval
return eval
def energy(self, subset1 = None, subset2 = None, small_change=False):
"""
:param subset1: a subset of a universe, or None
:type subset1: :class:`~MMTK.ChemicalObjects.ChemicalObject`
:param subset2: a subset of a universe, or None
:type subset2: :class:`~MMTK.ChemicalObjects.ChemicalObject`
:param small_change: if True, algorithms optimized for small
configurational changes relative to the last
evaluation may be used.
:type small_change: bool
:returns: the potential energy of interaction between the atoms
in subset1 and the atoms in subset2. If subset2 is None,
the interactions within subset1 are calculated. It both
subsets are None, the potential energy of the whole
universe is returned.
:rtype: float
"""
eval = self.energyEvaluator(subset1, subset2)
return eval(0, 0, small_change)
def energyAndGradients(self, subset1 = None, subset2 = None,
small_change=False):
"""
:returns: the energy and the energy gradients
:rtype: (float, :class:`~MMTK.ParticleProperties.ParticleVector`)
"""
eval = self.energyEvaluator(subset1, subset2)
return eval(1, 0, small_change)
def energyAndForceConstants(self, subset1 = None, subset2 = None,
small_change=False):
"""
:returns: the energy and the force constants
:rtype: (float, :class:`~MMTK.ParticleProperties.SymmetricPairTensor`)
"""
eval = self.energyEvaluator(subset1, subset2)
e, g, fc = eval(0, 1, small_change)
return e, fc
def energyGradientsAndForceConstants(self, subset1 = None, subset2 = None,
small_change=False):
"""
:returns: the energy, its gradients, and the force constants
:rtype: (float, :class:`~MMTK.ParticleProperties.ParticleVector`,
:class:`~MMTK.ParticleProperties.SymmetricPairTensor`)
"""
eval = self.energyEvaluator(subset1, subset2)
return eval(1, 1, small_change)
def energyTerms(self, subset1 = None, subset2 = None, small_change=False):
"""
:returns: a dictionary containing the energy values for each
energy term separately. The energy terms are defined by the
force field.
:rtype: dict
"""
eval = self.energyEvaluator(subset1, subset2)
eval(0, 0, small_change)
return eval.lastEnergyTerms()
def configurationDifference(self, conf1, conf2):
"""
:param conf1: a configuration
:type conf1: :class:`~MMTK.ParticleProperties.Configuration`
:param conf2: a configuration
:type conf2: :class:`~MMTK.ParticleProperties.Configuration`
:returns: the difference vector between the two configurations
for each atom, taking into account the universe
topology (e.g. minimum-image convention).
:rtype: :class:`~MMTK.ParticleProperties.ParticleVector`
"""
d = conf2-conf1
cell = conf1.cell_parameters
if cell is not None:
self._spec.foldCoordinatesIntoBox(d.array)
return d
def distanceVector(self, p1, p2, conf=None):
"""
:param p1: a vector or a chemical object whose position is taken
:param p2: a vector or a chemical object whose position is taken
:param conf: a configuration (None for the current configuration)
:returns: the distance vector between p1 and p2 (i.e. the
vector from p1 to p2) in the configuration conf,
taking into account the universe's topology.
"""
p1 = self.position(p1, conf)
p2 = self.position(p2, conf)
if conf is None:
return Vector(self._spec.distanceVector(p1.array, p2.array))
else:
cell = self._fixCellParameters(conf.cell_parameters)
if cell is None:
return Vector(self._spec.distanceVector(p1.array, p2.array))
else:
return Vector(self._spec.distanceVector(p1.array, p2.array,
cell))
def distance(self, p1, p2, conf = None):
"""
:param p1: a vector or a chemical object whose position is taken
:param p2: a vector or a chemical object whose position is taken
:param conf: a configuration (None for the current configuration)
:returns: the distance between p1 and p2, i.e. the length
of the distance vector
:rtype: float
"""
return self.distanceVector(p1, p2, conf).length()
def angle(self, p1, p2, p3, conf = None):
"""
:param p1: a vector or a chemical object whose position is taken
:param p2: a vector or a chemical object whose position is taken
:param p3: a vector or a chemical object whose position is taken
:param conf: a configuration (None for the current configuration)
:returns: the angle between the distance vectors p1-p2 and p3-p2
:rtype: float
"""
v1 = self.distanceVector(p2, p1, conf)
v2 = self.distanceVector(p2, p3, conf)
return v1.angle(v2)
def dihedral(self, p1, p2, p3, p4, conf = None):
"""
:param p1: a vector or a chemical object whose position is taken
:param p2: a vector or a chemical object whose position is taken
:param p3: a vector or a chemical object whose position is taken
:param p4: a vector or a chemical object whose position is taken
:param conf: a configuration (None for the current configuration)
:returns: the dihedral angle between the plane containing the
distance vectors p1-p2 and p3-p2 and the plane containing
the distance vectors p2-p3 and p4-p3
:rtype: float
"""
v1 = self.distanceVector(p2, p1, conf)
v2 = self.distanceVector(p3, p2, conf)
v3 = self.distanceVector(p3, p4, conf)
a = v1.cross(v2).normal()
b = v3.cross(v2).normal()
cos = a*b
sin = b.cross(a)*v2/v2.length()
return Transformation.angleFromSineAndCosine(sin, cos)
def _deleteAtom(self, atom):
pass
def basisVectors(self):
"""
:returns: the basis vectors of the elementary cell of a periodic
universe, or None for a non-periodic universe
:rtype: NoneType or list
"""
return None
def reciprocalBasisVectors(self):
"""
:returns: the reciprocal basis vectors of the elementary cell of
a periodic universe, or None for a non-periodic universe
:rtype: NoneType or list
"""
return None
def cellParameters(self):
return None
def setCellParameters(self, parameters):
if parameters is not None:
raise ValueError('incompatible cell parameters')
def _fixCellParameters(self, cell_parameters):
return cell_parameters
def cellVolume(self):
"""
:returns: the volume of the elementary cell of a periodic
universe, None for a non-periodic universe
:rtype: NoneType or float
"""
return None
def largestDistance(self):
"""
:returns: the largest possible distance between any two points
that can be represented independent of orientation, i.e. the
radius of the largest sphere that fits into the simulation
cell. Returns None if no such upper limit exists.
:rtype: NoneType or float
"""
return None
def contiguousObjectOffset(self, objects = None, conf = None,
box_coordinates = False):
"""
:param objects: a list of chemical objects, or None for all
objects in the universe
:type objects: list
:param conf: a configuration (None for the current configuration)
:param box_coordinates: use box coordinates rather than real ones
:type box_coordinates: bool
:returns: a set of displacement vectors relative to
the conf which, when added to the configuration,
create a configuration in which none of the objects
is split across the edge of the elementary cell.
For nonperiodic universes the return value is None.
:rtype: :class:`~MMTK.ParticleProperties.ParticleVector`
"""
return None
def contiguousObjectConfiguration(self, objects = None, conf = None):
"""
:param objects: a list of chemical objects, or None for all
objects in the universe
:type objects: list
:param conf: a configuration (None for the current configuration)
:returns: configuration conf (default: current configuration)
corrected by the contiguous object offsets for that
configuration.
:rtype: :class:`~MMTK.ParticleProperties.Configuration`
"""
if conf is None:
conf = self.configuration()
offset = self.contiguousObjectOffset(objects, conf)
if offset is not None:
return conf + offset
else:
return copy.copy(conf)
def realToBoxCoordinates(self, vector):
"""
Box coordinates are defined only for periodic universes;
their components have values between -0.5 and 0.5; these
extreme values correspond to the walls of the simulation box.
:param vector: a point in the universe
:returns: the box coordinate equivalent of vector, or the original
vector if no box coordinate system exists
:rtype: Scientific.Geometry.Vector
"""
return vector
def boxToRealCoordinates(self, vector):
"""
:param vector: a point in the universe expressed in box coordinates
:returns: the real-space equivalent of vector
:rtype: Scientific.Geometry.Vector
"""
return vector
def _realToBoxPointArray(self, array, parameters=None):
return array
def _boxToRealPointArray(self, array, parameters=None):
return array
def cartesianToFractional(self, vector):
"""
Fractional coordinates are defined only for periodic universes;
their components have values between 0. and 1.
:param vector: a point in the universe
:type vector: Scientific.Geometry.Vector
:returns: the fractional coordinate equivalent of vector
:rtype: Scientific.N.array_type
"""
raise ValueError("Universe is not periodic")
def cartesianToFractionalMatrix(self):
raise ValueError("Universe is not periodic")
def fractionalToCartesian(self, array):
"""
Fractional coordinates are defined only for periodic universes;
their components have values between 0. and 1.
:param array: an array of fractional coordinates
:type array: Scientific.N.array_type
:returns: the real-space equivalent of vector
:rtype: Scientific.Geometry.Vector
"""
raise ValueError("Universe is not periodic")
def fractionalToCartesianMatrix(self):
raise ValueError("Universe is not periodic")
def foldCoordinatesIntoBox(self):
return
def randomPoint(self):
"""
:returns: a random point from a uniform distribution within
the universe. This operation is defined only for
finite-volume universes, e.g. periodic universes.
:rtype: Scientific.Geometry.Vector
"""
raise TypeError("undefined operation")
def map(self, function):
"""
Apply a function to all objects in the universe and
return the list of the results. If the results are chemical
objects, a Collection object is returned instead of a list.
:param function: the function to be applied
:type function: callable
:returns: the list or collection of the results
"""
return self._objects.map(function)
def description(self, objects = None, index_map = None):
if objects is None:
objects = self
attributes = {}
for attr in dir(self):
if attr[0] != '_':
object = getattr(self, attr)
if ChemicalObjects.isChemicalObject(object) \
or Environment.isEnvironmentObject(object):
attributes[object] = attr
items = []
for o in objects.objectList():
attr = attributes.get(o, None)
if attr is not None:
items.append(repr(attr))
items.append(o.description(index_map))
for o in self._environment:
attr = attributes.get(o, None)
if attr is not None:
items.append(repr(attr))
items.append(o.description())
try:
classname = self.classname_for_trajectories
except AttributeError:
classname = self.__class__.__name__
s = 'c(%s,[%s])' % \
(`classname + self._descriptionArguments()`,
','.join(items))
return s
def _graphics(self, conf, distance_fn, model, module, options):
return self._objects._graphics(conf, distance_fn, model,
module, options)
def setFromTrajectory(self, trajectory, step = None):
"""
Set the state of the universe to the one stored in a trajectory.
This operation is thread-safe; it blocks other threads that
want to access the configuration or velocities while the data is
being updated.
:param trajectory: a trajectory object for this universe
:type trajectory: :class:`~MMTK.Trajectory.Trajectory`
:param step: a step number, or None for the default step
(0 for a standard trajectory, the last written
step for a restart trajectory)
:type step: int
"""
if step is None:
step = trajectory.defaultStep()
self.acquireWriteStateLock()
try:
self.setConfiguration(trajectory.configuration[step], False)
vel = self.velocities()
try:
vel_tr = trajectory.velocities[step]
except AttributeError:
if vel is not None:
Utility.warning("velocities were not modified because " +
"the trajectory does not contain " +
"velocity data.")
return
if vel is None:
self._atom_properties['velocity'] = vel_tr
else:
vel.assign(vel_tr)
finally:
self.releaseWriteStateLock()
#
# More efficient reimplementations of methods in Collections.GroupOfAtoms
#
def numberOfFixedAtoms(self):
return self.getParticleBoolean('fixed').sumOverParticles()
def degreesOfFreedom(self):
return 3*(self.numberOfAtoms()-self.numberOfFixedAtoms()) \
- self.numberOfDistanceConstraints()
def mass(self):
return self.masses().sumOverParticles()
def centerOfMass(self, conf = None):
m = self.masses()
if conf is None:
conf = self.configuration()
return (m*conf).sumOverParticles()/m.sumOverParticles()
def kineticEnergy(self, velocities = None):
if velocities is None:
velocities = self.velocities()
return 0.5*velocities.massWeightedDotProduct(velocities)
def momentum(self, velocities = None):
if velocities is None:
velocities = self.velocities()
return (self.masses()*velocities).sumOverParticles()
def translateBy(self, vector):
conf = self.configuration().array
N.add(conf, vector.array[N.NewAxis, :], conf)
def applyTransformation(self, t):
conf = self.configuration().array
rot = t.rotation().tensor.array
conf[:] = N.dot(conf, N.transpose(rot))
N.add(conf, t.translation().vector.array[N.NewAxis, :], conf)
def writeXML(self, file):
file.write('<?xml version="1.0" encoding="ISO-8859-1" ' +
'standalone="yes"?>\n\n')
file.write('<molecularsystem>\n\n')
file.write('<templates>\n\n')
memo = {'counter': 1}
instances = []
atoms = []
for object in self._objects.objectList():
instances = instances + object.writeXML(file, memo, 1)
atoms = atoms + object.getXMLAtomOrder()
file.write('\n</templates>\n\n')
file.write('<universe %s>\n' % self.XMLSpec())
for instance in instances:
file.write(' ')
file.write(instance)
file.write('\n')
conf = self.configuration()
if conf.hasValidPositions():
file.write(' <configuration>\n')
file.write(' <atomArray units="units:nm"\n')
file.write(' x3="')
for atom in atoms:
file.write(str(conf[atom][0]))
file.write(' ')
file.write('"\n')
file.write(' y3="')
for atom in atoms:
file.write(str(conf[atom][1]))
file.write(' ')
file.write('"\n')
file.write(' z3="')
for atom in atoms:
file.write(str(conf[atom][2]))
file.write(' ')
file.write('"\n')
file.write(' />\n')
file.write(' </configuration>\n')
file.write('</universe>\n\n')
file.write('</molecularsystem>\n')
#
# Infinite universes
#
class InfiniteUniverse(Universe):
"""
Infinite (unbounded and nonperiodic) universe.
"""
def __init__(self, forcefield=None, **properties):
"""
:param forcefield: a force field, or None for no force field
:type forcefield: :class:`~MMTK.ForceFields.ForceField.ForceField`
"""
Universe.__init__(self, forcefield, properties)
self._createSpec()
def CdistanceFunction(self):
from MMTK_universe import infinite_universe_distance_function
return infinite_universe_distance_function, N.array([0.])
def CcorrectionFunction(self):
from MMTK_universe import infinite_universe_correction_function
return infinite_universe_correction_function, N.array([0.])
def CvolumeFunction(self):
from MMTK_universe import infinite_universe_volume_function
return infinite_universe_volume_function, N.array([0.])
def CboxTransformationFunction(self):
return None, N.array([0.])
def _createSpec(self):
from MMTK_universe import InfiniteUniverseSpec
self._spec = InfiniteUniverseSpec()
def _descriptionArguments(self):
if self._forcefield is None:
return '()'
else:
return '(%s)' % self._forcefield.description()
def XMLSpec(self):
return 'topology="infinite"'
#
# 3D periodic universe base class
#
class Periodic3DUniverse(Universe):
is_periodic = True
def setVolume(self, volume):
"""
Multiplies all edge lengths by the same factor such that the cell
volume becomes equal to the specified value.
:param volume: the desired volume
:type volume: float
"""
factor = (volume/self.cellVolume())**(1./3.)
self.scaleSize(factor)
def foldCoordinatesIntoBox(self):
self._spec.foldCoordinatesIntoBox(self.configuration().array)
def basisVectors(self):
return [self.boxToRealCoordinates(Vector(1., 0., 0.)),
self.boxToRealCoordinates(Vector(0., 1., 0.)),
self.boxToRealCoordinates(Vector(0., 0., 1.))]
def cartesianToFractional(self, vector):
r1, r2, r3 = self.reciprocalBasisVectors()
return N.array([r1*vector, r2*vector, r3*vector])
def cartesianToFractionalMatrix(self):
return N.array(self.reciprocalBasisVectors())
def fractionalToCartesian(self, array):
e1, e2, e3 = self.basisVectors()
return array[0]*e1 + array[1]*e2 + array[2]*e3
def fractionalToCartesianMatrix(self):
return N.transpose(self.basisVectors())
def randomPoint(self):
return self.boxToRealCoordinates(Random.randomPointInBox(1., 1., 1.))
def contiguousObjectOffset(self, objects = None, conf = None,
box_coordinates = 0):
from MMTK_universe import contiguous_object_offset
if objects is None or objects == self or objects == [self]:
default = True
objects = self._objects.objectList()
pairs = self._bond_pairs
else:
default = False
pairs = None
if conf is None:
conf = self.configuration()
cell = self._fixCellParameters(conf.cell_parameters)
offset = ParticleProperties.ParticleVector(self)
if pairs is None:
pairs = []
for o in objects:
new_object = True
if ChemicalObjects.isChemicalObject(o):
units = o.bondedUnits()
elif Collections.isCollection(o) or isUniverse(o):
units = set([u
for element in o
for u in element.topLevelChemicalObject()
.bondedUnits()])
else:
raise ValueError(str(o) + " not a chemical object")
for bu in units:
atoms = [a.index for a in bu.atomsWithDefinedPositions()]
mpairs = bu.traverseBondTree(lambda a: a.index)
mpairs = [(a1, a2) for (a1, a2) in mpairs
if a1 in atoms and a2 in atoms]
if len(mpairs) == 0:
mpairs = Utility.pairs(atoms)
new_object = False
pairs.extend(mpairs)
pairs = N.array(pairs)
if default:
self._bond_pairs = pairs
if cell is None:
contiguous_object_offset(self._spec, pairs, conf.array,
offset.array, box_coordinates)
else:
contiguous_object_offset(self._spec, pairs, conf.array,
offset.array, box_coordinates, cell)
return offset
def _graphics(self, conf, distance_fn, model, module, options):
objects = self._objects._graphics(conf, distance_fn, model,
module, options)
v1, v2, v3 = self.basisVectors()
p = -0.5*(v1+v2+v3)
color = options.get('color', 'white')
material = module.EmissiveMaterial(color)
objects.append(module.Line(p, p+v1, material=material))
objects.append(module.Line(p, p+v2, material=material))
objects.append(module.Line(p+v1, p+v1+v2, material=material))
objects.append(module.Line(p+v2, p+v1+v2, material=material))
objects.append(module.Line(p, p+v3, material=material))
objects.append(module.Line(p+v1, p+v1+v3, material=material))
objects.append(module.Line(p+v2, p+v2+v3, material=material))
objects.append(module.Line(p+v1+v2, p+v1+v2+v3, material=material))
objects.append(module.Line(p+v3, p+v1+v3, material=material))
objects.append(module.Line(p+v3, p+v2+v3, material=material))
objects.append(module.Line(p+v1+v3, p+v1+v2+v3, material=material))
objects.append(module.Line(p+v2+v3, p+v1+v2+v3, material=material))
return objects
#
# Orthorhombic universe with periodic boundary conditions
#
class OrthorhombicPeriodicUniverse(Periodic3DUniverse):
"""
Periodic universe with orthorhombic elementary cell.
"""
def __init__(self, size = None, forcefield = None, **properties):
"""
:param size: a sequence of length three specifying the edge
lengths along the x, y, and z directions
:param forcefield: a force field, or None for no force field
:type forcefield: :class:`~MMTK.ForceFields.ForceField.ForceField`
"""
Universe.__init__(self, forcefield, properties)
self.data = N.zeros((3,), N.Float)
if size is not None:
self.setSize(size)
self._createSpec()
is_orthogonal = True
def __setstate__(self, state):
Universe.__setstate__(self, state)
if len(self.data.shape) == 2:
self.data = self.data[0]
def setSize(self, size):
self.data[:] = size
def scaleSize(self, factor):
"""
Multiplies all edge lengths by a factor.
:param factor: the scale factor
:type factor: float
"""
self.data[:] = factor*self.data
self._spec.foldCoordinatesIntoBox(self.configuration().array)
def setCellParameters(self, parameters):
if parameters is not None:
self.data[:] = parameters
def realToBoxCoordinates(self, vector):
x, y, z = vector
return Vector(x/self.data[0],
y/self.data[1],
z/self.data[2])
def boxToRealCoordinates(self, vector):
x, y, z = vector
return Vector(x*self.data[0],
y*self.data[1],
z*self.data[2])
def _realToBoxPointArray(self, array, parameters=None):
if parameters is None:
parameters = self.data
if parameters.shape == (3,):
parameters = parameters[N.NewAxis, :]
return array/parameters
def _boxToRealPointArray(self, array, parameters=None):
if parameters is None:
parameters = self.data
if parameters.shape == (3,):
parameters = parameters[N.NewAxis, :]
return array*parameters
def CdistanceFunction(self):
from MMTK_universe import orthorhombic_universe_distance_function
return orthorhombic_universe_distance_function, self.data
def CcorrectionFunction(self):
from MMTK_universe import orthorhombic_universe_correction_function
return orthorhombic_universe_correction_function, self.data
def CvolumeFunction(self):
from MMTK_universe import orthorhombic_universe_volume_function
return orthorhombic_universe_volume_function, self.data
def CboxTransformationFunction(self):
from MMTK_universe import orthorhombic_universe_box_transformation
return orthorhombic_universe_box_transformation, self.data
def cellParameters(self):
return self.data
def reciprocalBasisVectors(self):
return [Vector(1., 0., 0.)/self.data[0],
Vector(0., 1., 0.)/self.data[1],
Vector(0., 0., 1.)/self.data[2]]
def cellVolume(self):
return N.multiply.reduce(self.data)
def largestDistance(self):
return 0.5*N.minimum.reduce(self.data)
def _createSpec(self):
from MMTK_universe import OrthorhombicPeriodicUniverseSpec
self._spec = OrthorhombicPeriodicUniverseSpec(self.data)
def _descriptionArguments(self):
if self._forcefield is None:
return '((0.,0.,0.),)'
else:
return '((0.,0.,0.),%s)' % self._forcefield.description()
def XMLSpec(self):
return 'topology="periodic3d" ' + \
'cellshape="orthorhombic" ' + \
('cellsize="%f %f %f" ' % tuple(self.data)) + \
'units="units:nm"'
#
# Cubic universe with periodic boundary conditions
#
class CubicPeriodicUniverse(OrthorhombicPeriodicUniverse):
"""
Periodic universe with cubic elementary cell.
"""
def setSize(self, size):
"""
Set the edge length to a given value.
:param size: the new size
:type size: float
"""
OrthorhombicPeriodicUniverse.setSize(self, 3*(size,))
def _descriptionArguments(self):
if self._forcefield is None:
return '(0.)'
else:
return '(0.,%s)' % self._forcefield.description()
#
# Parallelepipedic universe with periodic boundary conditions
#
class ParallelepipedicPeriodicUniverse(Periodic3DUniverse):
"""
Periodic universe with parallelepipedic elementary cell.
"""
def __init__(self, shape = None, forcefield = None, **properties):
"""
:param shape: the basis vectors
:type shape: sequence of Scientific.Geometry.Vector
:param forcefield: a force field, or None for no force field
:type forcefield: :class:`~MMTK.ForceFields.ForceField.ForceField`
"""
Universe.__init__(self, forcefield, properties)
self.data = N.zeros((19,), N.Float)
if shape is not None:
self.setShape(shape)
self._createSpec()
is_periodic = True
def setShape(self, shape):
self.data[:9] = N.ravel(N.transpose([list(s) for s in shape]))
from MMTK_universe import parallelepiped_invert
parallelepiped_invert(self.data)
def scaleSize(self, factor):
"""
Multiplies all edge lengths by a factor.
:param factor: the scale factor
:type factor: float
"""
self.data[:9] = factor*self.data[:9]
from MMTK_universe import parallelepiped_invert
parallelepiped_invert(self.data)
self._spec.foldCoordinatesIntoBox(self.configuration().array)
def setCellParameters(self, parameters):
if parameters is not None:
self.data[:9] = parameters
from MMTK_universe import parallelepiped_invert
parallelepiped_invert(self.data)
def _fixCellParameters(self, cell_parameters):
full_parameters = 0.*self.data
full_parameters[:9] = cell_parameters
from MMTK_universe import parallelepiped_invert
parallelepiped_invert(full_parameters)
return full_parameters
def realToBoxCoordinates(self, vector):
x, y, z = vector
return Vector(self.data[0+9]*x + self.data[1+9]*y + self.data[2+9]*z,
self.data[3+9]*x + self.data[4+9]*y + self.data[5+9]*z,
self.data[6+9]*x + self.data[7+9]*y + self.data[8+9]*z)
def boxToRealCoordinates(self, vector):
x, y, z = vector
return Vector(self.data[0]*x + self.data[1]*y + self.data[2]*z,
self.data[3]*x + self.data[4]*y + self.data[5]*z,
self.data[6]*x + self.data[7]*y + self.data[8]*z)
def _realToBoxPointArray(self, array, parameters=None):
if parameters is None:
matrix = N.reshape(self.data[9:18], (1, 3, 3))
else:
parameters = N.concatenate([parameters, N.zeros((10,), N.Float)])
from MMTK_universe import parallelepiped_invert
parallelepiped_invert(parameters)
matrix = N.reshape(parameters[9:18], (1, 3, 3))
return N.add.reduce(matrix*array[:, N.NewAxis, :], axis=-1)
def _boxToRealPointArray(self, array, parameters=None):
if parameters is None:
parameters = self.data[:9]
matrix = N.reshape(parameters, (1, 3, 3))
return N.add.reduce(matrix*array[:, N.NewAxis, :], axis=-1)
def CdistanceFunction(self):
from MMTK_universe import parallelepipedic_universe_distance_function
return parallelepipedic_universe_distance_function, self.data
def CcorrectionFunction(self):
from MMTK_universe import parallelepipedic_universe_correction_function
return parallelepipedic_universe_correction_function, self.data
def CvolumeFunction(self):
from MMTK_universe import parallelepipedic_universe_volume_function
return parallelepipedic_universe_volume_function, self.data
def CboxTransformationFunction(self):
from MMTK_universe import parallelepipedic_universe_box_transformation
return parallelepipedic_universe_box_transformation, self.data
def cellParameters(self):
return self.data[:9]
def reciprocalBasisVectors(self):
return [Vector(self.data[9:12]),
Vector(self.data[12:15]),
Vector(self.data[15:18])]
def cellVolume(self):
return abs(self.data[18])
def largestDistance(self):
return min([0.5/v.length() for v in self.reciprocalBasisVectors()])
def _createSpec(self):
from MMTK_universe import ParallelepipedicPeriodicUniverseSpec
self._spec = ParallelepipedicPeriodicUniverseSpec(self.data)
def _descriptionArguments(self):
if self._forcefield is None:
return '((Vector(0.,0.,0.),Vector(0.,0.,0.),Vector(0.,0.,0.)))'
else:
return '((Vector(0.,0.,0.),Vector(0.,0.,0.),Vector(0.,0.,0.)),%s)'\
% self._forcefield.description()
def XMLSpec(self):
return 'topology="periodic3d" ' + \
'cellshape="parallelepipedic" ' + \
('cellshape="%f %f %f %f %f %f %f %f %f" '
% tuple(self.data[:9])) + \
'units="units:nm"'
#
# Recognition functions
#
def isUniverse(object):
"""
:param object: any Python object
:returns: True if object is a universe.
"""
return isinstance(object, Universe)
|