// -*- C++ -*-
// $RCSfile: cubicmaterial.C,v $
// $Revision: 1.7 $
// $Author: langer $
// $Date: 2001/10/25 21:15:08 $

/* This software was produced by NIST, an agency of the U.S. government,
 * and by statute is not subject to copyright in the United States.
 * Recipients of this software assume all responsibilities associated
 * with its operation, modification and maintenance. However, to
 * facilitate maintenance we ask that before distributing modifed
 * versions of this software, you first contact the authors at
 * oof_manager@ctcms.nist.gov. 
 */

#include "materialcmd.h"
#include "cubicmaterial.h"
#include "readbinary.h"

CharString CubicMaterial::type("cubic");
MaterialTypeRegistration
    CubicMaterial::reg("cubic",
		       new MaterialCmd<CubicMaterial>,
		       new MaterialTriCmd<CubicMaterial>,
		       2.0);

float CubicMaterial::young_dflt(1.0);
float CubicMaterial::poisson_dflt(0.33);
float CubicMaterial::t_coeff_dflt(0.0);
#ifdef THERMAL
float CubicMaterial::kappa_dflt(0.0);
#endif // THERMAL
float CubicMaterial::anisotropy_dflt(1.0);
EulerAngle CubicMaterial::orientation_dflt;

void CubicMaterial::output(FILE* fp_goof, unsigned char &flag,
			 int n1, int n2, int n3) const
{
    MaterialT::output(fp_goof, flag, n1, n2, n3);
    writebinary(fp_goof, float(orientation.alpha()));
    writebinary(fp_goof, float(orientation.beta()));
    writebinary(fp_goof, float(orientation.gamma()));
    writebinary(fp_goof, flag);
    writebinary(fp_goof, poisson);
    writebinary(fp_goof, young);
    writebinary(fp_goof, t_coeff);
#ifdef THERMAL
    writebinary(fp_goof, kappa);
#endif // THERMAL
    writebinary(fp_goof, anisotropy);
}

void CubicMaterial::output(FILE* fp_goof,  unsigned char &flag,
			   int n1, int n2, int n3, int same_element) const
{
    MaterialT::output(fp_goof, flag, n1, n2, n3, same_element);
    writebinary(fp_goof, float(orientation.alpha()));
    writebinary(fp_goof, float(orientation.beta()));
    writebinary(fp_goof, float(orientation.gamma()));
    writebinary(fp_goof, flag);
    writebinary(fp_goof, same_element);
    writebinary(fp_goof, poisson);
    writebinary(fp_goof, young);
    writebinary(fp_goof, t_coeff);
#ifdef THERMAL
    writebinary(fp_goof, kappa);
#endif // THERMAL
    writebinary(fp_goof, anisotropy);
}

void CubicMaterial::write_oof2(ostream &os, const CharString &indent) const {
  MaterialT::write_oof2(os, indent);
  os << "," << endl << indent
     << "CubicElasticity('cubic" << index() << "', C11="
     << young*(1-poisson)/((1+poisson)*(1-2*poisson))
     << ", C12="
     << young*poisson/((1+poisson)*(1-2*poisson))
     << ", C44="
     << anisotropy*young/(2*(1+poisson)) << ")";
  os << "," << endl << indent
     << "Orientation('orientation" << index() << "', EulerAngle("
     << orientation.alpha() << ", "
     << orientation.beta() << ", "
     << orientation.gamma() << "))";
  if(t_coeff != 0.0)
    os << "," << endl << indent
       << "IsotropicThermalExpansion('thermalE" << index() << "', "
       << t_coeff << ")";
}

int CubicMaterial::compare(const Material *other) const {
  if(!MaterialT::compare(other)) return 0;
  CubicMaterial *m2 = (CubicMaterial*) other;
  if(poisson != m2->poisson) return 0;
  if(young != m2->young) return 0;
  if(t_coeff != m2->t_coeff) return 0;
  if(!(orientation == m2->orientation)) return 0;
  if(anisotropy != m2->anisotropy) return 0;
#ifdef THERMAL
  if(kappa != m2->kappa) return 0;
#endif // THERMAL
  return 1;
}
