OpenMPCD
test_StarPolymers.py
1 def getConfig():
2  from MPCDAnalysis.Configuration import Configuration
3 
4  config = """
5  structure:
6  {
7  starCount = 1
8  armCountPerStar = 5
9  armParticlesPerArm = 3
10  hasMagneticParticles = true
11  particleMass = 4.5
12  }
13 
14  interactionParameters:
15  {
16  epsilon_core = 1.0
17  epsilon_arm = 1.1
18  epsilon_magnetic = 1.2
19 
20  sigma_core = 3.05
21  sigma_arm = 3.06
22  sigma_magnetic = 3.07
23 
24  D_core = 2.15
25  D_arm = 0.01
26  D_magnetic = 1.01
27 
28  magneticPrefactor = 100.7
29 
30  dipoleOrientation = [1.0, 0.0, 0.0]
31  }
32  """
33 
34  return Configuration(config)
35 
36 
37 def getParticles(filename = "test_StarPolymers_particles.vtf"):
38  from MPCDAnalysis.ParticleCollection import ParticleCollection
39  from MPCDAnalysis.Vector3DReal import Vector3DReal
40 
41  import os.path
42  particlePath = os.path.dirname(os.path.abspath(__file__))
43  particlePath += "/data/" + filename
44 
45  positions = []
46  velocities = []
47  with open(particlePath, "r") as vtf:
48  for line in vtf:
49  if line.startswith("atom"):
50  continue
51  if line.startswith("bond"):
52  continue
53  if line.startswith("timestep"):
54  continue
55  components = line.split()
56  pos = [float(x) for x in components[0:3]]
57  vel = [float(x) for x in components[3:6]]
58 
59  positions.append(Vector3DReal(pos))
60  velocities.append(Vector3DReal(vel))
61 
62  ret = ParticleCollection()
63  ret.setPositionsAndVelocities(positions, velocities)
64  return ret
65 
66 
67 from MPCDAnalysis.StarPolymers import StarPolymers
68 
69 def test_constructor():
70  import pytest
71 
72  with pytest.raises(NotImplementedError):
73  config = getConfig()
74  config["structure.starCount"] = 2
75  StarPolymers(config)
76 
77 
78  with pytest.raises(TypeError):
79  StarPolymers(0)
80 
81  with pytest.raises(TypeError):
82  StarPolymers({"structure.starCount": 1})
83 
84  with pytest.raises(TypeError):
85  StarPolymers(None)
86 
87  config = getConfig()
88  sp = StarPolymers(config)
89 
90  assert sp.getArmCountPerStar() == 5
91  config["structure.armCountPerStar"] = 2
92  assert sp.getArmCountPerStar() == 5
93 
94 def test_getStarCount():
95  sp = StarPolymers(getConfig())
96 
97  assert sp.getStarCount() == 1
98  assert sp.getStarCount() == 1
99 
100 
101 def test_getArmCountPerStar():
102  sp = StarPolymers(getConfig())
103 
104  assert sp.getArmCountPerStar() == 5
105  assert sp.getArmCountPerStar() == 5
106 
107 
108 def test_getArmParticlesPerArm():
109  sp = StarPolymers(getConfig())
110 
111  assert sp.getArmParticlesPerArm() == 3
112  assert sp.getArmParticlesPerArm() == 3
113 
114 
115 def test_getTotalParticleCountPerArm():
116  sp = StarPolymers(getConfig())
117 
118  assert sp.getTotalParticleCountPerArm() == 3 + 1
119  assert sp.getTotalParticleCountPerArm() == 3 + 1
120 
121 
122 def test_hasMagneticParticles():
123  sp = StarPolymers(getConfig())
124 
125  assert sp.hasMagneticParticles() == True
126  assert sp.hasMagneticParticles() == True
127 
128 
129 def test_getParticleMass():
130  sp = StarPolymers(getConfig())
131 
132  assert sp.getParticleMass() == 4.5
133  assert sp.getParticleMass() == 4.5
134 
135 
136 def test_getTotalParticleCountPerStar():
137  sp = StarPolymers(getConfig())
138 
139  assert sp.getTotalParticleCountPerStar() == 1 + 5 * (3 + 1)
140 
141 
142 def test_getTotalParticleCount():
143  sp = StarPolymers(getConfig())
144 
145  expected = sp.getStarCount() * sp.getTotalParticleCountPerStar()
146  assert sp.getTotalParticleCount() == expected
147  assert sp.getTotalParticleCount() == expected
148 
149 
150 def test_getParticleType():
151  import pytest
152 
153  sp = StarPolymers(getConfig())
154 
155  assert sp.getTotalParticleCount() == 21
156 
157  with pytest.raises(IndexError):
158  sp.getParticleType(-1)
159 
160  with pytest.raises(IndexError):
161  sp.getParticleType(sp.getTotalParticleCount())
162 
163  with pytest.raises(IndexError):
164  sp.getParticleType(sp.getTotalParticleCount() + 1)
165 
166  with pytest.raises(TypeError):
167  sp.getParticleType(0.0)
168 
169 
170  for _ in range(0, 3): #repeat to check caching
171  assert sp.getParticleType(0) == "Core"
172 
173  assert sp.getParticleType(1) == "Arm"
174  assert sp.getParticleType(2) == "Arm"
175  assert sp.getParticleType(3) == "Arm"
176  assert sp.getParticleType(4) == "Magnetic"
177 
178  assert sp.getParticleType(5) == "Arm"
179  assert sp.getParticleType(6) == "Arm"
180  assert sp.getParticleType(7) == "Arm"
181  assert sp.getParticleType(8) == "Magnetic"
182 
183  assert sp.getParticleType(9) == "Arm"
184  assert sp.getParticleType(10) == "Arm"
185  assert sp.getParticleType(11) == "Arm"
186  assert sp.getParticleType(12) == "Magnetic"
187 
188  assert sp.getParticleType(13) == "Arm"
189  assert sp.getParticleType(14) == "Arm"
190  assert sp.getParticleType(15) == "Arm"
191  assert sp.getParticleType(16) == "Magnetic"
192 
193  assert sp.getParticleType(17) == "Arm"
194  assert sp.getParticleType(18) == "Arm"
195  assert sp.getParticleType(19) == "Arm"
196  assert sp.getParticleType(20) == "Magnetic"
197 
198 
199 def test_getParticleStructureIndices():
200  import pytest
201 
202  sp = StarPolymers(getConfig())
203 
204  assert sp.getTotalParticleCount() == 21
205 
206  with pytest.raises(IndexError):
207  sp.getParticleStructureIndices(-1)
208 
209  with pytest.raises(IndexError):
210  sp.getParticleStructureIndices(sp.getTotalParticleCount())
211 
212  with pytest.raises(IndexError):
213  sp.getParticleStructureIndices(sp.getTotalParticleCount() + 1)
214 
215  with pytest.raises(TypeError):
216  sp.getParticleStructureIndices(0.0)
217 
218  for _ in range(0, 3): #repeat to check caching
219  assert sp.getParticleStructureIndices(0) == [0, None, None]
220 
221  assert sp.getParticleStructureIndices(1) == [0, 0, 0]
222  assert sp.getParticleStructureIndices(2) == [0, 0, 1]
223  assert sp.getParticleStructureIndices(3) == [0, 0, 2]
224  assert sp.getParticleStructureIndices(4) == [0, 0, None]
225 
226  assert sp.getParticleStructureIndices(5) == [0, 1, 0]
227  assert sp.getParticleStructureIndices(6) == [0, 1, 1]
228  assert sp.getParticleStructureIndices(7) == [0, 1, 2]
229  assert sp.getParticleStructureIndices(8) == [0, 1, None]
230 
231  assert sp.getParticleStructureIndices(9) == [0, 2, 0]
232  assert sp.getParticleStructureIndices(10) == [0, 2, 1]
233  assert sp.getParticleStructureIndices(11) == [0, 2, 2]
234  assert sp.getParticleStructureIndices(12) == [0, 2, None]
235 
236  assert sp.getParticleStructureIndices(13) == [0, 3, 0]
237  assert sp.getParticleStructureIndices(14) == [0, 3, 1]
238  assert sp.getParticleStructureIndices(15) == [0, 3, 2]
239  assert sp.getParticleStructureIndices(16) == [0, 3, None]
240 
241  assert sp.getParticleStructureIndices(17) == [0, 4, 0]
242  assert sp.getParticleStructureIndices(18) == [0, 4, 1]
243  assert sp.getParticleStructureIndices(19) == [0, 4, 2]
244  assert sp.getParticleStructureIndices(20) == [0, 4, None]
245 
246 
247 def test_particlesAreBonded():
248  import pytest
249 
250  sp = StarPolymers(getConfig())
251 
252  assert sp.getTotalParticleCount() == 21
253 
254  with pytest.raises(IndexError):
255  sp.particlesAreBonded(-1, 0)
256 
257  with pytest.raises(IndexError):
258  sp.particlesAreBonded(0, -1)
259 
260  with pytest.raises(IndexError):
261  sp.particlesAreBonded(sp.getTotalParticleCount(), 0)
262 
263  with pytest.raises(IndexError):
264  sp.particlesAreBonded(0, sp.getTotalParticleCount())
265 
266  with pytest.raises(IndexError):
267  sp.particlesAreBonded(sp.getTotalParticleCount() + 1, 0)
268 
269  with pytest.raises(IndexError):
270  sp.particlesAreBonded(0, sp.getTotalParticleCount() + 1)
271 
272  with pytest.raises(TypeError):
273  sp.particlesAreBonded(0.0, 0)
274 
275  with pytest.raises(TypeError):
276  sp.particlesAreBonded(0, 0.0)
277 
278 
279  for pID1 in range(0, sp.getTotalParticleCount()):
280  assert sp.particlesAreBonded(pID1, pID1) == False
281 
282  for pID2 in range(pID1 + 1, sp.getTotalParticleCount()):
283  assert sp.particlesAreBonded(pID1, pID2) \
284  == sp.particlesAreBonded(pID2, pID1)
285 
286  indices1 = sp.getParticleStructureIndices(pID1)
287  indices2 = sp.getParticleStructureIndices(pID2)
288 
289  if indices1[0] != indices2[0]:
290  assert sp.particlesAreBonded(pID1, pID2) == False
291  continue
292 
293  if sp.getParticleType(pID1) == "Core":
294  assert sp.hasMagneticParticles()
295  bonded = indices2[2] == 0
296  assert sp.particlesAreBonded(pID1, pID2) == bonded
297  elif sp.getParticleType(pID1) == "Magnetic":
298  #since pID2 > pID1, pID2 must be on a different arm:
299  assert sp.particlesAreBonded(pID1, pID2) == False
300  else:
301  if indices1[1] == indices2[1]:
302  bonded = pID2 - pID1 == 1 #using pID2 > pID1
303  assert sp.particlesAreBonded(pID1, pID2) == bonded
304  else:
305  assert sp.particlesAreBonded(pID1, pID2) == False
306 
307 
308 def test_setParticles():
309  from MPCDAnalysis.ParticleCollection import ParticleCollection
310  from MPCDAnalysis.Vector3DReal import Vector3DReal
311  import pytest
312 
313  sp = StarPolymers(getConfig())
314 
315  particleCount = sp.getTotalParticleCount()
316  assert particleCount == 21
317 
318  with pytest.raises(TypeError):
319  sp.setParticles(None)
320 
321  with pytest.raises(TypeError):
322  sp.setParticles([[0.0, 1.0, 2.0] for _ in range(0, 21)])
323 
324  with pytest.raises(ValueError):
325  particles = ParticleCollection()
326  positions = [Vector3DReal(i, 0, 0) for i in range(0, particleCount - 1)]
327  velocities = positions
328  particles.setPositionsAndVelocities(positions, velocities)
329  sp.setParticles(particles)
330 
331 
332  particles = getParticles()
333  sp.setParticles(particles)
334 
335  assert sp._particles is particles
336 
337 
338 def test_getMagneticClusters_getMagneticClusterCount():
339  import pytest
340 
341  particles = getParticles()
342 
343  def test_raiseBecauseNoMagnets():
344  config = getConfig()
345  config["structure.hasMagneticParticles"] = False
346  config["structure.armParticlesPerArm"] = \
347  config["structure.armParticlesPerArm"] + 1
348  mysp = StarPolymers(config)
349 
350  with pytest.raises(ValueError):
351  mysp.getMagneticClusters(2.5)
352 
353  with pytest.raises(ValueError):
354  mysp.getMagneticClusterCount(2.5)
355  test_raiseBecauseNoMagnets()
356 
357 
358  sp = StarPolymers(getConfig())
359  assert sp.getTotalParticleCount() == 21
360 
361  with pytest.raises(ValueError):
362  sp.getMagneticClusters(2.5)
363  with pytest.raises(ValueError):
364  sp.getMagneticClusterCount(2.5)
365 
366 
367  sp.setParticles(particles)
368 
369 
370  with pytest.raises(TypeError):
371  sp.getMagneticClusters([1])
372  with pytest.raises(TypeError):
373  sp.getMagneticClusterCount([1])
374 
375  with pytest.raises(ValueError):
376  sp.getMagneticClusters(-1)
377  with pytest.raises(ValueError):
378  sp.getMagneticClusterCount(-1)
379 
380  with pytest.raises(ValueError):
381  sp.getMagneticClusters(-1.0)
382  with pytest.raises(ValueError):
383  sp.getMagneticClusterCount(-1.0)
384 
385 
386  magnets = []
387  for i in [4, 8, 12, 16, 20]:
388  assert sp.getParticleType(i) == "Magnetic"
389  magnets.append(particles.getPosition(i))
390 
391  def indexByPosition(position):
392  for index, pos in enumerate(magnets):
393  if pos is position:
394  return index
395  raise RuntimeError()
396 
397 
398  expectedClustersCollection = \
399  {
400  0: [ [0], [1], [2], [3], [4] ],
401  0.8: [ [0], [1], [2], [3], [4] ],
402  0.9: [ [0, 3], [1], [2], [4] ],
403  3: [ [0, 3], [1], [2], [4] ],
404  3.3: [ [0, 3, 4], [1], [2] ],
405  7.8: [ [0, 3, 4], [1, 2] ],
406  7.9: [ [0, 1, 2, 3, 4] ],
407  100: [ [0, 1, 2, 3, 4] ]
408  }
409 
410 
411  for distance, expectedClusters in expectedClustersCollection.items():
412  assert len(expectedClusters) == sp.getMagneticClusterCount(distance)
413 
414  clusterSet = set()
415  for cluster in sp.getMagneticClusters(distance):
416  indices = [indexByPosition(pos) for pos in cluster]
417  clusterSet.add(frozenset(indices))
418 
419  expectedClusterSet = set()
420  for cluster in expectedClusters:
421  expectedClusterSet.add(frozenset(cluster))
422 
423  assert clusterSet == expectedClusterSet
424 
425 
426 def test_getWCAPotentialParameterEpsilon():
427  import pytest
428 
429  config = getConfig()
430  config_nomagnetic = getConfig()
431  config_nomagnetic["structure.hasMagneticParticles"] = False
432  sp = StarPolymers(config)
433  sp_nomagnetic = StarPolymers(config_nomagnetic)
434 
435  with pytest.raises(TypeError):
436  sp.getWCAPotentialParameterEpsilon(0, "Core")
437  with pytest.raises(TypeError):
438  sp.getWCAPotentialParameterEpsilon("Core", 0)
439 
440  with pytest.raises(ValueError):
441  sp.getWCAPotentialParameterEpsilon("Core", "Foo")
442  with pytest.raises(ValueError):
443  sp.getWCAPotentialParameterEpsilon("Foo", "Core")
444  with pytest.raises(ValueError):
445  sp_nomagnetic.getWCAPotentialParameterEpsilon("Core", "Magnetic")
446  with pytest.raises(ValueError):
447  sp_nomagnetic.getWCAPotentialParameterEpsilon("Magnetic", "Core")
448 
449 
450  epsilon_C = config["interactionParameters.epsilon_core"]
451  epsilon_A = config["interactionParameters.epsilon_arm"]
452  epsilon_M = config["interactionParameters.epsilon_magnetic"]
453 
454  epsilon_CC = epsilon_C
455  epsilon_AA = epsilon_A
456  epsilon_MM = epsilon_M
457 
458  import math
459  epsilon_CA = math.sqrt(epsilon_C * epsilon_A)
460  epsilon_CM = math.sqrt(epsilon_C * epsilon_M)
461  epsilon_AM = math.sqrt(epsilon_A * epsilon_M)
462 
463  assert sp.getWCAPotentialParameterEpsilon("Core", "Core") == epsilon_CC
464  assert sp.getWCAPotentialParameterEpsilon("Core", "Arm") == epsilon_CA
465  assert sp.getWCAPotentialParameterEpsilon("Core", "Magnetic") == epsilon_CM
466 
467  assert sp.getWCAPotentialParameterEpsilon("Arm", "Core") == epsilon_CA
468  assert sp.getWCAPotentialParameterEpsilon("Arm", "Arm") == epsilon_AA
469  assert sp.getWCAPotentialParameterEpsilon("Arm", "Magnetic") == epsilon_AM
470 
471  assert sp.getWCAPotentialParameterEpsilon("Magnetic", "Core") == epsilon_CM
472  assert sp.getWCAPotentialParameterEpsilon("Magnetic", "Arm") == epsilon_AM
473  assert sp.getWCAPotentialParameterEpsilon("Magnetic", "Magnetic") == \
474  epsilon_MM
475 
476 
477 def test_getWCAPotentialParameterSigma():
478  import pytest
479 
480  config = getConfig()
481  config_nomagnetic = getConfig()
482  config_nomagnetic["structure.hasMagneticParticles"] = False
483  sp = StarPolymers(config)
484  sp_nomagnetic = StarPolymers(config_nomagnetic)
485 
486  with pytest.raises(TypeError):
487  sp.getWCAPotentialParameterSigma(0, "Core")
488  with pytest.raises(TypeError):
489  sp.getWCAPotentialParameterSigma("Core", 0)
490 
491  with pytest.raises(ValueError):
492  sp.getWCAPotentialParameterSigma("Core", "Foo")
493  with pytest.raises(ValueError):
494  sp.getWCAPotentialParameterSigma("Foo", "Core")
495  with pytest.raises(ValueError):
496  sp_nomagnetic.getWCAPotentialParameterSigma("Core", "Magnetic")
497  with pytest.raises(ValueError):
498  sp_nomagnetic.getWCAPotentialParameterSigma("Magnetic", "Core")
499 
500 
501  sigma_C = config["interactionParameters.sigma_core"]
502  sigma_A = config["interactionParameters.sigma_arm"]
503  sigma_M = config["interactionParameters.sigma_magnetic"]
504 
505  sigma_CC = sigma_C
506  sigma_AA = sigma_A
507  sigma_MM = sigma_M
508 
509  sigma_CA = (sigma_C + sigma_A) / 2.0
510  sigma_CM = (sigma_C + sigma_M) / 2.0
511  sigma_AM = (sigma_A + sigma_M) / 2.0
512 
513  assert sp.getWCAPotentialParameterSigma("Core", "Core") == sigma_CC
514  assert sp.getWCAPotentialParameterSigma("Core", "Arm") == sigma_CA
515  assert sp.getWCAPotentialParameterSigma("Core", "Magnetic") == sigma_CM
516 
517  assert sp.getWCAPotentialParameterSigma("Arm", "Core") == sigma_CA
518  assert sp.getWCAPotentialParameterSigma("Arm", "Arm") == sigma_AA
519  assert sp.getWCAPotentialParameterSigma("Arm", "Magnetic") == sigma_AM
520 
521  assert sp.getWCAPotentialParameterSigma("Magnetic", "Core") == sigma_CM
522  assert sp.getWCAPotentialParameterSigma("Magnetic", "Arm") == sigma_AM
523  assert sp.getWCAPotentialParameterSigma("Magnetic", "Magnetic") == sigma_MM
524 
525 
526 def test_getWCAPotentialParameterD():
527  import pytest
528 
529  config = getConfig()
530  config_nomagnetic = getConfig()
531  config_nomagnetic["structure.hasMagneticParticles"] = False
532  sp = StarPolymers(config)
533  sp_nomagnetic = StarPolymers(config_nomagnetic)
534 
535  with pytest.raises(TypeError):
536  sp.getWCAPotentialParameterD(0, "Core")
537  with pytest.raises(TypeError):
538  sp.getWCAPotentialParameterD("Core", 0)
539 
540  with pytest.raises(ValueError):
541  sp.getWCAPotentialParameterD("Core", "Foo")
542  with pytest.raises(ValueError):
543  sp.getWCAPotentialParameterD("Foo", "Core")
544  with pytest.raises(ValueError):
545  sp_nomagnetic.getWCAPotentialParameterD("Core", "Magnetic")
546  with pytest.raises(ValueError):
547  sp_nomagnetic.getWCAPotentialParameterD("Magnetic", "Core")
548 
549 
550  D_C = config["interactionParameters.D_core"]
551  D_A = config["interactionParameters.D_arm"]
552  D_M = config["interactionParameters.D_magnetic"]
553 
554  D_CC = D_C
555  D_AA = D_A
556  D_MM = D_M
557 
558  D_CA = (D_C + D_A) / 2.0
559  D_CM = (D_C + D_M) / 2.0
560  D_AM = (D_A + D_M) / 2.0
561 
562  assert sp.getWCAPotentialParameterD("Core", "Core") == D_CC
563  assert sp.getWCAPotentialParameterD("Core", "Arm") == D_CA
564  assert sp.getWCAPotentialParameterD("Core", "Magnetic") == D_CM
565 
566  assert sp.getWCAPotentialParameterD("Arm", "Core") == D_CA
567  assert sp.getWCAPotentialParameterD("Arm", "Arm") == D_AA
568  assert sp.getWCAPotentialParameterD("Arm", "Magnetic") == D_AM
569 
570  assert sp.getWCAPotentialParameterD("Magnetic", "Core") == D_CM
571  assert sp.getWCAPotentialParameterD("Magnetic", "Arm") == D_AM
572  assert sp.getWCAPotentialParameterD("Magnetic", "Magnetic") == D_MM
573 
574 
575 def test_getWCAPotential():
576  import pytest
577 
578  config = getConfig()
579  config_nomagnetic = getConfig()
580  config_nomagnetic["structure.hasMagneticParticles"] = False
581  sp = StarPolymers(config)
582  sp_nomagnetic = StarPolymers(config_nomagnetic)
583 
584  with pytest.raises(TypeError):
585  sp.getWCAPotential(0, "Core")
586  with pytest.raises(TypeError):
587  sp.getWCAPotential("Core", 0)
588 
589  with pytest.raises(ValueError):
590  sp.getWCAPotential("Core", "Foo")
591  with pytest.raises(ValueError):
592  sp.getWCAPotential("Foo", "Core")
593  with pytest.raises(ValueError):
594  sp_nomagnetic.getWCAPotential("Core", "Magnetic")
595  with pytest.raises(ValueError):
596  sp_nomagnetic.getWCAPotential("Magnetic", "Core")
597 
598 
600  import WeeksChandlerAndersen_DistanceOffset as WCA
601  types = ["Core", "Arm", "Magnetic"]
602  for type1 in types:
603  for type2 in types:
604  epsilon = sp.getWCAPotentialParameterEpsilon(type1, type2)
605  sigma = sp.getWCAPotentialParameterSigma(type1, type2)
606  d = sp.getWCAPotentialParameterD(type1, type2)
607 
608  wca = sp.getWCAPotential(type1, type2)
609 
610  assert isinstance(wca, WCA)
611  assert wca.getEpsilon() == epsilon
612  assert wca.getSigma() == sigma
613  assert wca.getD() == d
614 
615  #check that caching works:
616  wca = sp.getWCAPotential(type1, type2)
617 
618  assert isinstance(wca, WCA)
619  assert wca.getEpsilon() == epsilon
620  assert wca.getSigma() == sigma
621  assert wca.getD() == d
622 
623 
624 def test_getFENEPotential():
625  import pytest
626 
627  config = getConfig()
628  config_nomagnetic = getConfig()
629  config_nomagnetic["structure.hasMagneticParticles"] = False
630  sp = StarPolymers(config)
631  sp_nomagnetic = StarPolymers(config_nomagnetic)
632 
633  with pytest.raises(TypeError):
634  sp.getFENEPotential(0, "Core")
635  with pytest.raises(TypeError):
636  sp.getFENEPotential("Core", 0)
637 
638  with pytest.raises(ValueError):
639  sp.getFENEPotential("Core", "Foo")
640  with pytest.raises(ValueError):
641  sp.getFENEPotential("Foo", "Core")
642  with pytest.raises(ValueError):
643  sp_nomagnetic.getFENEPotential("Core", "Magnetic")
644  with pytest.raises(ValueError):
645  sp_nomagnetic.getFENEPotential("Magnetic", "Core")
646 
647 
648  from MPCDAnalysis.PairPotentials.FENE import FENE
649 
650  types = ["Core", "Arm", "Magnetic"]
651  for type1 in types:
652  for type2 in types:
653  epsilon = sp.getWCAPotentialParameterEpsilon(type1, type2)
654  sigma = sp.getWCAPotentialParameterSigma(type1, type2)
655  d = sp.getWCAPotentialParameterD(type1, type2)
656 
657  fene = sp.getFENEPotential(type1, type2)
658 
659  assert isinstance(fene, FENE)
660  assert fene.getK() == pytest.approx(30 * epsilon * sigma ** -2)
661  assert fene.get_l_0() == d
662  assert fene.getR() == 1.5 * sigma
663 
664  #check that caching works:
665  fene = sp.getFENEPotential(type1, type2)
666 
667  assert isinstance(fene, FENE)
668  assert fene.getK() == pytest.approx(30 * epsilon * sigma ** -2)
669  assert fene.get_l_0() == d
670  assert fene.getR() == 1.5 * sigma
671 
672 
673 def test_getMagneticPotential():
674  import pytest
675 
676  config = getConfig()
677  config_nomagnetic = getConfig()
678  config_nomagnetic["structure.hasMagneticParticles"] = False
679  sp = StarPolymers(config)
680  sp_nomagnetic = StarPolymers(config_nomagnetic)
681 
682  with pytest.raises(RuntimeError):
683  sp_nomagnetic.getMagneticPotential()
684 
685 
687  import MagneticDipoleDipoleInteraction_ConstantIdenticalDipoles \
688  as Potential
689  from MPCDAnalysis.Vector3DReal import Vector3DReal
690 
691  prefactor = config["interactionParameters.magneticPrefactor"]
692  orientationX = config["interactionParameters.dipoleOrientation.[0]"]
693  orientationY = config["interactionParameters.dipoleOrientation.[1]"]
694  orientationZ = config["interactionParameters.dipoleOrientation.[2]"]
695  orientation = Vector3DReal(orientationX, orientationY, orientationZ)
696 
697  potential = sp.getMagneticPotential()
698 
699  assert isinstance(potential, Potential)
700  assert potential.getPrefactor() == prefactor
701  assert potential.getOrientation() == orientation
702 
703  #check that caching works:
704  potential = sp.getMagneticPotential()
705 
706  assert isinstance(potential, Potential)
707  assert potential.getPrefactor() == prefactor
708  assert potential.getOrientation() == orientation
709 
710 
711 def test_getPotentialEnergy():
712  import pytest
713 
714  particles = getParticles("test_StarPolymers_particles_2.vtf")
715 
716  sp = StarPolymers(getConfig())
717  assert sp.getStarCount() == 1
718  assert sp.getTotalParticleCount() == 21
719 
720  with pytest.raises(ValueError):
721  sp.getPotentialEnergy()
722 
723  wca_CA = sp.getWCAPotential("Core", "Arm")
724  wca_CM = sp.getWCAPotential("Core", "Magnetic")
725  wca_AA = sp.getWCAPotential("Arm", "Arm")
726  wca_AM = sp.getWCAPotential("Arm", "Magnetic")
727  wca_MM = sp.getWCAPotential("Magnetic", "Magnetic")
728 
729  fene_CA = sp.getFENEPotential("Core", "Arm")
730  fene_AA = sp.getFENEPotential("Arm", "Arm")
731  fene_AM = sp.getFENEPotential("Arm", "Magnetic")
732 
733  magnetic = sp.getMagneticPotential()
734 
735  arms = [ [1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15], [17, 18, 19]]
736  magneticParticles = [4, 8, 12, 16, 20]
737 
738  for arm in arms:
739  for p in arm:
740  assert sp.getParticleType(p) == "Arm"
741  for p in magneticParticles:
742  assert sp.getParticleType(p) == "Magnetic"
743 
744 
745  sp.setParticles(particles)
746 
747  expected = 0.0
748 
749  pos_C = particles.getPosition(0)
750  for arm in arms:
751  for i, p in enumerate(arm):
752  r = pos_C - particles.getPosition(p)
753 
754  expected += wca_CA.getPotential(r)
755  if i == 0:
756  expected += fene_CA.getPotential(r)
757  for p in magneticParticles:
758  r = pos_C - particles.getPosition(p)
759  expected += wca_CM.getPotential(r)
760 
761 
762  for arm1 in arms:
763  for arm2 in arms:
764  for p1 in arm1:
765  pos1 = particles.getPosition(p1)
766  for p2 in arm2:
767  if p2 <= p1:
768  continue
769  r = pos1 - particles.getPosition(p2)
770 
771  expected += wca_AA.getPotential(r)
772 
773  if p2 - p1 == 1:
774  expected += fene_AA.getPotential(r)
775 
776  for arm in arms:
777  for p1 in arm:
778  pos1 = particles.getPosition(p1)
779  for p2 in magneticParticles:
780  r = pos1 - particles.getPosition(p2)
781 
782  expected += wca_AM.getPotential(r)
783 
784 
785  for p1 in magneticParticles:
786  pos1 = particles.getPosition(p1)
787 
788  bonded = particles.getPosition(p1 - 1)
789  expected += fene_AM.getPotential(pos1 - bonded)
790 
791  for p2 in magneticParticles:
792  if p2 <= p1:
793  continue
794  r = pos1 - particles.getPosition(p2)
795  expected += wca_MM.getPotential(r)
796  expected += magnetic.getPotential(r)
797 
798  assert sp.getPotentialEnergy() == expected
799 
800  #check that caching works:
801  assert sp.getPotentialEnergy() == expected
MPCDAnalysis.ParticleCollection
Definition: ParticleCollection.py:1
MPCDAnalysis.PairPotentials.FENE
Definition: FENE.py:1
MPCDAnalysis.PairPotentials.MagneticDipoleDipoleInteraction_ConstantIdenticalDipoles
Definition: MagneticDipoleDipoleInteraction_ConstantIdenticalDipoles.py:1
MPCDAnalysis.StarPolymers
Definition: StarPolymers.py:1
MPCDAnalysis.PairPotentials.WeeksChandlerAndersen_DistanceOffset
Definition: WeeksChandlerAndersen_DistanceOffset.py:1
MPCDAnalysis.Vector3DReal
Definition: Vector3DReal.py:1
MPCDAnalysis.Configuration
Definition: Configuration.py:1