Skip to content

Instantly share code, notes, and snippets.

@orbeckst
Created January 9, 2025 00:20
Show Gist options
  • Select an option

  • Save orbeckst/2b3cbe554a38f5e79df39e82a8395d2d to your computer and use it in GitHub Desktop.

Select an option

Save orbeckst/2b3cbe554a38f5e79df39e82a8395d2d to your computer and use it in GitHub Desktop.
Create a MDAnalysis Universe for a "trajectory" in a numpy array.
import MDAnalysis as mda
import numpy as np
n_atoms = 6
n_steps = 4
# create empty Universe with one atom per residue
u = mda.Universe.empty(n_atoms, n_residues=n_atoms, atom_resindex=np.arange(n_atoms), trajectory=True)
# optional: name each atom CA
u.add_TopologyAttr("name")
u.atoms.names = "CA"
# fake trajectory as np array
coordinates = 100*np.random.random(size=(n_steps, n_atoms, 3)) - 50
# add trajectory to Universe
u.load_new(coordinates)
# You can now use u in the same way as a "normal" Universe that was created from
# files, such as u = mda.Universe(PRMTOP, NCDF).
#
# Note that you have to manually add resids and other data if you want to access it.
# See https://userguide.mdanalysis.org/stable/examples/constructing_universe.html#Creating-a-blank-Universe
@orbeckst
Copy link
Author

orbeckst commented Jan 9, 2025

If you want to add masses (such as carbon), add the appropriate topology attribute and set it with an array of mass values

u.add_TopologyAttr("mass", [mda.topology.guessers.tables.masses["C"]] * u.atoms.n_atoms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment