00001
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include "GenericElement.h"
00009
00010 class fixedConcreteElement : public ConcreteElementNodeData {
00011 public:
00012 enum{nPts=4};
00013 CPoint pts[nPts], data[nPts];
00014 fixedConcreteElement(
00015 CPoint a,CPoint b,CPoint c,CPoint d)
00016 {
00017 pts[0]=a; pts[1]=b; pts[2]=c; pts[3]=d;
00018
00020 for (int i=0;i<nPts;i++) data[i]=pts[i];
00021 }
00022
00023 void shift(const CVector &v) {
00024 for (int i=0;i<nPts;i++) pts[i]+=v;
00025 }
00026
00027 CPoint getNodeLocation(int i) const {return pts[i];}
00028 const double *getNodeData(int i) const {return data[i];}
00029
00030 CPoint getCenter(void) const {
00031 CPoint r(0);
00032 for (int i=0;i<nPts;i++) r+=pts[i];
00033 return r*(1.0/nPts);
00034 }
00035 };
00036
00037 void die(const char *fmt,const char *file,int line,const char *why) {
00038 fprintf(stderr,fmt,file,line,why);
00039 abort();
00040 }
00041
00042 #define check(expr) { bool b=(expr); \
00043 if (!b) die("Check failed: %s:%d> (%s) is false\n",__FILE__,__LINE__,#expr); \
00044 }
00045
00046 bool equalVectors(const CVector &l,const CVector &r) {
00047 for (int i=0;i<3;i++)
00048 if (fabs(l[i]-r[i])>1.0e-10)
00049 return false;
00050 return true;
00051 }
00052
00053 int main() {
00054 CVector geoShift(1.2,2.3,3.4);
00055
00062 fixedConcreteElement r(CPoint(0,0,0),CPoint(1,0,0),CPoint(0,1,0),CPoint(0,0,1));
00063 fixedConcreteElement l(CPoint(0,0,0),CPoint(-1,0,0),CPoint(0,1,0),CPoint(0,0,1));
00064 l.shift(geoShift);
00065 r.shift(geoShift);
00066
00067 GenericElement tet(4);
00068 CVector natc;
00069
00070
00071 check(tet.element_contains_point(r.getCenter(),r,natc));
00072 check(tet.element_contains_point(l.getCenter(),l,natc));
00073 check(!tet.element_contains_point(r.getCenter(),l,natc));
00074 check(!tet.element_contains_point(l.getCenter(),r,natc));
00075
00076
00077 CVector interp;
00078 check(tet.interpolate(3,r,r.getCenter(),interp));
00079 check(equalVectors(interp,r.getCenter()-geoShift));
00080 check(tet.interpolate(3,l,l.getCenter(),interp));
00081 check(equalVectors(interp,l.getCenter()-geoShift));
00082
00083
00084 for (int i=0;i<4;i++) {
00085 check(tet.interpolate(3,r,r.getNodeLocation(i),interp));
00086 check(equalVectors(interp,CVector(r.getNodeData(i))));
00087
00088 double frac=0.1+0.2*i;
00089 CPoint middle(0,frac,0); middle+=geoShift;
00090 CVector interpL,interpR;
00091 check(tet.interpolate(3,l,middle,interpL));
00092 check(tet.interpolate(3,r,middle,interpR));
00093 check(equalVectors(interpL,interpR));
00094 CVector theoryValue=(1-frac)*CVector(r.getNodeData(0))+
00095 frac*CVector(r.getNodeData(2));
00096 check(equalVectors(interpL,theoryValue));
00097 }
00098
00099 printf("All tests passed.\n");
00100 return 0;
00101 }