]> git.treefish.org Git - phys/latlib.git/commitdiff
...
authorAlex Schmidt <alex@treefish.org>
Tue, 26 Jun 2012 11:06:08 +0000 (21:06 +1000)
committerAlex Schmidt <alex@treefish.org>
Tue, 26 Jun 2012 11:06:08 +0000 (21:06 +1000)
neigh.cpp [new file with mode: 0644]
neigh.h [new file with mode: 0644]

diff --git a/neigh.cpp b/neigh.cpp
new file mode 100644 (file)
index 0000000..a25d1e7
--- /dev/null
+++ b/neigh.cpp
@@ -0,0 +1,28 @@
+#include "neigh.h"
+
+neigh::neigh(const int& dimension, const int& length)
+{
+  nfield = new int[ipow(dimension,length)*2*dimension];
+
+  for (int ipos = 0; ipos < ipow(length,dimension); ipos++)
+    {
+      for (int idir = 0; idir < 2*dimension; idir++)
+       {
+         if ( idir < dimension ) //pos direction
+           {
+             if ( ( ipos/ipow(length,idir%dimension) ) % length == length-1 )
+               nfield[ipos*2*dimension + idir] = ipos - ipow(length,idir%dimension)*(length-1);
+             else
+               nfield[ipos*2*dimension + idir] = ipos + ipow(length,idir%dimension);
+           }
+         else //negative dir
+           {
+             if ( ( ipos/ipow(length,idir%dimension) ) % length == 0 )
+               nfield[ipos*2*dimension + idir] = ipos + ipow(length,idir%dimension)*(length-1);
+             else
+               nfield[ipos*2*dimension + idir] = ipos - ipow(length,idir%dimension);
+           }
+         
+       }
+    }
+}
diff --git a/neigh.h b/neigh.h
new file mode 100644 (file)
index 0000000..73ffe62
--- /dev/null
+++ b/neigh.h
@@ -0,0 +1,18 @@
+#ifndef NEIGH_H
+#define NEIGH_H
+
+class neigh
+{
+ private:
+  int *nfield;
+  static int ipow(const int& x, const int& p){
+    if (p == 0) return 1;
+    if (p == 1) return x;
+    return x * ipow(x, p-1);
+  }
+ public:
+  neigh(const int& length, const int& dimension);
+  int& operator[] (int i) {return nfield[i];}
+};
+
+#endif