Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced backwards compatibility with previous function #104

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
52 changes: 34 additions & 18 deletions OpenMiChroM/ChromDynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ def setup(self, platform="CUDA", gpu="default",
self.gpu = str(gpu)

# Define the platform priority order
default_platforms = ['CUDA', 'HIP', 'OpenCL', 'CPU']
default_platforms = {'cuda': 'CUDA', 'hip': 'HIP', 'opencl': 'OpenCL', 'cpu': ' CPU'}

# Rearrange the platform priority so that the specified platform is first
preferred_platform = platform.upper()
platform_priority = [preferred_platform] + [p for p in default_platforms if p != preferred_platform]
preferred_platform = default_platforms[platform.lower()]
platform_priority = [preferred_platform] + [p for p in default_platforms.values() if p != preferred_platform]

# Dictionary to map platform names to their specific property names
property_names = {
'CUDA': {'DeviceIndex': 'CudaDeviceIndex', 'Precision': 'CudaPrecision'},
'HIP': {'DeviceIndex': 'HipDeviceIndex', 'Precision': 'HipPrecision'},
'OPENCL': {'DeviceIndex': 'OpenCLDeviceIndex', 'Precision': 'OpenCLPrecision'},
'OpenCL': {'DeviceIndex': 'OpenCLDeviceIndex', 'Precision': 'OpenCLPrecision'},
'CPU': {}
}

Expand Down Expand Up @@ -268,7 +268,7 @@ def getPositions(self):
Returns an array of positions.
"""

return np.asarray(self.data / self.nm, dtype=np.float32)
return np.asarray(self.context.getState(getPositions=True).getPositions(asNumpy=True) / self.nm, dtype=np.float32)


def getLoops(self, looplists):
Expand Down Expand Up @@ -1289,15 +1289,15 @@ def createSimulation(self):
# Print information to console
print(simulationInfo)
print(energyInfo)
print(f'\nPotential energy per forceGroup:\n {self.printForces()}')
print(f'\nPotential energy per forceGroup:\n {self.getForces()}')

filePath = Path(self.folder) / 'initialStats.txt'
with open(filePath, 'w') as f:
for info in platformInfo:
print(info, file=f)
print(simulationInfo, file=f)
print(energyInfo, file=f)
print(f'\nPotential energy per forceGroup:\n {self.printForces()}', file=f)
print(f'\nPotential energy per forceGroup:\n {self.getForces()}', file=f)


def createReporters(self, statistics=True, traj=False, trajFormat="cndb", energyComponents=False,
Expand Down Expand Up @@ -2012,15 +2012,14 @@ def saveStructure(self, filename=None, mode="gro"):

data = self.getPositions()

if filename is None:
filename = self.name +"_step%d." % self.step + mode

filename = os.path.join(self.folder, filename)

if not hasattr(self, "type_list_letter"):
raise ValueError("Chromatin sequence not defined!")

if mode == "xyz":
if filename is None:
filename = self.name +"_step%d." % self.simulation.currentStep + mode
filename = os.path.join(self.folder, filename)

lines = []
lines.append(str(len(data)) + "\n")

Expand All @@ -2045,9 +2044,11 @@ def saveStructure(self, filename=None, mode="gro"):

for chainNum, chain in zip(range(len(self.chains)),self.chains):
pdb_string = []
filename = self.name +"_" + str(chainNum) + "_block%d." % self.step + mode

if filename is None:
filename = self.name +"_" + str(chainNum) + "_step%d." % self.simulation.currentStep + mode
filename = os.path.join(self.folder, filename)

data_chain = data[chain[0]:chain[1]+1]
types_chain = self.type_list_letter[chain[0]:chain[1]+1]

Expand Down Expand Up @@ -2078,7 +2079,9 @@ def saveStructure(self, filename=None, mode="gro"):
for chainNum, chain in zip(range(len(self.chains)),self.chains):

gro_string = []
filename = self.name +"_" + str(chainNum) + "_block%d." % self.step + mode

if filename is None:
filename = self.name +"_" + str(chainNum) + "_step%d." % self.simulation.currentStep + mode
filename = os.path.join(self.folder, filename)

data_chain = data[chain[0]:chain[1]+1]
Expand Down Expand Up @@ -2119,10 +2122,12 @@ def chunks(l, n):
return ([l[i:i+n] for i in range(0, len(l), n)])

for chainNum, chain in zip(range(len(self.chains)),self.chains):
filename = self.name +"_" + str(chainNum) + "_block%d." % self.step + mode
if filename is None:
filename = self.name +"_" + str(chainNum) + "_step%d." % self.simulation.currentStep + mode
filename = os.path.join(self.folder, filename)

ndbf = []

filename = os.path.join(self.folder, filename)
data_chain = data[chain[0]:chain[1]+1]

ndbf.append(header_string.format('HEADER','NDB File genereted by Open-MiChroM'," ", " "))
Expand Down Expand Up @@ -2255,9 +2260,9 @@ def fibonacciSphere(samples=1, randomize=True):
return positions


def printForces(self):
def getForces(self):
R"""
Prints the energy values for each force applied in the system.
Gets the energy values for each force applied in the system.
"""
forceNames = []
forceValues = []
Expand All @@ -2270,6 +2275,17 @@ def printForces(self):
df = pd.DataFrame(forceValues,forceNames)
df.columns = ['Values']
return(df)


def printForces(self):
R"""
Prints the energy values for each force applied in the system.
"""

df = self.getForces()
print(f'\nPotential energy per forceGroup:\n {df}')

return df


def printHeader(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/output/initialStats.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Platform Name: OpenCL
Platform Speed: 50.0
Platform Property Names: ('DeviceIndex', 'DeviceName', 'OpenCLPlatformIndex', 'OpenCLPlatformName', 'Precision', 'UseCpuPme', 'DisablePmeStream')
DeviceIndex Value: 0
DeviceName Value: Apple M2 Max
OpenCLPlatformIndex Value: 0
OpenCLPlatformName Value: Apple
Precision Value: single
UseCpuPme Value: false
DisablePmeStream Value: false

Simulation name: test
Number of beads: 2712, Number of chains: 1
Potential energy: 67.54760, Kinetic Energy: 1.53377 at temperature: 1.0

Potential energy per forceGroup:
Values
FENEBond 55318.665039
AngleForce 0.989609
RepulsiveSoftCore 0.000000
TypetoType -222.474209
IdealChromosome -1.086154
FlatBottomHarmonic 128093.007812
Potential Energy (total) 183189.104658
3 changes: 3 additions & 0 deletions tests/output/statistics.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Step,RG,Etotal,Epot,Ekin,Temperature

20000,59.178,28.808,26.957,1.851,1.234
Loading