Skip to content

Academic Year 22-23 cleanup #12

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

Merged
merged 12 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ init*.*
tmp
__pycache__
hints.txt
.idea
30 changes: 22 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.2.2
rev: "1.5.2"
hooks:
- id: nbqa-autopep8
args: [--nbqa-skip-celltags=before-import]
- id: nbqa-black
args: ["--nbqa-skip-celltags=before-import"]
- id: nbqa-pyupgrade
args: [--py36-plus]
args: ["--py39-plus"]
- id: nbqa-isort
args: [--nbqa-skip-celltags=before-import]
args: ["--nbqa-skip-celltags=before-import"]
- id: nbqa-check-ast
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: "v4.3.0"
hooks:
- id: check-yaml
- id: check-ast
Expand All @@ -26,10 +26,24 @@ repos:
.*\.prmtop
)$
- repo: https://github.com/kynan/nbstripout.git
rev: 0.5.0
rev: "0.6.0"
hooks:
- id: nbstripout
- repo: https://github.com/psf/black
rev: 21.12b0
rev: "22.8.0"
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: "5.10.1"
hooks:
- id: isort
- repo: https://github.com/pre-commit/pygrep-hooks
rev: "v1.9.0"
hooks:
- id: python-no-eval
- id: python-use-type-annotations
- repo: https://github.com/asottile/pyupgrade
rev: "v2.38.0"
hooks:
- id: pyupgrade
args: ["--py39-plus"]
68 changes: 35 additions & 33 deletions 01_first_steps/01_water.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,26 @@
"outputs": [],
"source": [
"# a. Select the TIP3P-FB force field.\n",
"forcefield = ForceField('amber14/tip3pfb.xml')\n",
"forcefield = ForceField(\"amber14/tip3pfb.xml\")\n",
"\n",
"# b. Create an object to compute energies and forces for our topology.\n",
"system = forcefield.createSystem(topology, nonbondedCutoff=1*nanometer)\n",
"system = forcefield.createSystem(topology, nonbondedCutoff=1 * nanometer)\n",
"\n",
"# c. Definition an integrator, mandatory.\n",
"integrator = VerletIntegrator(1 * femtoseconds)\n",
"\n",
"# d. A simulation object in OpenMM combines topology, system and integrator.\n",
"simulation = Simulation(topology, system, integrator)\n",
"simulation.context.setPositions(np.array([\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 1.0],\n",
" [0.0, 1.0, 0.0],\n",
"]) * angstroms)"
"simulation.context.setPositions(\n",
" np.array(\n",
" [\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 1.0],\n",
" [0.0, 1.0, 0.0],\n",
" ]\n",
" )\n",
" * angstroms\n",
")"
]
},
{
Expand Down Expand Up @@ -258,8 +263,7 @@
" # with all attributes, useful to explore undocumented\n",
" # attributes.\n",
" # print(vars(atom))\n",
" print(\"Name and mass of atom {}: {}, {}\".format(\n",
" iatom, atom.name, atom.element.mass))"
" print(f\"Name and mass of atom {iatom}: {atom.name}, {atom.element.mass}\")"
]
},
{
Expand Down Expand Up @@ -334,8 +338,7 @@
"outputs": [],
"source": [
"# Simply extract and print information\n",
"state = simulation.context.getState(\n",
" getPositions=True, getForces=True, getEnergy=True)\n",
"state = simulation.context.getState(getPositions=True, getForces=True, getEnergy=True)\n",
"print(\"Potential energy: \", state.getPotentialEnergy())\n",
"print()\n",
"print(\"Atomic positions\")\n",
Expand Down Expand Up @@ -364,7 +367,7 @@
"d01 /= np.linalg.norm(d01)\n",
"d02 /= np.linalg.norm(d02)\n",
"cosine = np.dot(d01, d02)\n",
"print(np.arccos(cosine)*180/np.pi)"
"print(np.arccos(cosine) * 180 / np.pi)"
]
},
{
Expand All @@ -385,10 +388,10 @@
"# NGLView is a structure and trajectory visualization that integrates well with Jupyter Notebooks.\n",
"\n",
"# Write out a PDB file\n",
"with open('water.pdb', 'w') as outfile:\n",
"with open(\"water.pdb\", \"w\") as outfile:\n",
" PDBFile.writeFile(topology, pos, outfile)\n",
"# Visualize\n",
"nglview.show_mdtraj(mdtraj.load('water.pdb'))"
"nglview.show_mdtraj(mdtraj.load(\"water.pdb\"))"
]
},
{
Expand Down Expand Up @@ -433,24 +436,24 @@
"simulation.reporters = []\n",
"\n",
"# Write a frame to the PDB trajectory every step.\n",
"simulation.reporters.append(PDBReporter('traj.pdb', 1))\n",
"simulation.reporters.append(PDBReporter(\"traj.pdb\", 1))\n",
"\n",
"# Write scalar properties to a CSV file every step.\n",
"simulation.reporters.append(StateDataReporter(\n",
" \"scalars.csv\",\n",
" 1,\n",
" time=True,\n",
" potentialEnergy=True,\n",
" totalEnergy=True,\n",
" temperature=True))\n",
"simulation.reporters.append(\n",
" StateDataReporter(\n",
" \"scalars.csv\",\n",
" 1,\n",
" time=True,\n",
" potentialEnergy=True,\n",
" totalEnergy=True,\n",
" temperature=True,\n",
" )\n",
")\n",
"\n",
"# Write scalar properties to screen every 100 steps.\n",
"simulation.reporters.append(StateDataReporter(\n",
" stdout,\n",
" 100,\n",
" step=True,\n",
" totalEnergy=True,\n",
" temperature=True))\n",
"simulation.reporters.append(\n",
" StateDataReporter(stdout, 100, step=True, totalEnergy=True, temperature=True)\n",
")\n",
"\n",
"# Actually run the molecular dynamics simulation.\n",
"simulation.step(1000)"
Expand All @@ -469,7 +472,7 @@
"metadata": {},
"outputs": [],
"source": [
"nglview.show_mdtraj(mdtraj.load('traj.pdb'))"
"nglview.show_mdtraj(mdtraj.load(\"traj.pdb\"))"
]
},
{
Expand All @@ -490,7 +493,6 @@
"outputs": [],
"source": [
"# The following line is needed to obtain an interactive plot.\n",
"%matplotlib widget\n",
"df = pandas.read_csv(\"scalars.csv\")\n",
"# To show the contents of a Pandas Dataframe, just put\n",
"# the variable on the last line.\n",
Expand All @@ -505,8 +507,8 @@
"source": [
"# Plot the potential and total energy as function of time.\n",
"ax = plt.gca()\n",
"df.plot(kind='line', x='#\"Time (ps)\"', y='Potential Energy (kJ/mole)', ax=ax)\n",
"df.plot(kind='line', x='#\"Time (ps)\"', y='Total Energy (kJ/mole)', ax=ax)"
"df.plot(kind=\"line\", x='#\"Time (ps)\"', y=\"Potential Energy (kJ/mole)\", ax=ax)\n",
"df.plot(kind=\"line\", x='#\"Time (ps)\"', y=\"Total Energy (kJ/mole)\", ax=ax)"
]
},
{
Expand All @@ -515,7 +517,7 @@
"metadata": {},
"outputs": [],
"source": [
"df.plot(kind='line', x='#\"Time (ps)\"', y='Temperature (K)')"
"df.plot(kind=\"line\", x='#\"Time (ps)\"', y=\"Temperature (K)\")"
]
},
{
Expand Down
41 changes: 20 additions & 21 deletions 01_first_steps/02_lennard_jones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,15 @@
"outputs": [],
"source": [
"# Define the ensemble to be simulated in.\n",
"integrator = LangevinIntegrator(temperature, 1/picosecond, 2*femtoseconds)\n",
"integrator = LangevinIntegrator(temperature, 1 / picosecond, 2 * femtoseconds)\n",
"system.addForce(MonteCarloBarostat(pressure, temperature))\n",
"\n",
"# Define a simulation object.\n",
"simulation = Simulation(topology, system, integrator)\n",
"\n",
"# Initialization steps before MD.\n",
"# - Asign random positions\n",
"simulation.context.setPositions(np.random.uniform(\n",
" 0, box_size/angstrom, (natom, 3))*angstrom)\n",
"simulation.context.setPositions(np.random.uniform(0, box_size / angstrom, (natom, 3)) * angstrom)\n",
"# - Minimize the energy\n",
"simulation.minimizeEnergy()\n",
"# - Initialize velocities with random values at 300K.\n",
Expand All @@ -176,29 +175,29 @@
"\n",
"# Write the initial geometry as a PDB file.\n",
"positions = simulation.context.getState(getPositions=True).getPositions()\n",
"with open('ljinit.pdb', 'w') as f:\n",
"with open(\"ljinit.pdb\", \"w\") as f:\n",
" PDBFile.writeFile(simulation.topology, positions, f)\n",
"\n",
"# Write a frame to the DCD trajectory every 100 steps.\n",
"simulation.reporters.append(DCDReporter('ljtraj.dcd', 100))\n",
"simulation.reporters.append(DCDReporter(\"ljtraj.dcd\", 100))\n",
"\n",
"# Write scalar properties to a CSV file every 10 steps.\n",
"simulation.reporters.append(StateDataReporter(\n",
" \"ljscalars.csv\",\n",
" 10,\n",
" time=True,\n",
" potentialEnergy=True,\n",
" totalEnergy=True,\n",
" temperature=True,\n",
" volume=True))\n",
"simulation.reporters.append(\n",
" StateDataReporter(\n",
" \"ljscalars.csv\",\n",
" 10,\n",
" time=True,\n",
" potentialEnergy=True,\n",
" totalEnergy=True,\n",
" temperature=True,\n",
" volume=True,\n",
" )\n",
")\n",
"\n",
"# Write scalar properties to screen every 1000 steps.\n",
"simulation.reporters.append(StateDataReporter(\n",
" stdout,\n",
" 1000,\n",
" step=True,\n",
" temperature=True,\n",
" volume=True))\n",
"simulation.reporters.append(\n",
" StateDataReporter(stdout, 1000, step=True, temperature=True, volume=True)\n",
")\n",
"\n",
"# Actually run the molecular dynamics simulation.\n",
"simulation.step(30000)\n",
Expand Down Expand Up @@ -249,8 +248,8 @@
"outputs": [],
"source": [
"df = pandas.read_csv(\"ljscalars.csv\")\n",
"df.plot(kind='line', x='#\"Time (ps)\"', y='Temperature (K)')\n",
"df.plot(kind='line', x='#\"Time (ps)\"', y='Box Volume (nm^3)')"
"df.plot(kind=\"line\", x='#\"Time (ps)\"', y=\"Temperature (K)\")\n",
"df.plot(kind=\"line\", x='#\"Time (ps)\"', y=\"Box Volume (nm^3)\")"
]
},
{
Expand Down
Loading