Commit 9f852f5f authored by Sebastian Hütter's avatar Sebastian Hütter
Browse files

Improve C++-ness, eliminate some macros

- fm_exp moved to math_special (exp2 was already there)
- use std::min/max template instead of macros
- use memory->create for dynamic arrays (still 1-indexed with macro)
- remove _ from function names, adjust method visibility
parent fea28d80
Loading
Loading
Loading
Loading

src/USER-MEAMC/fm_exp.cpp

deleted100644 → 0
+0 −135
Original line number Diff line number Diff line
extern "C" {
/*
   Copyright (c) 2012,2013   Axel Kohlmeyer <akohlmey@gmail.com>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
   * Neither the name of the <organization> nor the
     names of its contributors may be used to endorse or promote products
     derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* faster versions of 2**x, e**x, and 10**x in single and double precision.
 *
 * Based on the Cephes math library 2.8
 */

#include <math.h>
#include <stdint.h>

/* internal definitions for the fastermath library */

/* IEEE 754 double precision floating point data manipulation */
typedef union
{
    double   f;
    uint64_t u;
    struct {int32_t  i0,i1;};
}  udi_t;
#define FM_DOUBLE_BIAS 1023
#define FM_DOUBLE_EMASK 2146435072
#define FM_DOUBLE_MBITS 20
#define FM_DOUBLE_MMASK 1048575
#define FM_DOUBLE_EZERO 1072693248

/* generate 2**num in floating point by bitshifting */
#define FM_DOUBLE_INIT_EXP(var,num)                 \
    var.i0 = 0;                                     \
    var.i1 = (((int) num) + FM_DOUBLE_BIAS) << 20

/* double precision constants */
#define FM_DOUBLE_LOG2OFE  1.4426950408889634074
#define FM_DOUBLE_LOGEOF2  6.9314718055994530942e-1
#define FM_DOUBLE_LOG2OF10 3.32192809488736234789
#define FM_DOUBLE_LOG10OF2 3.0102999566398119521e-1
#define FM_DOUBLE_LOG10OFE 4.3429448190325182765e-1
#define FM_DOUBLE_SQRT2    1.41421356237309504880
#define FM_DOUBLE_SQRTH    0.70710678118654752440

/* optimizer friendly implementation of exp2(x).
 *
 * strategy:
 *
 * split argument into an integer part and a fraction:
 * ipart = floor(x+0.5);
 * fpart = x - ipart;
 *
 * compute exp2(ipart) from setting the ieee754 exponent
 * compute exp2(fpart) using a pade' approximation for x in [-0.5;0.5[
 *
 * the result becomes: exp2(x) = exp2(ipart) * exp2(fpart)
 */

static const double fm_exp2_q[] = {
/*  1.00000000000000000000e0, */
    2.33184211722314911771e2,
    4.36821166879210612817e3
};
static const double fm_exp2_p[] = {
    2.30933477057345225087e-2,
    2.02020656693165307700e1,
    1.51390680115615096133e3
};

static double fm_exp2(double x)
{
    double   ipart, fpart, px, qx;
    udi_t    epart;

    ipart = floor(x+0.5);
    fpart = x - ipart;
    FM_DOUBLE_INIT_EXP(epart,ipart);

    x = fpart*fpart;

    px =        fm_exp2_p[0];
    px = px*x + fm_exp2_p[1];
    qx =    x + fm_exp2_q[0];
    px = px*x + fm_exp2_p[2];
    qx = qx*x + fm_exp2_q[1];

    px = px * fpart;

    x = 1.0 + 2.0*(px/(qx-px));
    return epart.f*x;
}

double fm_exp(double x)
{
#if defined(__BYTE_ORDER__)
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    return fm_exp2(FM_DOUBLE_LOG2OFE * x);
#endif
#endif
    return exp(x);
}

/*
 * Local Variables:
 * mode: c
 * compile-command: "make -C .."
 * c-basic-offset: 4
 * fill-column: 76
 * indent-tabs-mode: nil
 * End:
 */
}
+21 −39
Original line number Diff line number Diff line
@@ -2,31 +2,25 @@
#define LMP_MEAM_H

#include <stdlib.h>
#include "memory.h"

#define maxelt 5

extern "C" {
    double fm_exp(double);
}

namespace LAMMPS_NS {

typedef enum { FCC, BCC, HCP, DIM, DIA, B1, C11, L12, B2 } lattice_t;

struct allocatable_double_2d {
  allocatable_double_2d() : dim1(0),dim2(0),ptr(0) {};
  int dim1, dim2;
  double* ptr;
};

class MEAM {
 public:
  MEAM(){};
  MEAM(Memory *mem) :
    memory(mem) {};

  ~MEAM() {
    meam_cleanup_();
    meam_cleanup();
  }
 private:
  Memory *&memory;

  // cutforce = force cutoff
  // cutforcesq = force cutoff squared

@@ -89,10 +83,10 @@ class MEAM {
  int eltind[maxelt + 1][maxelt + 1];
  int neltypes;

  allocatable_double_2d phir; // [:,:]
  double **phir;

  allocatable_double_2d phirar, phirar1, phirar2, phirar3, phirar4, phirar5,
    phirar6; // [:,:]
  double **phirar, **phirar1, **phirar2, **phirar3, **phirar4, **phirar5,
    **phirar6;

  double attrac_meam[maxelt + 1][maxelt + 1],
    repuls_meam[maxelt + 1][maxelt + 1];
@@ -108,13 +102,10 @@ class MEAM {

  int nr, nrar;
  double dr, rdrar;
 public:
 protected:
  void meam_checkindex(int, int, int, int*, int*);
  void meam_setup_param_(int*, double*, int*, int*, int*);
  void meam_dens_final_(int*, int*, int*, int*, int*, double*, double*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void G_gam(double, int, double, double*, int*);
  void dG_gam(double, int, double, double*, double*);
  void meam_dens_init_(int*, int*, int*, int*, int*, double*, int*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void getscreen(int, int, double*, double*, double*, double*, int, int*, int, int*, int, int*, int*);
  void screen(int, int, int, double*, double, double*, int, int*, int, int*, int*);
  void calc_rho1(int, int, int, int*, int*, double*, int, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*);
@@ -124,9 +115,6 @@ class MEAM {
  void dCfunc(double, double, double, double*);
  void dCfunc2(double, double, double, double*, double*);

  void meam_force_(int*, int*, int*, int*, int*, int*, double*, double*, int*, int*, int*, double*, int*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void meam_cleanup_();
  void meam_setup_done_(double*);
  void alloyparams();
  void compute_pair_meam();
  double phi_meam(double, int, int);
@@ -141,13 +129,17 @@ class MEAM {
  double erose(double, double, double, double, double, double, int);
  void interpolate_meam(int);
  double compute_phi(double, int, int);
  void meam_setup_global_(int*, int*, double*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);

 public:
  void meam_setup_global(int*, int*, double*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void meam_setup_param(int*, double*, int*, int*, int*);
  void meam_setup_done(double*);
  void meam_dens_init(int*, int*, int*, int*, int*, double*, int*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void meam_dens_final(int*, int*, int*, int*, int*, double*, double*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void meam_force(int*, int*, int*, int*, int*, int*, double*, double*, int*, int*, int*, double*, int*, int*, int*, int*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, double*, int*);
  void meam_cleanup();
};

// Functions we need for compat
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#define MAX(A,B) ((A) > (B) ? (A) : (B))

#define iszero(f) (fabs(f) < 1e-20)

@@ -171,7 +163,7 @@ Fortran Array Semantics in C.
    - Multi-Dimensional MUST be declared in reverse, so that the order when accessing is the same as in Fortran
 - arrays that are passed externally via the meam_* functions must use the arr*v() functions below
   (or be used with 0-based indexing)
 - allocatable arrays (only global phir*) are actually a struct, and must be accessed with arr2()
 - allocatable arrays must be accessed with arr2()
*/

// we receive a pointer to the first element, and F dimensions is ptr(a,b,c)
@@ -194,19 +186,9 @@ Fortran Array Semantics in C.
  ptr[(i - 1) + (j - 1) * (DIM1__##ptr) +                                      \
      (k - 1) * (DIM1__##ptr) * (DIM2__##ptr)]

// allocatable arrays via special type
#define allocate_2d(arr, cols, rows)                                           \
  arr.dim1 = cols;                                                             \
  arr.dim2 = rows;                                                             \
  arr.ptr = (double*)calloc((size_t)(cols) * (size_t)(rows), sizeof(double));
#define allocated(a) (a.ptr != NULL)
#define meam_deallocate(a)                                                     \
  ({                                                                           \
    free(a.ptr);                                                               \
    a.ptr = NULL;                                                              \
  })
// allocatable arrays
// access data with same index as used in fortran (1-based)
#define arr2(arr, i, j) arr.ptr[(arr.dim1) * (j - 1) + (i - 1)]
#define arr2(arr, i, j) arr[j-1][i-1]

};

+9 −9
Original line number Diff line number Diff line
@@ -3,15 +3,15 @@
using namespace LAMMPS_NS;

void
MEAM::meam_cleanup_(void)
MEAM::meam_cleanup(void)
{

  meam_deallocate(this->phirar6);
  meam_deallocate(this->phirar5);
  meam_deallocate(this->phirar4);
  meam_deallocate(this->phirar3);
  meam_deallocate(this->phirar2);
  meam_deallocate(this->phirar1);
  meam_deallocate(this->phirar);
  meam_deallocate(this->phir);
  memory->destroy(this->phirar6);
  memory->destroy(this->phirar5);
  memory->destroy(this->phirar4);
  memory->destroy(this->phirar3);
  memory->destroy(this->phirar2);
  memory->destroy(this->phirar1);
  memory->destroy(this->phirar);
  memory->destroy(this->phir);
}
 No newline at end of file
+7 −6
Original line number Diff line number Diff line
#include "meam.h"
#include <math.h>
#include "math_special.h"

using namespace LAMMPS_NS;
// Extern "C" declaration has the form:
@@ -20,7 +21,7 @@ using namespace LAMMPS_NS;
//

void
MEAM::meam_dens_final_(int* nlocal, int* nmax, int* eflag_either, int* eflag_global,
MEAM::meam_dens_final(int* nlocal, int* nmax, int* eflag_either, int* eflag_global,
                 int* eflag_atom, double* eng_vdwl, double* eatom, int* ntype,
                 int* type, int* fmap, double* Arho1, double* Arho2,
                 double* Arho2b, double* Arho3, double* Arho3b, double* t_ave,
@@ -221,7 +222,7 @@ MEAM::G_gam(double Gamma, int ibar, double gsmooth_factor, double* G, int* error
      *G = sqrt(1.0 + Gamma);
    }
  } else if (ibar == 1) {
    *G = fm_exp(Gamma / 2.0);
    *G = MathSpecial::fm_exp(Gamma / 2.0);
  } else if (ibar == 3) {
    *G = 2.0 / (1.0 + exp(-Gamma));
  } else if (ibar == -5) {
@@ -242,9 +243,9 @@ MEAM::dG_gam(double Gamma, int ibar, double gsmooth_factor, double* G, double* d
{
  // Compute G(Gamma) and dG(gamma) based on selection flag ibar:
  //   0 => G = sqrt(1+Gamma)
  //   1 => G = fm_exp(Gamma/2)
  //   1 => G = MathSpecial::fm_exp(Gamma/2)
  //   2 => not implemented
  //   3 => G = 2/(1+fm_exp(-Gamma))
  //   3 => G = 2/(1+MathSpecial::fm_exp(-Gamma))
  //   4 => G = sqrt(1+Gamma)
  //  -5 => G = +-sqrt(abs(1+Gamma))
  double gsmooth_switchpoint;
@@ -264,10 +265,10 @@ MEAM::dG_gam(double Gamma, int ibar, double gsmooth_factor, double* G, double* d
      *dG = 1.0 / (2.0 * *G);
    }
  } else if (ibar == 1) {
    *G = fm_exp(Gamma / 2.0);
    *G = MathSpecial::fm_exp(Gamma / 2.0);
    *dG = *G / 2.0;
  } else if (ibar == 3) {
    *G = 2.0 / (1.0 + fm_exp(-Gamma));
    *G = 2.0 / (1.0 + MathSpecial::fm_exp(-Gamma));
    *dG = *G * (2.0 - *G) / 2;
  } else if (ibar == -5) {
    if ((1.0 + Gamma) >= 0) {
+10 −9
Original line number Diff line number Diff line
#include "meam.h"
#include <math.h>
#include "math_special.h"

using namespace LAMMPS_NS;
//     Extern "C" declaration has the form:
@@ -21,7 +22,7 @@ using namespace LAMMPS_NS;
//

void
MEAM::meam_dens_init_(int* i, int* nmax, int* ntype, int* type, int* fmap, double* x,
MEAM::meam_dens_init(int* i, int* nmax, int* ntype, int* type, int* fmap, double* x,
                int* numneigh, int* firstneigh, int* numneigh_full,
                int* firstneigh_full, double* scrfcn, double* dscrfcn,
                double* fcpair, double* rho0, double* arho1, double* arho2,
@@ -209,14 +210,14 @@ MEAM::calc_rho1(int i, int nmax, int ntype, int* type, int* fmap, double* x,
        aj = rij / this->re_meam[eltj][eltj] - 1.0;
        ro0i = this->rho0_meam[elti];
        ro0j = this->rho0_meam[eltj];
        rhoa0j = ro0j * fm_exp(-this->beta0_meam[eltj] * aj) * sij;
        rhoa1j = ro0j * fm_exp(-this->beta1_meam[eltj] * aj) * sij;
        rhoa2j = ro0j * fm_exp(-this->beta2_meam[eltj] * aj) * sij;
        rhoa3j = ro0j * fm_exp(-this->beta3_meam[eltj] * aj) * sij;
        rhoa0i = ro0i * fm_exp(-this->beta0_meam[elti] * ai) * sij;
        rhoa1i = ro0i * fm_exp(-this->beta1_meam[elti] * ai) * sij;
        rhoa2i = ro0i * fm_exp(-this->beta2_meam[elti] * ai) * sij;
        rhoa3i = ro0i * fm_exp(-this->beta3_meam[elti] * ai) * sij;
        rhoa0j = ro0j * MathSpecial::fm_exp(-this->beta0_meam[eltj] * aj) * sij;
        rhoa1j = ro0j * MathSpecial::fm_exp(-this->beta1_meam[eltj] * aj) * sij;
        rhoa2j = ro0j * MathSpecial::fm_exp(-this->beta2_meam[eltj] * aj) * sij;
        rhoa3j = ro0j * MathSpecial::fm_exp(-this->beta3_meam[eltj] * aj) * sij;
        rhoa0i = ro0i * MathSpecial::fm_exp(-this->beta0_meam[elti] * ai) * sij;
        rhoa1i = ro0i * MathSpecial::fm_exp(-this->beta1_meam[elti] * ai) * sij;
        rhoa2i = ro0i * MathSpecial::fm_exp(-this->beta2_meam[elti] * ai) * sij;
        rhoa3i = ro0i * MathSpecial::fm_exp(-this->beta3_meam[elti] * ai) * sij;
        if (this->ialloy == 1) {
          rhoa1j = rhoa1j * this->t1_meam[eltj];
          rhoa2j = rhoa2j * this->t2_meam[eltj];
Loading