Science_Chemistry
[ class tree: Science_Chemistry ] [ index: Science_Chemistry ] [ all elements ]

Source for file Atom.php

Documentation is available at Atom.php

  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$
  20. //
  21.  
  22. require_once "Science/Chemistry.php";
  23.  
  24. /**
  25.  * Base class representing an Atom
  26.  *
  27.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  28.  * @version 1.0
  29.  * @access  public
  30.  * @package Science_Chemistry
  31.  */
  32.  
  33.     /**
  34.      * Element symbol
  35.      *
  36.      * @var     string 
  37.      * @access  private
  38.      *
  39.      * @see     getElement();
  40.      */
  41.     var $element="";
  42.  
  43.     /**
  44.      * Science_Chemistry_Coordinates object
  45.      *
  46.      * @var     object  Science_Chemistry_Coordinates 
  47.      * @access  private
  48.      *
  49.      * @see     getCoordinates();
  50.      */
  51.     var $xyz;
  52.  
  53.     /**
  54.      * Constructor for the class, requires the element symbol
  55.      * and an optional array of coordinates
  56.      *
  57.      * @param   string  $element    chemical symbol
  58.      * @param   optional array   $coords     array of coordinates (x, y, z)
  59.      * @access  public
  60.      * @return  object  Science_Chemistry_Atom 
  61.      *
  62.      * @see     setCoordinates()
  63.      */
  64.     function Science_Chemistry_Atom($element$coords=""{
  65.         if ($element && preg_match("/[[:alpha:]]{1,2}/"$element))
  66.             $this->element $element;
  67.         else
  68.             return null;
  69.         if (is_array($coords&& count($coords== 3)
  70.             if (!$this->xyz = new Science_Chemistry_Coordinates($coords))
  71.                 return null;
  72.     }
  73.  
  74.     /**
  75.      * Sets the coordinates for the atom object
  76.      *
  77.      * @param   array   $coords     array of coordinates (x, y, z)
  78.      * @return  boolean 
  79.      * @access  public
  80.      */
  81.     function setCoordinates($coords{
  82.         $this->xyz = new Science_Chemistry_Coordinates($coords);
  83.         return (is_object($this->xyz&& !empty($this->xyz));
  84.     }
  85.  
  86.     /**
  87.      * Returns the chemical symbol for the atom
  88.      *
  89.      * @return  string 
  90.      * @access  public
  91.      *
  92.      * @see     $element;
  93.      */
  94.     function getElement({
  95.         return $this->element;
  96.     }
  97.  
  98.     /**
  99.      * Returns the coordinates object for the atom
  100.      *
  101.      * @return  object  Science_Chemistry_Coordinates 
  102.      * @access  public
  103.      *
  104.      * @see     $xyz;
  105.      */
  106.     function getCoordinates({
  107.         return $this->xyz;
  108.     }
  109.  
  110.     /**
  111.      * Calculates the cartesian distance from this atom
  112.      * instance to another
  113.      *
  114.      * @param   object  Science_Chemistry_Atom $atom2 
  115.      * @return  float   distance
  116.      * @access  public
  117.      */
  118.     function distance($atom2{
  119.         if (!empty($this->xyz&& Science_Chemistry_Coordinates::areCoordinates($this->xyz
  120.             && Science_Chemistry_Atom::isAtom($atom2))
  121.             return $this->xyz->distance($atom2->xyz);
  122.         else
  123.             return -1.0;
  124.     }
  125.  
  126.     /**
  127.      * Checks if the object is an instance of Science_Chemistry_Atom
  128.      *
  129.      * @param   object  Science_Chemistry_Atom $obj 
  130.      * @return  boolean 
  131.      * @access  public
  132.      */
  133.     function isAtom($obj{
  134.         return  (is_object($obj&& 
  135.                  (strtolower(strtolower(get_class($obj))) == strtolower("Science_Chemistry_Atom")
  136.                   || is_subclass_of($objstrtolower("Science_Chemistry_Atom")))
  137.                 );
  138.     }
  139.  
  140.     /**
  141.      * Returns a string representation of the Science_Chemistry_Atom object
  142.      * Alias of toXYZ()
  143.      *
  144.      * @return  string 
  145.      * @access  public
  146.      * @see toXYZ()
  147.      */
  148.     function toString({
  149.         return $this->toXYZ();
  150.     }
  151.  
  152.     /**
  153.      * Returns a XYZ representation of the Science_Chemistry_Atom object
  154.      *
  155.      * @return  string 
  156.      * @access  public
  157.      * @see toString()
  158.      */
  159.     function toXYZ({
  160.         if ($this->element && $this->xyz)
  161.             return sprintf("%2s",$this->element)." ".$this->xyz->toString();
  162.     }
  163.  
  164.     /**
  165.      * Returns a CML representation of the Science_Chemistry_Atom object
  166.      * Accepts an optional id
  167.      *
  168.      * @param   optional    string  $id
  169.      * @return  string 
  170.      * @access  public
  171.      */
  172.     function toCML($id=1{
  173.         $out = "   <atom title=\"atom\" id=\"$id\">\n";
  174.         $out .= "    <string title=\"name\">".$this->element."</string>\n";
  175.         $out .= "    ".$this->xyz->toCML();
  176.         $out .= "   </atom>\n";
  177.         return $out;
  178.     }
  179.  
  180. // end of class Science_Chemistry_Atom
  181.  
  182. // vim: expandtab: ts=4: sw=4
  183. ?>

Documentation generated on Mon, 11 Mar 2019 15:48:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.