You can store the center of mass in a list or numpy array by appending to a list and then converting the list to an array at the end:
import numpy as np
time = []
cm = []
dna1 = u.select_atoms("resid 1 to 147")
for ts in u.trajectory:
time.append(u.trajectory.time)
cm.append(dna1.atoms.center_of_mass())
time = np.array(time)
cm = np.array(cm)
There are fancier ways to solve this problem. For a start, you don’t need the intermediate list but you can instead allocate the array in advance:
import numpy as np
time = np.zeros(u.trajectory.n_frames, dtype=np.float32)
cm = np.zeros((u.trajectory.n_frames, 3), dtype=np.float32)
dna1 = u.select_atoms("resid 1 to 147")
for i, ts in enumerate(u.trajectory):
time[i] = u.trajectory.time
cm[i] = dna1.atoms.center_of_mass()
# now work with time and cm array
from MDAnalysis.analysis.base import AnalysisFromFunction
def center_of_mass(ag):
return ag.center_of_mass()
com = AnalysisFromFunction(center_of_mass, u.trajectory, dna1).run(verbose=True)
print(com.results.times) # like time array before
print(com.resulys.timeseries) # like cm array before
Hope this helps.