forked from bachiraoun/fullrmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.py
3571 lines (3315 loc) · 189 KB
/
Engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
"""
Engine is fullrmc's main module. It contains 'Engine' the main class
of fullrmc which is the stochastic artist. The engine class
takes only Protein Data Bank formatted files
`'.pdb' <http://deposit.rcsb.org/adit/docs/pdb_atom_format.html>`_ as
atomic/molecular input structure. It handles and fits simultaneously many
experimental data while controlling the evolution of the system using
user-defined molecular and atomistic constraints such as bond-length,
bond-angles, dihedral angles, inter-molecular-distances, etc.
"""
# standard libraries imports
from __future__ import print_function
import os, sys, time, uuid, tempfile, multiprocessing
import copy, inspect, collections, re
# external libraries imports
import numpy as np
try:
import cPickle as pickle
except:
import pickle
from pdbparser.pdbparser import pdbparser
from pdbparser.Utilities.BoundaryConditions import InfiniteBoundaries, PeriodicBoundaries
from pyrep import Repository
# fullrmc library imports
from .__pkginfo__ import __version__
from .Globals import INT_TYPE, FLOAT_TYPE, LOGGER, WATER_NUMBER_DENSITY
from .Globals import str, long, unicode, bytes, basestring, range, xrange, maxint
from .Core.boundary_conditions_collection import transform_coordinates
from .Core.Collection import Broadcaster, is_number, is_integer, get_elapsed_time, generate_random_float
from .Core.Collection import _AtomsCollector, _Container, get_caller_frames
from .Core.Constraint import Constraint, SingularConstraint, RigidConstraint, ExperimentalConstraint
from .Core.Group import Group, EmptyGroup
from .Core.MoveGenerator import SwapGenerator, RemoveGenerator
from .Core.GroupSelector import GroupSelector
from .Selectors.RandomSelectors import RandomSelector
class InterceptHook(object):
"""Engine runtime intercept hook. This can be used by a thread or another
process to intercept a running stochastic engine to do different actions
such as stop, save, export a pdb file or reset the standard error.
Calling InterceptHook methods on a non-running engine won't do any action.
:Parameters:
#. path (string): Engine repository path
#. password (string): Engine repository password. If None,
default value will be given hoping that the running engine
repository password is also the default one. Otherwise the hook
connection will fail.
From a different python process, run the following code to handle a hook
of a running engine.
.. code-block:: python
# import fullrmc's InterceptHook
from fullrmc import InterceptHook
# create hook
hook = InterceptHook(path='my_engine.rmc')
# safely force stopping running engine
hook.stop_engine()
# safely force saving running engine
hook.save_engine()
# safely export a pdb file of the current engine structure status
hook.export_pdb()
# safely reset standard error during engine runtime
hook.reset_standard_error()
"""
def __init__(self, path, password=None):
self.__hook = None
if path is not None:
rep = Repository(password=password)
rep = rep.load_repository(path)
self.__hook = rep.locker
#assert not self.__hook.isServer, "It's not safe for the hook to be the engine repository server. Make sure to load engine first"
#assert self.__hook.isClient, "Hook to engine '%s' not successfully created"%path
def stop_engine(self):
"""stop a running engine"""
self.__hook.publish('stop_engine')
def save_engine(self):
"""force saving a running engine"""
self.__hook.publish('save_engine')
def export_pdb(self):
"""export a pdb of the current state of a running engine"""
self.__hook.publish('export_pdb')
def reset_standard_error(self):
"""reset a running engine total standard error"""
self.__hook.publish('reset_standard_error')
@classmethod
def get_engine_hook(cls, engine):
"""Get engine instance intercept hook"""
locker = engine._get_repository().locker
hook = InterceptHook(None,None)
hook._InterceptHook__hook = locker
return hook
@classmethod
def clear(cls, engine):
"""release all unexecuted hooks from engine"""
locker = engine._Engine__repository.locker
for m in ['stop_engine','save_engine','export_pdb','reset_standard_error']:
_ = locker.pop_message(m)
@classmethod
def _intercept(cls, engine, step, restartPdb, _frame, _usedConstraints, _constraints, _lastSavedTotalStandardError):
"""meant for internal use only"""
locker = engine._Engine__repository.locker
usedFrame = engine.usedFrame
stop = False
# reset standard error
if locker.pop_message('reset_standard_error') is not None:
LOGGER.usage("@%s request to reset engine's standard error is received at step %i"%( usedFrame, step) )
_ = engine.initialize_used_constraints(force=True)
_ = [c._runtime_initialize() for c in _usedConstraints]
engine._Engine__totalStandardError = engine.compute_total_standard_error(_constraints, current="standardError")
# save engine
if locker.pop_message('save_engine') is not None:
LOGGER.usage("@%s request to save engine is received at step %i"%( usedFrame, step) )
_lastSavedTotalStandardError = \
engine._Engine__on_runtime_step_save_engine(_saveFrequency = step+1,
step = step,
_frame = _frame,
_usedConstraints = _usedConstraints,
_lastSavedTotalStandardError = _lastSavedTotalStandardError)
# export pdb
if locker.pop_message('export_pdb') is not None:
LOGGER.usage("@%s request to export pdb is received at step %i"%( usedFrame, step) )
if restartPdb is False:
engine.export_pdb( 'restart.pdb' )
else:
engine.export_pdb( restartPdb )
# stop engine
if locker.get_message('stop_engine') is not None:
stop = True
LOGGER.usage("@%s request to stop engine is received at step %i"%( usedFrame, step) )
# return stop flag and _lastSavedTotalStandardError
return stop, _lastSavedTotalStandardError
class Engine(object):
"""
fulrmc's engine, is used to launch a stochastic modelling which is
different than traditional Reverse Monte Carlo (RMC).
It has the capability to use and fit simultaneously multiple sets of
experimental data. One can also define constraints such as distances,
bonds length, angles and many others.
:Parameters:
#. path (None, string): Engine repository (directory) path to save the
engine. If None is given path will be set when saving the engine
using Engine.save method. If a non-empty directory is found at the
given path an error will be raised unless freshStart flag attribute
is set to True.
#. logFile (None, string): Logging file basename. A logging file full
name will be the given logFile appended '.log' extension
automatically. If None is given, logFile is left unchanged.
#. freshStart (boolean): Whether to remove any existing fullrmc engine
at the given path if found. If set to False, an error will be raise
if a fullrmc engine or a non-empty directory is found at the given
path.
#. timeout (number): The maximum delay or time allowed to successfully
set the lock upon reading or writing the engine repository
#. password (None, string): Engine repository password. If None,
default value will be given.
.. code-block:: python
# import engine
from fullrmc.Engine import Engine
# create engine
ENGINE = Engine(path='my_engine.rmc')
# set pdb file
ENGINE.set_pdb(pdbFileName)
# Add constraints ...
# Re-define groups if needed ...
# Re-define groups selector if needed ...
# Re-define moves generators if needed ...
# save engine
ENGINE.save()
# run engine for 10000 steps and save only at the end
ENGINE.run(numberOfSteps=10000, saveFrequency=10000)
"""
def __init__(self, path=None, logFile=None, freshStart=False, timeout=10, password=None):
# check password
if password is not None:
assert isinstance(password, basestring), "password must be None or a string"
self.__password = password
# set repository and frame data
ENGINE_DATA = ('_Engine__frames', '_Engine__usedFrame', )
## MUST ADD ORIGINAL DATA TO STORE ALL ORIGINAL PDB ATTRIBUTES
## THIS IS NEEDED TO SET GROUPS AND ETC ESPECIALLY AFTER REMOVING
## ATOMS FROM SYSTEM.
self.__frameOriginalData = {}
FRAME_DATA = ('_Engine__pdb', '_Engine__tolerance', ### MIGHT NEED TO MOVE _Engine__pdb TO ENGINE_DATA
'_Engine__boundaryConditions', '_Engine__isPBC', '_Engine__isIBC',
'_Engine__basisVectors', '_Engine__reciprocalBasisVectors',
'_Engine__numberDensity', '_Engine__volume',
'_Engine__realCoordinates','_Engine__boxCoordinates',
'_Engine__groups', '_Engine__groupSelector', '_Engine__state',
'_Engine__generated', '_Engine__tried', '_Engine__accepted',
'_Engine__removed', '_Engine__tolerated', '_Engine__totalStandardError',
'_Engine__lastSelectedGroupIndex', '_Engine__numberOfMolecules',
'_Engine__moleculesIndex', '_Engine__moleculesName',
'_Engine__allElements', '_Engine__elements',
'_Engine__elementsIndex', '_Engine__numberOfAtomsPerElement',
'_Engine__allNames', '_Engine__names',
'_Engine__namesIndex', '_Engine__numberOfAtomsPerName',
'_atomsCollector',)
#'_atomsCollector', '_Container')
MULTIFRAME_DATA = ('_Engine__constraints',)#'_Engine__broadcaster') # multiframe data will be save appart with ENGINE_DATA and pulled for traditional frames
RUNTIME_DATA = ('_Engine__realCoordinates','_Engine__boxCoordinates',
'_Engine__state', '_Engine__generated', '_Engine__tried',
'_Engine__accepted','_Engine__tolerated', '_Engine__removed',
'_Engine__totalStandardError', '_Engine__lastSelectedGroupIndex',
'_atomsCollector', # RUNTIME_DATA must have all atomsCollector data keys and affected attributes upon amputating atoms
'_Engine__moleculesIndex', '_Engine__moleculesName',
'_Engine__elementsIndex', '_Engine__allElements',
'_Engine__namesIndex', '_Engine__allNames',
'_Engine__numberOfAtomsPerName',
'_Engine__numberOfAtomsPerElement',
'_Engine__names','_Engine__elements',
'_Engine__numberOfMolecules','_Engine__numberDensity',)
# might need to add groups to FRAME_DATA
object.__setattr__(self, 'ENGINE_DATA', tuple( ENGINE_DATA) )
object.__setattr__(self, 'FRAME_DATA', tuple( FRAME_DATA) )
object.__setattr__(self, 'MULTIFRAME_DATA', tuple( MULTIFRAME_DATA) )
object.__setattr__(self, 'RUNTIME_DATA', tuple( RUNTIME_DATA) )
# initialize engine' info
self.__frames = {'0':None}
self.__usedFrame = '0'
self.__id = str(uuid.uuid1())
self.__version = __version__
# set timeout
self.set_timeout(timeout)
# check whether an engine exists at this path
if self.is_engine(path):
if freshStart:
rep = Repository(timeout=self.__timeout, password=self.__password)
rep.DEBUG_PRINT_FAILED_TRIALS = False
rep.remove_repository(path, removeEmptyDirs=True)
rep.close()
else:
m = "An Engine is found at '%s'. "%path
m += "If you wish to override it set freshStart argument to True. "
m += "If you wish to load it set path to None and use Engine.load method instead. "
raise Exception( LOGGER.error(m) )
# initialize path and repository
if path is not None:
result, message = self.__check_path_to_create_repository(path)
assert result, LOGGER.error(message)
self.__path = path
self.__repository = None
# initialize atoms collector
dataKeys = ('realCoordinates', 'boxCoordinates',
'moleculesIndex', 'moleculesName',
'elementsIndex', 'allElements',
'namesIndex', 'allNames')
self._atomsCollector = _AtomsCollector(self, dataKeys=dataKeys)
## initialize objects container
#self._container = _Container()
# initialize engine attributes
self.__broadcaster = Broadcaster()
self.__constraints = []
self.__state = time.time()
self.__groups = []
self.__groupSelector = None
self.__tolerance = 0.
# set mustSave flag, it indicates whether saving whole engine is needed before running
self.__saveGroupsFlag = True
# set pdb
self.__pdb = None
self.set_pdb(pdb=None)
# create runtime variables and arguments
self._runtime_ncores = INT_TYPE(1)
# set LOGGER file path
if logFile is not None:
self.set_log_file(logFile)
def codify(self, export='codified_engine.py', name='ENGINE',
engineSaveName='engine.rmc', frames=None, addDependencies=True):
"""Codify engine full state to a code that once executed it will
regenerate and and save the engine on disk. This is a better alternative
to transfering engine from one system to another. The generated code
is python 2 and 3 compatible and operating system safe allowing
to get an executable code to regenerate the engine.
:Parameters:
#. export (None, str): file to write generated codified engine
code. If string is given, it's the file path
#. name (str): engine variable name in the generate code
#. engineSaveName (None,str): directory path to save engine upon
executing the codified engine code
#. frames (None, str, list): the list of frames to codify. At least
one traditional frame must be given. If None, all frames will
be codified.
#. addDependencies (bool): whether to add imports dependencies
to the generated code header. Dependencies will always be
added to the code header in the exported file.
:Returns:
#. dependencies (list): list of dependencies import strings
#. code (str): the codified engine code
"""
# check parameters
if export is not None:
assert isinstance(export, basestring), LOGGER.error("export must be None or a file path")
if engineSaveName is not None:
assert isinstance(engineSaveName, basestring), LOGGER.error("engineSaveName must be a string")
assert isinstance(name, basestring), LOGGER.error("name must be a string")
assert re.match('[a-zA-Z_][a-zA-Z0-9_]*$', name) is not None, LOGGER.error("given name '%s' can't be used as a variable name"%name)
if frames is None:
frames = list(self.__frames)
elif isinstance(frames, basestring):
frames = [frames]
assert all([frm in self.__frames for frm in frames]), LOGGER.error("codifying frames must all be defined")
multiLUT = {}
framesLUT = collections.OrderedDict()
for f in frames:
if isinstance(f, basestring):
frmName = f
frmAlias = f
else:
assert isinstance(f, (list,tuple)), LOGGER.error("codifying frames list item must be a string or a list")
assert len(f) == 2, LOGGER.error("codifying frames list items must be lists of length 2")
frmName = f[0]
frmAlias = f[1]
assert frmAlias not in framesLUT, LOGGER.error("codifying frame '%s' multiple time is not allowed"%(f,))
isNormalFrame, isMultiframe, isSubframe = self.get_frame_category(frmName)
if isNormalFrame:
framesLUT[frmAlias] = frmName
else:
multiLUT[frmAlias] = frmName
assert len(framesLUT), LOGGER.error("codifying at least one traditional frame is mandatory")
for mf in multiLUT:
framesLUT[mf] = multiLUT[mf]
# start dependencies
LOGGER.info('Codifying engine ...')
dependencies = collections.OrderedDict()
dependencies["# This code is auto-generated by fullrmc {ver}".format(ver=self.__version)] = True
dependencies["# For questions and concerns use fullrmc's forum http://bachiraoun.github.io/fullrmc/QAForum.html"] = True
dependencies["\n# import dependencies"] = True
dependencies['from __future__ import print_function'] = True
dependencies['import os, sys'] = True
dependencies['from fullrmc import get_version'] = True
dependencies['from fullrmc import Engine'] = True
dependencies['from fullrmc.Globals import LOGGER'] = True
dependencies['from fullrmc.Globals import FLOAT_TYPE']= True
dependencies['from fullrmc.Globals import INT_TYPE'] = True
dependencies['from fullrmc.Core.Constraint import ExperimentalConstraint'] = True
# initialize code
code = ["\n# Check fullrmc's version",
"if get_version()!='{ver}':".format(ver=self.__version),
" LOGGER.warn('Engine is codified using fullrmc {vers} while this used fullrmc version is %s'%(get_version(),))".format(vers=self.__version),
"\n",
"LOGGER.info('Building codified engine')",
"\n# Initialize Engine",
"{name} = Engine(path='{engineSaveName}')".format(name=name, engineSaveName=engineSaveName)]
# create frames
rename0 = '0' not in framesLUT
priors = {}
for idx, frmAlias in enumerate(framesLUT):
frmName = framesLUT[frmAlias]
frmData = self.__frames[frmName]
ismulti = frmData is not None
if idx == 0:
assert not ismulti, LOGGER.error("First codified frame must not be multi-frame. PLEASE REPORT")
self.set_used_frame(frmName)
if rename0:
rename0 = False
code.append("{name}.rename_frame(frame='0', newName='{newFrame}')".format(name=name, newFrame=frmData[0]))
# create first frame
dep, kod = self._codify_frame__(name=name, pdb=self.__pdb, addConstraints=self.__constraints, updateConstraints=None, addDependencies=False, priors=priors)
for d in dep:
_ = dependencies.setdefault(d,True)
code.append(kod)
# check to save engine
if engineSaveName is not None:
code.append("")
code.append("# Save engine")
code.append("{name}.save()".format(name=name))
else:
# create frame
code.append("\n\n# Adding frame '{frame}'".format(frame=frmAlias))
if ismulti:
code.append("LOGGER.info(\"Adding multiframe '{frame}'\")".format(frame=frmAlias))
else:
code.append("LOGGER.info(\"Adding traditional frame '{frame}'\")".format(frame=frmAlias))
if ismulti:
frmData['name'] = frmAlias
frm2set = [("os.path.join('{a}','{n}')".format(a=frmAlias,n=n), os.path.join(frmAlias,n), os.path.join(frmName,n)) for n in frmData['frames_name']]
frmData = [frmData]
else:
frmData = ["%s"%frmAlias]
frm2set = [("'%s'"%frmAlias,frmAlias,frmName)]
code.append("{name}.add_frames({frmData})".format(name=name, frmData=frmData))
# create frame or multiframe code
# af: alias path or path to execute on any operating system os.path.join(...)
# ap: alias path
# of: original file path
for idx, (af, ap, of) in enumerate(frm2set):
self.set_used_frame(of)
code.append("\n\n# Creating frame '{ap}'".format(ap=ap))
code.append("LOGGER.info(\"Creating frame '{ap}'\")".format(ap=ap))
code.append("{name}.set_used_frame({frmName})".format(name=name, frmName=af))
if ismulti:
pdb = self.__pdb
if idx == 0:
addConstraints = self.__constraints
updateConstraints = None
code.append("# Remove all constraints")
code.append("constraints = {name}.constraints".format(name=name))
else:
addConstraints = [_c for _c in self.__constraints if not isinstance(_c, ExperimentalConstraint)]
updateConstraints = [_c for _c in self.__constraints if isinstance(_c, ExperimentalConstraint)]
code.append("constraints = [_c for _c in {name}.constraints if not isinstance(_c, ExperimentalConstraint)]".format(name=name))
code.append("# Remove non-experimental constraints")
code.append("{name}.remove_constraints(constraints=constraints)".format(name=name))
else:
pdb = None
addConstraints = None
updateConstraints = self.__constraints
# codify constraints
dep, kod = self._codify_frame__(name=name, pdb=pdb, addConstraints=addConstraints, updateConstraints=updateConstraints, addDependencies=False, priors=priors)
for d in dep:
_ = dependencies.setdefault(d,True)
code.append(kod)
# check to save engine
if engineSaveName is not None:
code.append("")
code.append("# Save engine")
code.append("{name}.save()".format(name=name))
# add dependencies
dependencies = list(dependencies)
if addDependencies:
code = dependencies + [''] + code
code = '\n'.join(code)
# log
LOGGER.info('Engine Codified successfully.')
# export
if export is not None:
with open(export, 'w') as fd:
fd.write(code)
LOGGER.info("Codified engine successfully exported to '%s'"%(export,))
# return
return dependencies, code
def _codify_frame__(self, name='ENGINE', pdb=None, addConstraints=None, updateConstraints=None, addDependencies=False, priors=None):
def add_get_kod_update(code, dependencies, kod, priors, varName, pbn, addToCode=True):
for d in dep:
_ = dependencies.setdefault(d,True)
if kod not in priors:
code.append(kod)
prioredVarName = pbn+"_"+varName
priors[kod] = prioredVarName
assert prioredVarName not in priors, LOGGER.error("codify duplicate prior variable name '%s' found. PLEASE REPORT"%prioredVarName)
priors[prioredVarName] = kod
if addToCode:
code.append("{prioredVarName} = {varName}".format(prioredVarName=prioredVarName, varName=varName))
else:
prioredVarName = priors[kod]
return code, prioredVarName
if priors is None:
priors = {}
assert isinstance(priors, dict), LOGGER.error('priors must be None or a dict')
pbn = '_'+self.__usedFrame.replace('-','_').replace(os.sep, '_') # priors basename
assert re.match('[a-zA-Z_][a-zA-Z0-9_]*$', pbn) is not None, LOGGER.error("code prior basename '%s' can't be used in a variable name. PLEASE REPORT"%(pbn))
dependencies = collections.OrderedDict()
# create engine
code = []
# add pdb
if pdb is not None:
code.append("\n# Create pdb")
varName = "pdb"
dep, kod = pdb._codify__(name=varName, addDependencies=False)
code, varName = add_get_kod_update(code=code, dependencies=dependencies, kod=kod, priors=priors, varName=varName, pbn=pbn)
# set pdb
code.append("\n# Set pdb")
code.append( "{name}.set_pdb({varName})".format(name=name, varName=varName) )
# set real coordinates coordinates
code.append("\n# Set real coordinates")
code.append("x = {x}".format(x = list(self.__realCoordinates[:,0])))
code.append("y = {y}".format(y = list(self.__realCoordinates[:,1])))
code.append("z = {z}".format(z = list(self.__realCoordinates[:,2])))
code.append("{name}._Engine__realCoordinates = np.transpose([x,y,z]).astype(FLOAT_TYPE)".format(name=name))
# set engine coordinates and boundary conditions. This will take care of box coordinates
code.append("\n# Set boundary conditions")
bc = None
if self.isPBC:
bc = [list(i) for i in list(self.__boundaryConditions.get_vectors())]
code.append( "{name}.set_boundary_conditions({bc})".format(name=name, bc=bc) )
# set selector
dep, kod = self.__groupSelector._codify__(name='sel', addDependencies=False)
code.append("\n# Set group selector")
code.append(kod)
code.append( "{name}.set_group_selector(sel)".format(name=name) )
for d in dep:
_ = dependencies.setdefault(d,True)
# create groups and move generators LUT
mgLUT = {}
code.append("groups = []")
for idx,gr in enumerate(self.__groups):
varName = 'gr_%i'%idx
dep, kod = gr._codify__(name=varName, addDependencies=False, codifyGenerator=False)
code, varName = add_get_kod_update(code=code, dependencies=dependencies, kod=kod, priors=priors, varName=varName, pbn=pbn)
code.append("groups.append({varName})".format(varName=varName))
varName = "mg"
dep, kod = gr.moveGenerator._codify__(name=varName, addDependencies=False)
mgLUT.setdefault(kod, []).append(idx)
for d in dep:
_ = dependencies.setdefault(d,True)
# set groups
code.append("\n# Create groups")
code.append("{name}.set_groups(groups, name=None, _check=False)".format(name=name))
# set move generators
code.append("\n# Set groups move generator")
for kod in mgLUT:
code.append("indexes = {indexes}".format(indexes=mgLUT[kod]))
code.append("for idx in indexes:")
code.append(' '+ kod.replace("\n", "\n "))
code.append(" {name}.groups[idx].set_move_generator(mg)".format(name=name))
# add constraints
if addConstraints is not None:
code.append("\n# Create and add constraints")
for c in addConstraints:
dep, kod = c._codify__(engine=name, addDependencies=False)
code.append(kod)
for d in dep:
_ = dependencies.setdefault(d,True)
# update constraints
if updateConstraints is not None:
for c in updateConstraints:
code.append("\n# Update '{cn}' constraint".format(cn=c.constraintName))
code.append("constraint = ([None] + [c for c in {name}.constraints if c.constraintName=='{cn}'])[-1]".format(name=name, cn=c.constraintName))
code.append("assert constraint is not None, LOGGER.error(\"constraint '%s' not found upon building codified engine \"%('{cn}',))".format(cn=c.constraintName))
dep, kod = c._codify_update__(name='constraint', addDependencies=False)
code.append(kod)
for d in dep:
_ = dependencies.setdefault(d,True)
# collect all collected data
if len(self._atomsCollector):
code.append("\n# Collect removed atoms")
for idx in self._atomsCollector.indexes:
code("{name}._on_collector_collect_atom(realIndex={idx})".format(name=name, idx=idx))
# add dependencies
dependencies = list(dependencies)
if addDependencies:
code = dependencies + [''] + code
# return
return dependencies, '\n'.join(code)
def __repr__(self):
repr = "fullrmc %s (Version %s)"%(self.__class__.__name__, self.__version)
if self.__repository is None:
return repr
repoStats = self.__repository.get_stats()[:2]
ndirs = repoStats[0]
nfiles = repoStats[1]
nframes = sum([1 for f in self.__frames if self.__frames[f] is None])
nmframes = sum([1 for f in self.__frames if self.__frames[f] is not None])
repr = "%s @%s [%i directories] [%i files] (%i frames) (%i multiframes)"%(repr,self.__repository.path, ndirs, nfiles, nframes, nmframes)
return repr
def __str__(self):
return self.__repr__()
def __setattr__(self, name, value):
if name in ('ENGINE_DATA', 'FRAME_DATA', 'RUNTIME_DATA', 'MULTIFRAME_DATA'):
raise Exception(LOGGER.error("Setting '%s' is not allowed."%name))
else:
object.__setattr__(self, name, value)
def __getstate__(self):
state = {}
for k in self.__dict__:
if k in self.ENGINE_DATA:
continue
elif k in self.FRAME_DATA:
continue
elif k in self.MULTIFRAME_DATA:
continue
state[k] = self.__dict__[k]
# no need to pickle repository. This might cause locker problems. It
# will be instanciated upon loading
state['_Engine.__repository'] = None
# return state
return state
#def __setstate__(self, d):
# self.__dict__ = d
def __check_get_frame_name(self, name):
assert isinstance(name, basestring), "Frame name must be a string"
name = str(name)
assert name.replace('_','').replace('-','').replace(' ','').isalnum(), LOGGER.error("Frame name must be strictly alphanumeric with the exception of '-' and '_'")
return name
def __check_frames(self, frames, raiseExisting=True):
if not isinstance(frames, (list,set,tuple)):
frames = [frames]
else:
frames = list(frames)
assert len(frames), LOGGER.error("frames must be a non-empty list.")
for idx, frm in enumerate(frames):
if isinstance(frm, basestring):
frameName = self.__check_get_frame_name(frm)
elif isinstance(frm, dict):
#assert self.__repository is not None, LOGGER.error("Creating multiframe is not allowed before initializing repository. Save engine then proceed.")
assert 'name' in frm, "multiframe dictionary must contain 'name'"
frameName = self.__check_get_frame_name(frm['name'])
assert 'frames_name' in frm, "multiframe dictionary must contain 'frames_name'"
multiFramesName = frm['frames_name']
if not isinstance(multiFramesName, (list,set,tuple)):
assert isinstance(multiFramesName, int), LOGGER.error("multiframe dictionary 'frames_name' value must be a list of names of an integer indicating number of frames")
assert multiFramesName>=1, LOGGER.error("multiframe dictionary integer 'frames_name' value must be >=1")
multiFramesName = [str(i) for i in range(multiFramesName)]
assert len(multiFramesName)>=1, LOGGER.error("multiframe dictionary 'frames_name' list number of items must be >=1")
multiFramesName = [self.__check_get_frame_name(str(i)) for i in multiFramesName]
assert len(multiFramesName) == len(set(multiFramesName)), LOGGER.error("Multiframe dictionary 'frames_name' list redundancy is not allowed")
# get frame dictionary copy for usage safety purposes
frm = copy.deepcopy(frm)
frm['frames_name'] = tuple(multiFramesName)
frm['name'] = str(frameName)
else:
assert isinstance(frm, int), LOGGER.error('Each frame must be either interger a string or a dict')
frameName = self.__check_get_frame_name(str(frm))
if raiseExisting:
assert frameName not in self.__frames, LOGGER.error("frame name '%s' exists already."%frameName)
frames[idx] = frm
# check for redundancy
assert len(frames) == len(set([i if isinstance(i, basestring) else i['name'] for i in frames])), LOGGER.error("Redundancy is not allowed in frame names.")
# all is good
return frames
def __check_path_to_create_repository(self, path):
# check for string
if not isinstance(path, basestring):
return False, "Repository path must be a string. '%s' is given"%path
# test if directory is empty
if os.path.isdir(path):
if len(os.listdir(path)):
return False, "Repository path directory at '%s' is not empty"%path
# all is good unless directory is not writable.
return True, ""
def __set_runtime_ncores(self, ncores):
if ncores is None:
ncores = INT_TYPE(1)
else:
assert is_integer(ncores), LOGGER.error("ncores must be an integer")
ncores = INT_TYPE(ncores)
assert ncores>0, LOGGER.error("ncores must be > 0")
if ncores > multiprocessing.cpu_count():
LOGGER.warn("ncores '%s' is reset to %s which is the number of available cores on your machine"%(ncores, multiprocessing.cpu_count()))
ncores = INT_TYPE(multiprocessing.cpu_count())
self._runtime_ncores = ncores
def _reinit_engine(self):
""" Initialize all engine arguments and flags. """
# engine state
self.__state = time.time()
# engine last moved group index
self.__lastSelectedGroupIndex = None
# engine generated steps number
self.__generated = 0
# engine tried steps number
self.__tried = 0
# engine accepted steps number
self.__accepted = 0
# engine tolerated steps number
self.__tolerated = 0
# engine removed atoms
self.__removed = [0.,0.,0.]
# current model totalStandardError from constraints
self.__totalStandardError = None
# set groups as atoms
self.set_groups(None)
# set group selector as random
self.set_group_selector(None)
# update constraints in repository
if self.__repository is not None:
self.__repository.update_file(value=self, relativePath='engine')
self.__repository.update_file(value=self.__state, relativePath=os.path.join(self.__usedFrame, '_Engine__state'))
self.__repository.update_file(value=self.__lastSelectedGroupIndex, relativePath=os.path.join(self.__usedFrame, '_Engine__lastSelectedGroupIndex'))
self.__repository.update_file(value=self.__generated, relativePath=os.path.join(self.__usedFrame, '_Engine__generated'))
self.__repository.update_file(value=self.__removed, relativePath=os.path.join(self.__usedFrame, '_Engine__removed'))
self.__repository.update_file(value=self.__tried, relativePath=os.path.join(self.__usedFrame, '_Engine__tried'))
self.__repository.update_file(value=self.__accepted, relativePath=os.path.join(self.__usedFrame, '_Engine__accepted'))
self.__repository.update_file(value=self.__tolerated, relativePath=os.path.join(self.__usedFrame, '_Engine__tolerated'))
self.__repository.update_file(value=self.__totalStandardError, relativePath=os.path.join(self.__usedFrame, '_Engine__totalStandardError'))
def _dump_to_repository(self, value, relativePath, repository=None):
if repository is None:
repository = self.__repository
if repository is None:
return
isRepoFile, fileOnDisk, infoOnDisk, classOnDisk = repository.is_repository_file(relativePath)
if isRepoFile and fileOnDisk and infoOnDisk and classOnDisk:
repository.update(value=value, relativePath=relativePath)
else:
repository.dump(value=value, relativePath=relativePath, replace=True)
def _set_path(self, path):
self.__path = path
def _set_repository(self, repo):
self.__repository = repo
def _get_repository(self):
return self.__repository
def _get_broadcaster(self):
return self.__broadcaster
def _on_collector_reset(self):
pass
def _on_collector_collect_atom(self, realIndex):
assert not self._atomsCollector.is_collected(realIndex), LOGGER.error("Trying to collect atom index %i which is already collected."%realIndex)
relativeIndex = self._atomsCollector.get_relative_index(realIndex)
# create dataDict and remove
dataDict = {}
dataDict['realCoordinates'] = self.__realCoordinates[relativeIndex,:]
dataDict['boxCoordinates'] = self.__boxCoordinates[relativeIndex, :]
dataDict['moleculesIndex'] = self.__moleculesIndex[relativeIndex]
dataDict['moleculesName'] = self.__moleculesName[relativeIndex]
dataDict['elementsIndex'] = self.__elementsIndex[relativeIndex]
dataDict['allElements'] = self.__allElements[relativeIndex]
dataDict['namesIndex'] = self.__namesIndex[relativeIndex]
dataDict['allNames'] = self.__allNames[relativeIndex]
assert self.__numberOfAtomsPerElement[dataDict['allElements']]-1>0, LOGGER.error("Collecting last atom of any element type is not allowed. It's better to restart your simulation without any '%s' rather than removing them all!"%dataDict['allElements'])
# collect atom
self._atomsCollector.collect(index=realIndex, dataDict=dataDict)
# collect all constraints BEFORE removing data from engine.
for c in self.__constraints:
c._on_collector_collect_atom(realIndex=realIndex)
# remove data from engine AFTER collecting constraints data.
self.__realCoordinates = np.delete(self.__realCoordinates, relativeIndex, axis=0)
self.__boxCoordinates = np.delete(self.__boxCoordinates, relativeIndex, axis=0)
self.__moleculesIndex = np.delete(self.__moleculesIndex,relativeIndex, axis=0)
self.__moleculesName.pop(relativeIndex)
self.__elementsIndex = np.delete(self.__elementsIndex, relativeIndex, axis=0)
self.__allElements.pop(relativeIndex)
self.__namesIndex = np.delete(self.__namesIndex, relativeIndex, axis=0)
self.__allNames.pop(relativeIndex)
# adjust other attributes
self.__numberOfAtomsPerName[dataDict['allNames']] -= 1
self.__numberOfAtomsPerElement[dataDict['allElements']] -= 1
#self.__elements = sorted(set(self.__allElements)) # no element should disappear
self.__names = sorted(set(self.__names))
self.__numberOfMolecules = len(set(self.__moleculesIndex))
# update number density in periodic boundary conditions only
if self.__isPBC:
self.__numberDensity = FLOAT_TYPE(self.numberOfAtoms) / FLOAT_TYPE(self.__volume)
def _on_collector_release_atom(self, realIndex):
# get relative index
relativeIndex = self._atomsCollector.get_relative_index(realIndex)
# get dataDict
dataDict = self._atomsCollector.release(realIndex)
# release all constraints
for c in self.__constraints:
c._on_collector_release_atom(realIndex=realIndex)
# re-insert data
self.__realCoordinates = np.insert(self.__realCoordinates, relativeIndex, dataDict["realCoordinates"], axis=0)
self.__boxCoordinates = np.insert(self.__boxCoordinates, relativeIndex, dataDict["boxCoordinates"], axis=0)
self.__moleculesIndex = np.insert(self.__moleculesIndex, relativeIndex, dataDict["moleculesIndex"],axis=0)
self.__moleculesName.insert(relativeIndex, dataDict["moleculesName"])
self.__elementsIndex = np.insert(self.__elementsIndex, relativeIndex, dataDict["elementsIndex"], axis=0)
self.__allElements.insert(relativeIndex, dataDict["allElements"])
self.__namesIndex = np.insert(self.__namesIndex, relativeIndex, dataDict["namesIndex"], axis=0)
self.__allNames.insert(relativeIndex, dataDict["allNames"])
# adjust other attributes
self.__numberOfAtomsPerName[dataDict['allNames']] += 1
self.__numberOfAtomsPerElement[dataDict['allElements']] += 1
self.__elements = list(set(self.__allElements))
self.__names = sorted(set(self.__names))
self.__numberOfMolecules = len(set(self.__moleculesIndex))
# update number density in periodic boundary conditions only
if self.__isPBC:
self.__numberDensity = FLOAT_TYPE(self.numberOfAtoms) / FLOAT_TYPE(self.__volume)
@property
def path(self):
""" Engine's repository path if set or save."""
return self.__path
@property
def version(self):
""" Stochastic engine version."""
return self.__version
@property
def info(self):
""" Stochastic engine information (version, id) tuple."""
return (self.__version, self.__id)
@property
def frames(self):
""" Stochastic engine frames list copy."""
return copy.deepcopy(self.__frames)
@property
def framesPath(self):
""" List of engine traditional frames name and multiframes path."""
framesPath = []
for n,v in self.__frames.items():
if v is None:
framesPath.append(n)
else:
for sn in v['frames_name']:
framesPath.append(os.path.join(n,sn))
return framesPath
@property
def usedFrame(self):
""" Stochatic engine frame in use."""
return copy.deepcopy(self.__usedFrame)
@property
def lastSelectedGroupIndex(self):
""" The last moved group instance index in groups list. """
return self.__lastSelectedGroupIndex
@property
def lastSelectedGroup(self):
""" The last moved group instance. """
if self.__lastSelectedGroupIndex is None:
return None
return self.__groups[self.__lastSelectedGroupIndex]
@property
def lastSelectedAtomsIndex(self):
""" The last moved atoms index. """
if self.__lastSelectedGroupIndex is None:
return None
return self.lastSelectedGroup.indexes
@property
def state(self):
""" Engine's state. """
return self.__state
@property
def generated(self):
""" Number of generated moves. """
return self.__generated
@property
def removed(self):
""" Removed atoms tuple (tried, accepted, ratio)"""
return tuple(self.__removed)
@property
def tried(self):
""" Number of tried moves. """
return self.__tried
@property
def accepted(self):
""" Number of accepted moves. """
return self.__accepted
@property
def tolerated(self):
""" Number of tolerated steps in spite of increasing
totalStandardError"""
return self.__tolerated
@property
def tolerance(self):
""" Tolerance in percent. """
return self.__tolerance*100.
@property
def groups(self):
""" Engine's defined groups list. """
return self.__groups
@property
def pdb(self):
""" Engine's pdbparser instance. """
return self.__pdb
@property
def boundaryConditions(self):
""" Engine's boundaryConditions instance. """
return self.__boundaryConditions
@property
def isPBC(self):
""" Whether boundaryConditions are periodic. """
return self.__isPBC
@property
def isIBC(self):
""" Whether boundaryConditions are infinte. """
return self.__isIBC
@property
def basisVectors(self):
""" The boundary conditions basis vectors in case of
PeriodicBoundaries, None in case of InfiniteBoundaries. """
return self.__basisVectors
@property
def reciprocalBasisVectors(self):
""" The boundary conditions reciprocal basis vectors in case of
PeriodicBoundaries, None in case of InfiniteBoundaries. """
return self.__reciprocalBasisVectors
@property
def volume(self):
""" The boundary conditions basis volume in case of PeriodicBoundaries,
None in case of InfiniteBoundaries. """
return self.__volume
@property
def realCoordinates(self):
""" The real coordinates of the current configuration. """
return self.__realCoordinates
@property
def boxCoordinates(self):
""" The box coordinates of the current configuration in case of
PeriodicBoundaries. Similar to realCoordinates in case of
InfiniteBoundaries."""
return self.__boxCoordinates
@property
def numberOfMolecules(self):
""" Number of molecules."""
return self.__numberOfMolecules
@property
def moleculesIndex(self):
""" Atoms molecule index list. """
return self.__moleculesIndex
@property
def moleculesName(self):
""" Atoms molecule name list. """
return self.__moleculesName
@property
def elementsIndex(self):
""" Atoms element index list indexing elements sorted set. """
return self.__elementsIndex
@property
def elements(self):
""" Sorted set of all existing atom elements. """
return self.__elements
@property
def allElements(self):
""" Atoms element list. """
return self.__allElements
@property