home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Molecule_XYZ.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.9 KB  |  125 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Molecule_XYZ.php,v 1.4 2003/01/04 11:56:25 mj Exp $
  20. //
  21.  
  22. require_once "Science/Chemistry.php";
  23. require_once "Science/Chemistry/Molecule.php";
  24.  
  25. /**
  26.  * Base class representing a Molecule from a XYZ format file
  27.  *
  28.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  29.  * @version 1.0
  30.  * @access  public
  31.  * @package Science_Chemistry
  32.  */
  33. class Science_Chemistry_Molecule_XYZ extends Science_Chemistry_Molecule {
  34.  
  35.     /**
  36.      * Energy of the molecule. Optional value in XYZ file format.
  37.      *
  38.      * @var float
  39.      * @access public
  40.      */
  41.     var $energy = 0.0;
  42.  
  43.     /**
  44.      * Constructor for the class, accepts 2 optional parameters:
  45.      * the data and its source. Possible values for $src: "file", "string"
  46.      *
  47.      * @param   optional    string $xyzdata
  48.      * @param   optional    string  $src   one of "file" or "string"
  49.      * @return  object Science_Chemistry_Molecule_XYZ
  50.      * @access  public
  51.      * @see     parseXYZ()
  52.      */
  53.     function Science_Chemistry_Molecule_XYZ($xyzdata="", $src="file") {
  54.         if (!empty($xyzdata))
  55.             if (!$this->parseXYZ($xyzdata, $src))
  56.                 return null;
  57.     }
  58.  
  59.     /**
  60.      * method that does the parsing of the XYZ data itself
  61.      *
  62.      * @param   string  $xyzdata
  63.      * @param   string  $src
  64.      * @return  boolean
  65.      * @access  public
  66.      * @see     Science_Chemistry_Molecule_XYZ()
  67.      */
  68.     function parseXYZ($xyzdata, $src) {
  69.         if ($src == "file") {
  70.             $line = file($xyzdata);
  71.         } elseif ($src == "string") {
  72.             $line = explode("\n", $xyzdata);
  73.         } else {
  74.             return false;
  75.         }
  76.         unset($this->atoms);
  77.         // first line is number of atoms
  78.         $this->num_atoms = trim($line[0]);
  79.         // second line is molecule name and energy
  80.         ereg("^([[:alnum:].]+)[[:space:]]+([[:digit:].-]+)",trim($line[1]),&$re);
  81.         $this->name = trim($re[1]);
  82.         $this->energy = trim($re[2]);
  83.         for ($i=2; $i<count($line); $i++) {
  84.             if (!ereg("^#",$line[$i]) && !ereg("^$", $line[$i])) {
  85.                 $this->atoms[] = $this->parseAtom($line[$i]);
  86.             }
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Parses an XYZ atom record
  92.      *
  93.      * @param   string  $line
  94.      * @return  object  Science_Chemistry_Atom
  95.      * @access   public
  96.      * @see     parseXYZ()
  97.      */
  98.     function parseAtom($line) {
  99.         list($element, $x, $y, $z) = split("[\t ]+",trim($line));
  100.         return new Science_Chemistry_Atom($element, array($x, $y, $z));
  101.     }
  102.  
  103.     /**
  104.      * Generates a string representation of the XYZ molecule
  105.      * Overrides parent Science_Chemistry_Molecule::toString() method
  106.      *
  107.      * @return  string
  108.      * @access  public
  109.      */
  110.     function toString() {
  111.         if (!$this->atoms)
  112.             return false;
  113.         $out[] = $this->num_atoms;
  114.         $out[] = $this->name."\t".sprintf("%15f",$this->energy);
  115.         reset($this->atoms);
  116.         for ($i=0; $i<$this->num_atoms; $i++)
  117.             $out[] = $this->atoms[$i]->toString();
  118.         return implode("\n",$out)."\n";
  119.     }
  120.  
  121. } // end of class Science_Chemistry_Molecule_XYZ
  122.  
  123. // vim: expandtab: ts=4: sw=4
  124. ?>
  125.