=============== Assignment 06 =============== These are practical assignments. Please send your solutions by email by *Wednesday* Feb 30, 11:59 pm, subject "PHY598 assignment 06" Materials from http://becksteinlab.physics.asu.edu/pages/courses/2013/SimBioNano/03 can be used. Include as individual attachments (or zip file): 1) the scripts you have written (named as in the example) 2) a document (plain text) that includes YOUR NAME and the required output as described below. Put output between lines like this (the command you used in the brackets):: --------------[command]------------- (paste/insert your output here) .... -------------------------------------------------- 3) You *may* use any scripts provided in class as an example and copy parts verbatim. This will not be considered plagiarism. For this assignment you are not to copy the script of another student. You may discuss with other students but you must write your own code and run your own test cases. Help on Python is available from http://becksteinlab.physics.asu.edu/pages/courses/2013/SimBioNano/03/IntroductiontoPython/p03_instructor.html#task-reading-a-coordinate-file (see links) and within the Python interpreter with the help() function. For instance, in order to lear more about the len() function say >>> help("len") or simply >>> help(len) (The '>>>' is the Python prompt and should not be typed!) --------------------------------------------------------------------------- 6.1) Python control statements For each of the following control statements, briefly state (a) the syntax, (b) the purpose, (c) a short example of Python code using the statement. 1) 'for' loop 2) 'while' loop 3) 'if' statement 4) 'break' statement 5) 'continue' statement 6.2) Lists and strings Lists and strings share some similarities but also differences. Let's look at them. (Type code after >>> in the Python interpreter). >>> bag = ["guide", "towel", "tea", 42] >>> ga = "Four score and seven years ago" 1) How do you have to slice 'bag' in order to get ['towel', 'tea']? 2) What does 'bag[::-1]' do? How do you slice bag in order to get ['tea', 'towel']? 3) Strings can also be sliced. How do you have to slice 'ga' to get (1) "Four" (2) "seven" 4) You can access elements of a list in a variety of ways: What does >>> bag[0] = 'book' do? (print bag!) Create two new variables: >>> mybag = bag >>> yourbag = bag[:] and use them: >>> mybag[3] = "mice" >>> yourbag.append("money") What is the content of bag, mybag, yourbag? From your observation, how does the assignment 'x = a' differs from 'y = a[:]'? 5) Try >>> ga[:4] = "Three" What happens? (Strings are "immutable" whereas lists are "mutable" objects in Python.) How would you construct the string "Three score and seven years ago" from ga and the string "Three"? 6) What does - ga.split() - a, b, c = ga.split()[:3] - list([1,2,3]) - list(ga) do? You can show the output but you need to explain in your own words what is happening. 7) Nested lists: for :: bags = [['salt', 'pepper'], ['pen', 'eraser', 'ruler']] How do you have to index bags to get * ['salt', 'pepper'] * 'pepper' * 'ruler' 8) For a list of coordinates of four particles:: r = [[0.0, 0.0, 0.0], [1.34234, 1.34234, 0.0], [1.34234, 0.0, 1.34234], [0.0, 1.34234, 1.34234]] * How do you access the coordinates of the second particle and what dod you get? * How do you access the y-coordinate of the second particle and what do you get? * Write code to translate all particles by a vector t :: t = [1.34234, -1.34234, -1.34234] Show the code and the translated coordinates. 6.3 XYZ file reader Make a Python function out of the XYZ reader from the class. It should * take filename as an argument * return a tuple (atoms, coordinates) * check that the number of coordinates read agrees with the number stated in the file. If it doesn't then your code should "raise a ValueError". A ValueError is a Python exception. Raising exceptions is Python's way to signal that something has gone wrong. You do this by adding the ``raise`` statement and the exception :: raise ValueError("number of coordinates read does not agree with number of atoms stated in file") Your function should be useable like this :: atoms, coordinates = read_xyz(filename) Download the test files test_06_0.xyz test_06_1.xyz from http://becksteinlab.physics.asu.edu/pages/courses/2013/SimBioNano/03/ Test your in a script and send the output together with your code:: #!/usr/bin/env python # Assignment 6.3 # your name testfiles = ["test_06_0.xyz", "test_06_0.xyz"] def read_xyz(filename): # you have to do this for f in testfiles: print("Testing %r" % f) atoms, coordinates = read_xyz("test_06_0.xyz") print(atoms[3:8]) print(coordinates[3:8]) 6.4 XYZ-file output Write code that takes a list of atoms and a list of coordinates and writes it to an XYZ file. The title should be optional (and default to "simulation"). The XYZ format looks like this. First line is the number of atoms, second line is a string, all following lines contain four space-separated entries: atom name and coordinates. N title text ......... atom1 x y z atom2 x y z ... atomN x y z The basic data structures for our code will be atoms: a list of atom names, e.g. ["Ar", "Ar", "Ne", ...] coordinates: a list of coordinates; each coordinate is (for now) a list of the three cartesian coordinates: [[0.32, -0.1, 2.0], [1.99, -0.667, 2.8907], ...] Verify that the code you wrote in 6.3 can read the output. 1) show your code for your function write_xyz(filename, atoms, coordinates, title="simulation") 2) apply your function to the following input:: atoms = ['He', 'He', 'He', 'He'] coordinates = [[0.0, 0.0, 0.0], [1.34234, 1.34234, 0.0], [1.34234, 0.0, 1.34234], [0.0, 1.34234, 1.34234]] * send the output (call it a06.xyz) * read a06.xyz with read_xyz(); send the output