diff --git a/CodeExamples/Molecule.ipynb b/CodeExamples/Molecule.ipynb index 93c48a34600eb48de13284315865b8764a52d05b..bfcd610ef83ca97295ec950f283b8afc2f9c75c2 100644 --- a/CodeExamples/Molecule.ipynb +++ b/CodeExamples/Molecule.ipynb @@ -33,71 +33,14 @@ " New concept of extending a class which inherits variable and method.\n", " \"\"\"\n", "\n", - " def __init__(self,x,y,z,r):\n", - " \"\"\"\n", - " Constructor to form an atom with x,y,z,r parameters\n", - " \"\"\"\n", - " Vector3d.__init__(self,x,y,z) # set underlying Vector3d\n", - " self.radius = float(r) # add radius of atom\n", "\n", - " def __str__(self):\n", - " \"\"\"\n", - " Implemnts str() (this overwrite the one in Vector3d)\n", - " \"\"\"\n", - " return \"Atom : r = \" + str(self.radius) + \" at \" + Vector3d.__str__(self)\n", - "\n", - " def isInside(self,v):\n", - " \"\"\"\n", - " isInside method, returns True if Vector3d v in inside atom.\n", - " (Note distance() inherited from Vector3d)\n", - " \"\"\"\n", - " return self.distance(v) <= self.radius\n", "\n", "class Molecule(list):\n", " \"\"\"\n", " Molecule, being a list to hold Atoms\n", " \"\"\"\n", - " def __init__(self,*args):\n", - " \"\"\"\n", - " Constructor to form a Molecule with optional arguments each one assume to be an Atom\n", - " Note *args syntax, new concept.\n", - " \"\"\"\n", - " list.__init__(self) # Init underlying list\n", - " for a in args: # Append any args to list, note args will be a list\n", - " self.append(a)\n", - "\n", - " def __str__(self):\n", - " \"\"\"\n", - " Format string of all atoms\n", - " \"\"\"\n", - " s = \"\"\n", - " for a in self:\n", - " s += str(a) + \"\\n\"\n", - " return s\n", - "\n", - " def inInside(self, v):\n", - " \"\"\"\n", - " Method to check if vector is inside moleule.\n", - " \"\"\"\n", - " for a in self:\n", - " if a.isInside(v):\n", - " return True # Jump out as soon as inside an atom.\n", + " \n", "\n", - " return False # if here then outside\n", - "\n", - " def fromFile(self,file):\n", - " \"\"\"\n", - " Read Atoms from a file\n", - " \"\"\"\n", - " for line in file.readlines():\n", - " if len(line) > 0 and not line.startswith(\"#\"): # Ignore blank lines and comments\n", - " tokens = line.split(\",\")\n", - " r = float(tokens[1]) # ignore first token with name\n", - " x = float(tokens[2])\n", - " y = float(tokens[3])\n", - " z = float(tokens[4])\n", - " self.append(Atom(x,y,z,r)) # Form Atom and append to self\n", - " return self\n", "\n", "class BoundingBox(object):\n", " \"\"\"\n", @@ -105,51 +48,7 @@ " This class involves several new Python features.\n", " (This is the most complex class in the whole program)\n", " \"\"\"\n", - " def __init__(self, m):\n", - " \"\"\"\n", - " The Conctructor, which takes molecule or an atom.\n", - " This uses recursion to call itself if needed.\n", - " \"\"\"\n", - "\n", - " if isinstance(m,list): # If a list given ,its a moleculte\n", - " # Set initial box as two infinite vectors\n", - " self.min = Vector3d(float(\"inf\"),float(\"inf\"),float(\"inf\"))\n", - " self.max = Vector3d(float(\"-inf\"),float(\"-inf\"),float(\"-inf\"))\n", - "\n", - " # Now add it atoms one at a time\n", - " for a in m:\n", - " # Note recalling the BoundingBox for each atom (recursion)\n", - " self.addBox(BoundingBox(a))\n", - "\n", - " else: # Assume its an atom\n", - " self.min = m.subtract(m.radius)\n", - " self.max = m.add(m.radius)\n", - "\n", - "\n", - " def addBox(self,box):\n", - " \"\"\"\n", - " Method to add a box, used the min/max methods in Vector3d\n", - " \"\"\"\n", - " self.min = self.min.min(box.min)\n", - " self.max = self.max.max(box.max)\n", - "\n", - "\n", - "\n", - " def volume(self):\n", - " \"\"\"\n", - " Get the volume of the box\n", - " \"\"\"\n", - " d = self.max.subtract(self.min)\n", - " return d.x*d.y*d.z # Return the volume\n", - "\n", - " def getRandomPoint(self):\n", - " \"\"\"\n", - " Get a random point in the bounding box as a Vector3d.\n", - " \"\"\"\n", - " x = random.uniform(self.min.x,self.max.x)\n", - " y = random.uniform(self.min.y,self.max.y)\n", - " z = random.uniform(self.min.z,self.max.z)\n", - " return Vector3d(x,y,z)\n", + " \n", "\n", "\n", "def main():\n", @@ -157,37 +56,8 @@ " \"\"\"\n", "\n", " file = open(str(input(\"File : \")),\"r\") # Open file\n", - " mol = Molecule().fromFile(file) # Read in molecule\n", - " print(\"Molecule is \\n\" + str(mol)) # Print out info\n", - " box = BoundingBox(mol) # Form Bounding box\n", - "\n", - " maxpoint = float(input(\"Number of points : \")) # Max number of points\n", - " plotInterval = maxpoint/100 # Plot internal for monitoring\n", - " xData = [] # Data liost for graph\n", - " yData = []\n", - "\n", - " # counters for number inside box total number of time round loop\n", - " inside = 0\n", - " p = 0\n", - "\n", - " while p < maxpoint: # Loop counting internal points\n", - " p += 1\n", - " # Test if a random point is inside the molecule\n", - " if mol.inInside(box.getRandomPoint()):\n", - " inside += 1\n", - "\n", - " if p % plotInterval == 0: # Store graphical data (100 point only)\n", - " xData.append(p)\n", - " yData.append(box.volume()*inside/p)\n", - "\n", - " estimate = box.volume()*inside/p # Print out final estimage\n", - " print(\"Final estimate is : \" + str(estimate))\n", - "\n", - "\n", - " plt.plot(xData,yData) # Draw graph (with default plot)\n", - " plt.ylim(0.9*estimate,1.1*estimate)\n", - " plt.title(\"Estimate of volume for \" + str(maxpoint) + \" points.\")\n", - " plt.show()\n", + " \n", + " \n", "\n", "main()\n", "\n"