[noob question] how to properly use iterators ?

15 views
Skip to first unread message

yann claveau

unread,
Jun 9, 2022, 7:10:38 AMJun 9
to MDnalysis discussion
Hello,
here is my basic python question :

I would like to know how to properly use ts iterator:

for ts in u.trajectory

For instance, if I want to calculate the displacement from one z-position at time t to the z-position at time t+dt. I can do this, but I guess it is not as it should be coded:

d=np.empty([u.trajectory.n_frames, u.trajectory.n_atoms])
t=0

for ts in u.trajectory:
    d[t] = u.ts.positions[:,2]
    t += 1
   
d[1:]-d[:-1]

is there a way to do a sort of u.ts[t-1].positions ?
Thanks !

Hugo Macdermott-Opeskin

unread,
Jun 9, 2022, 10:25:18 PMJun 9
to mdnalysis-...@googlegroups.com
Hi Yann, 

You can slice the `trajectory` object itself to get a subset of the frames.

See the userguide section on this for more info.

However if you are collecting multiple timepoints to compare for an MSD (which i guess is perhaps what you are doing given your previous question?) you are better off doing what you have done and collecting the positions in a big array. This is how it is done in the MSD source code

Hopefully that answers your question?

Cheers

Hugo MacDermott-Opeskin


--
You received this message because you are subscribed to the Google Groups "MDnalysis discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-discussion/85733bfb-6e60-414d-9977-f586962310c8n%40googlegroups.com.

yann claveau

unread,
Jun 10, 2022, 4:57:15 AMJun 10
to MDnalysis discussion
Thanks a lot again. Actually it was for another thing but, yes, also for thinking about my MSD problem. It was more a naive question to progress in Python and to better understand MDA.
Reply all
Reply to author
Forward
0 new messages