/***************************************************************************
 *   Copyright (C) 2006 by Harish Kukreja                                  *
 *   harish.kukreja@gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//
// C++ Interface: computations
//
// Description: Definitions of miscellaneous computation functions used in Physiks. Also includes
//              definitions for functions that modify the states of PhysicalObjects according to
//              fields.
//
// Author: Harish Kukreja <harish.kukreja@gmail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//


#include "computations.h"
#include "objects.h"

#include <gsl/gsl_blas.h>
#include <gsl/gsl_const_mksa.h>
#include <gsl/gsl_math.h>

using namespace Physiks;

PHYSCALAR Physiks::Computation::distance(const PhysicalObject &a, const PhysicalObject &b) {
  return Vector::norm(a.position - b.position);
}

Vector Physiks::Computation::connecting_vector(const PhysicalObject &a,
    const PhysicalObject &b) {
  return Vector::normalize(b.position - a.position);
}

PHYSCALAR Physiks::Computation::gravitational_force(const PhysicalObject &a,
    const PhysicalObject &b) {
  PHYSCALAR dist = distance(a, b);
  return (GSL_CONST_MKSA_GRAVITATIONAL_CONSTANT * a.mass * b.mass / (dist * dist));
}

Vector Physiks::Computation::gravitational_force_vector(const PhysicalObject &a, const PhysicalObject &b) {
  return gravitational_force(a, b) * connecting_vector(a, b);
}

PHYSCALAR Physiks::Computation::coulomb_force(const PhysicalObject &a,
    const PhysicalObject &b) {
  PHYSCALAR dist = distance(a, b);
  return a.charge * b.charge / (GSL_CONST_MKSA_VACUUM_PERMITTIVITY * 4.0 * M_PI * (dist * dist));
}

Vector Physiks::Computation::coulomb_force_vector(const PhysicalObject &a, const PhysicalObject &b) {
  return coulomb_force(a, b) * connecting_vector(a, b);
}
