Exemplo n.º 1
0
TYPED_TEST(MathScale2Test, DataTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	{
		Scale2 s{1, 2};
		auto p = s.data();
		EXPECT_TRUE(p != nullptr);
		EXPECT_EQ(1, *p);
		*p = 5;
		++p;
		EXPECT_EQ(2, *p);

		EXPECT_EQ(Scale2(5, 2), s);
	}
#if !defined(BKSGE_GCC)
	{
		BKSGE_STATIC_CONSTEXPR Scale2 s{1, 2};
		BKSGE_STATIC_CONSTEXPR auto p = s.data();
		BKSGE_CONSTEXPR_EXPECT_TRUE(p != nullptr);
		BKSGE_CONSTEXPR_EXPECT_EQ(1, p[0]);
		BKSGE_CONSTEXPR_EXPECT_EQ(2, p[1]);
	}
#endif
}
Exemplo n.º 2
0
TYPED_TEST(MathScale2Test, CopyAssignTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	Scale2 v1(0, 1);
	Scale2 v2(3, 4);
	EXPECT_EQ(0, v1[0]);
	EXPECT_EQ(1, v1[1]);
	EXPECT_EQ(3, v2[0]);
	EXPECT_EQ(4, v2[1]);

	v1 = Scale2(6, -7);
	v2 = Scale2i(-9, 10);
	EXPECT_EQ( 6, v1[0]);
	EXPECT_EQ(-7, v1[1]);
	EXPECT_EQ(-9, v2[0]);
	EXPECT_EQ(10, v2[1]);

	// 自己代入
	v1 = v1;
	v2 = v2;
	EXPECT_EQ( 6, v1[0]);
	EXPECT_EQ(-7, v1[1]);
	EXPECT_EQ(-9, v2[0]);
	EXPECT_EQ(10, v2[1]);

	// 多重代入
	v1 = v2 = Scale2f(4, 5);
	EXPECT_EQ(4, v1[0]);
	EXPECT_EQ(5, v1[1]);
	EXPECT_EQ(4, v2[0]);
	EXPECT_EQ(5, v2[1]);
}
Exemplo n.º 3
0
TYPED_TEST(MathScale2Test, IdentityTest)
{
	using Scale2 = bksge::math::Scale2<TypeParam>;

	BKSGE_CONSTEXPR_OR_CONST auto s = Scale2::Identity();
	static_assert(std::is_same<decltype(s), const Scale2>::value, "");
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(1, 1), s);
}
Exemplo n.º 4
0
TYPED_TEST(MathScale2Test, ZeroTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	BKSGE_CONSTEXPR_OR_CONST auto s = Scale2::Zero();
	static_assert(std::is_same<decltype(s), const Scale2>::value, "");
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(0, 0), s);
}
Exemplo n.º 5
0
TYPED_TEST(MathScale2Test, SubTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	// Scale2 -= Scale2
	{
		Scale2 s;
		Scale2 t = (s -= Scale2(2, 3));
		EXPECT_EQ(Scale2(-2, -3), s);
		EXPECT_EQ(t, s);
	}

	// Scale2 - Scale2 -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t =
			Scale2(-3, 4) - Scale2(0, 2);
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(-3, 2), t);
	}
}
Exemplo n.º 6
0
TYPED_TEST(MathScale2Test, DivScale2Test)
{
	using Scale2 = bksge::math::Scale2<TypeParam>;

	// Scale2 /= Scale2
	{
		Scale2 s(8, 12);

		auto t = (s /= Scale2(-1, 2));
		static_assert(std::is_same<decltype(t), Scale2>::value, "");
		EXPECT_EQ(Scale2(-8, 6), s);
		EXPECT_EQ(t, s);
	}

	// Scale2 / Scale2 -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST auto t = Scale2(-3, 4) / Scale2(1, 2);
		static_assert(std::is_same<decltype(t), const Scale2>::value, "");
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(-3, 2), t);
	}
}
Exemplo n.º 7
0
TYPED_TEST(MathScale2Test, MulScale2Test)
{
	using Scale2 = bksge::math::Scale2<TypeParam>;

	// Scale2 *= Scale2
	{
		Scale2 s(2, 3);

		auto t = (s *= Scale2(-1, 2));
		static_assert(std::is_same<decltype(t), Scale2>::value, "");
		EXPECT_EQ(Scale2(-2, 6), s);
		EXPECT_EQ(t, s);
	}

	// Scale2 * Scale2 -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST auto t = Scale2(-3, 4) * Scale2(0, 2);
		static_assert(std::is_same<decltype(t), const Scale2>::value, "");
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(0, 8), t);
	}
}
Exemplo n.º 8
0
TYPED_TEST(MathScale2Test, DivScalarTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	// Scale2 /= スカラー
	{
		Scale2 s(2, 4);
		{
			Scale2 t = (s /= 2);
			EXPECT_EQ(Scale2(1, 2), s);
			EXPECT_EQ(t, s);
		}
		{
			Scale2 t = (s /= 0.5);
			EXPECT_EQ(Scale2(2, 4), s);
			EXPECT_EQ(t, s);
		}
	}

	// Scale2 / スカラー -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = Scale2(-4, 8) / -4;
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(1, -2), t);
	}
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = Scale2(-4, 8) / 0.25;
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(-16, 32), t);
	}
}
Exemplo n.º 9
0
TYPED_TEST(MathScale2Test, MulScalarTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	// Scale2 *= スカラー
	{
		Scale2 s(2, 3);
		{
			Scale2 t = (s *= 4);
			EXPECT_EQ(Scale2(8, 12), s);
			EXPECT_EQ(t, s);
		}
		{
			Scale2 t = (s *= 0.5);
			EXPECT_EQ(Scale2(4, 6), s);
			EXPECT_EQ(t, s);
		}
	}

	// Scale2 * スカラー -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = Scale2(-3, 42) * -4;
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(12, -168), t);
	}
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = Scale2(4, 6) * 2.5;
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(10, 15), t);
	}
	// スカラー * Scale2 -> Scale2
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = 5 * Scale2(7, -8);
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(35, -40), t);
	}
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 t = -1.5 * Scale2(4, -6);
		BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(-6, 9), t);
	}
}
Exemplo n.º 10
0
TYPED_TEST(MathScale2Test, TupleGetTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	{
		Scale2 s{1, 2};

		EXPECT_EQ(1, bksge::get<0>(s));
		EXPECT_EQ(2, bksge::get<1>(s));

		bksge::get<0>(s) = 5;

		EXPECT_EQ(Scale2(5, 2), s);
		EXPECT_EQ(5, bksge::get<0>(s));
		EXPECT_EQ(2, bksge::get<1>(s));
	}
	{
		BKSGE_CONSTEXPR_OR_CONST Scale2 s{1, 2};

		BKSGE_CONSTEXPR_EXPECT_EQ(1, bksge::get<0>(s));
		BKSGE_CONSTEXPR_EXPECT_EQ(2, bksge::get<1>(s));
	}
}
Exemplo n.º 11
0
TYPED_TEST(MathScale2Test, SwapTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	Scale2 v1{11, 12};
	Scale2 v2{21, 22};

	EXPECT_EQ(Scale2(11, 12), v1);
	EXPECT_EQ(Scale2(21, 22), v2);

	v1.swap(v2);

	EXPECT_EQ(Scale2(21, 22), v1);
	EXPECT_EQ(Scale2(11, 12), v2);

	swap(v1, v2);

	EXPECT_EQ(Scale2(11, 12), v1);
	EXPECT_EQ(Scale2(21, 22), v2);
}
Exemplo n.º 12
0
PetscErrorCode ComputeJacobian_LS(DM dm, Vec locX, PetscInt cell, PetscScalar CellValues[], void *ctx)
{
  User              user = (User) ctx;
  Physics           phys = user->model->physics;
  PetscInt          dof = phys->dof;
  const PetscScalar *facegeom, *cellgeom,*x;
  PetscErrorCode    ierr;
  DM                dmFace, dmCell;

  DM                dmGrad = user->dmGrad;
  PetscInt          fStart, fEnd, face, cStart;
  Vec               locGrad, locGradLimiter, Grad;
  /*here the localGradLimiter refers to the gradient that has been multiplied by the limiter function.
   The locGradLimiter is used to construct the uL and uR, and the locGrad is used to caculate the diffusion term*/
  Vec               TempVec; /*a temperal vec for the vector restore*/

  PetscFunctionBeginUser;

  ierr = VecGetDM(user->facegeom,&dmFace);CHKERRQ(ierr);
  ierr = VecGetDM(user->cellgeom,&dmCell);CHKERRQ(ierr);

  ierr = DMGetGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
  ierr = VecDuplicate(Grad, &TempVec);CHKERRQ(ierr);
  ierr = VecCopy(Grad, TempVec);CHKERRQ(ierr);
  /*Backup the original vector and use it to restore the value of dmGrad,
    because I do not want to change the values of the cell gradient*/

  ierr = VecGetArrayRead(user->facegeom,&facegeom);CHKERRQ(ierr);
  ierr = VecGetArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
  ierr = VecGetArrayRead(locX,&x);CHKERRQ(ierr);
  ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
  ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
  {
    PetscScalar *grad;
    ierr = VecGetArray(Grad,&grad);CHKERRQ(ierr);

    /* Limit interior gradients. Using cell-based loop because it generalizes better to vector limiters. */

      const PetscInt    *faces;
      PetscInt          numFaces,f;
      PetscReal         *cellPhi; /* Scalar limiter applied to each component separately */
      const PetscScalar *cx;
      const CellGeom    *cg;
      PetscScalar       *cgrad;
      PetscInt          i;

      ierr = PetscMalloc(phys->dof*sizeof(PetscScalar),&cellPhi);CHKERRQ(ierr);

      ierr = DMPlexGetConeSize(dm,cell,&numFaces);CHKERRQ(ierr);
      ierr = DMPlexGetCone(dm,cell,&faces);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dm,cell,x,&cx);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dmCell,cell,cellgeom,&cg);CHKERRQ(ierr);
      ierr = DMPlexPointGlobalRef(dmGrad,cell,grad,&cgrad);CHKERRQ(ierr);

      /* Limiter will be minimum value over all neighbors */
      for (i=0; i<dof; i++) {
        cellPhi[i] = PETSC_MAX_REAL;
      }
      for (f=0; f<numFaces; f++) {
        const PetscScalar *ncx;
        const CellGeom    *ncg;
        const PetscInt    *fcells;
        PetscInt          face = faces[f],ncell;
        PetscScalar       v[DIM];
        PetscBool         ghost;
        ierr = IsExteriorGhostFace(dm,face,&ghost);CHKERRQ(ierr);
        if (ghost) continue;
        ierr  = DMPlexGetSupport(dm,face,&fcells);CHKERRQ(ierr);
        ncell = cell == fcells[0] ? fcells[1] : fcells[0];  /*The expression (x ? y : z) has the value of y if x is nonzero, z otherwise */
        ierr  = DMPlexPointLocalRead(dm,ncell,x,&ncx);CHKERRQ(ierr);
        ierr  = DMPlexPointLocalRead(dmCell,ncell,cellgeom,&ncg);CHKERRQ(ierr);
        Waxpy2(-1, cg->centroid, ncg->centroid, v);
        for (i=0; i<dof; i++) {
          /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */
          PetscScalar phi,flim = 0.5 * (ncx[i] - cx[i]) / Dot2(&cgrad[i*DIM],v);
          phi        = (*user->LimitGrad)(flim);
          cellPhi[i] = PetscMin(cellPhi[i],phi);
        }
      }
      /* Apply limiter to gradient */
      for (i=0; i<dof; i++) Scale2(cellPhi[i],&cgrad[i*DIM],&cgrad[i*DIM]);

      ierr = PetscFree(cellPhi);CHKERRQ(ierr);

    ierr = VecRestoreArray(Grad,&grad);CHKERRQ(ierr);
  }
  ierr = DMGetLocalVector(dmGrad,&locGradLimiter);CHKERRQ(ierr);
  ierr = DMGlobalToLocalBegin(dmGrad,Grad,INSERT_VALUES,locGradLimiter);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(dmGrad,Grad,INSERT_VALUES,locGradLimiter);CHKERRQ(ierr);

  ierr = VecCopy(TempVec, Grad);CHKERRQ(ierr);/*Restore the vector*/

  ierr = DMGetLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);
  ierr = DMGlobalToLocalBegin(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);

  ierr = DMRestoreGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
  ierr = VecDestroy(&TempVec);CHKERRQ(ierr);

  {
    const PetscScalar *grad, *gradlimiter;
    ierr = VecGetArrayRead(locGrad,&grad);CHKERRQ(ierr);
    ierr = VecGetArrayRead(locGradLimiter,&gradlimiter);CHKERRQ(ierr);
    for (face=fStart; face<fEnd; face++) {
      const PetscInt    *cells;
      PetscInt          ghost,i,j;
      PetscScalar       *fluxcon, *fluxdiff, *fx[2];
      const FaceGeom    *fg;
      const CellGeom    *cg[2];
      const PetscScalar *cx[2],*cgrad[2], *cgradlimiter[2];
      PetscScalar       *uL, *uR;
      PetscReal         FaceArea;

      ierr = PetscMalloc(phys->dof * phys->dof * sizeof(PetscScalar), &fluxcon);CHKERRQ(ierr); /*For the convection terms*/
      ierr = PetscMalloc(phys->dof * phys->dof * sizeof(PetscScalar), &fluxdiff);CHKERRQ(ierr); /*For the diffusion terms*/
      ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &uL);CHKERRQ(ierr);
      ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &uR);CHKERRQ(ierr);

      fx[0] = uL; fx[1] = uR;

      ierr = DMPlexGetLabelValue(dm, "ghost", face, &ghost);CHKERRQ(ierr);
      if (ghost >= 0) continue;
      ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dmFace,face,facegeom,&fg);CHKERRQ(ierr);
      for (i=0; i<2; i++) {
        PetscScalar dx[DIM];
        ierr = DMPlexPointLocalRead(dmCell,cells[i],cellgeom,&cg[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dm,cells[i],x,&cx[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dmGrad,cells[i],gradlimiter,&cgradlimiter[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dmGrad,cells[i],grad,&cgrad[i]);CHKERRQ(ierr);
        Waxpy2(-1,cg[i]->centroid,fg->centroid,dx);
        for (j=0; j<dof; j++) {
          fx[i][j] = cx[i][j] + Dot2(cgradlimiter[i],dx);
        }
        /*fx[0] and fx[1] are the value of the variables on the left and right
          side of the face, respectively, that is u_L and u_R.*/
      }

      ierr = RiemannSolver_Rusanov_Jacobian(user, cgrad[0], cgrad[1], fg->centroid, cg[0]->centroid, cg[1]->centroid, fg->normal,
                  fx[0], fx[1], fluxcon, fluxdiff);CHKERRQ(ierr);

      ierr = DMPlexComputeCellGeometryFVM(dm, face, &FaceArea, NULL, NULL);CHKERRQ(ierr);
        /*Compute the face area*/

      for (i=0; i<phys->dof; i++) {
        for (j=0; j<phys->dof; j++) {
          if(cells[0]<user->cEndInterior) CellValues[cells[0]*dof*dof + i*dof + j] -= cells[0]*1.0;
          if(cells[1]<user->cEndInterior) CellValues[cells[1]*dof*dof + i*dof + j] += cells[1]*1.2;
        }
      }
//      ierr = PetscPrintf(PETSC_COMM_WORLD,"\n");CHKERRQ(ierr);
      ierr = PetscFree(fluxcon);CHKERRQ(ierr);
      ierr = PetscFree(fluxdiff);CHKERRQ(ierr);
      ierr = PetscFree(uL);CHKERRQ(ierr);
      ierr = PetscFree(uR);CHKERRQ(ierr);
    }
    ierr = VecRestoreArrayRead(locGrad,&grad);CHKERRQ(ierr);
    ierr = VecRestoreArrayRead(locGradLimiter,&gradlimiter);CHKERRQ(ierr);
  }
  ierr = VecRestoreArrayRead(user->facegeom,&facegeom);CHKERRQ(ierr);
  ierr = VecRestoreArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
  ierr = VecRestoreArrayRead(locX,&x);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(dmGrad,&locGradLimiter);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);

  PetscFunctionReturn(0);
}
Exemplo n.º 13
0
void Pyramid::createVAO(void)
{

	struct pyramidVertexStride
	{
		float x;
		float y;
		float z;
		float s;
		float t;
		float nx; 
		float ny;
		float nz;
	};

	struct triangleList
	{
		unsigned short v1;
		unsigned short v2;
		unsigned short v3;
	};

	pyramidVertexStride pVerts[5];

	triangleList		pList[6];

	#if 1
		FileHandle fh2;
		FileError ferror;

		ferror = File::open(fh2, "pyramid.vbo", FILE_READ);
		assert(ferror == FILE_SUCCESS);

		ferror = File::read(fh2, &pVerts, 5 * sizeof(pyramidVertexStride));
		assert(ferror == FILE_SUCCESS);

		ferror = File::read(fh2, &pList, 6 * sizeof(triangleList));
		assert(ferror == FILE_SUCCESS);

		ferror = File::close(fh2);
		assert(ferror == FILE_SUCCESS);

	#else

		// apex
		pVerts[0].x = 0.0f;
		pVerts[0].y = 1.0f;
		pVerts[0].z = 0.0f;
		pVerts[0].s = 0.5f;
		pVerts[0].t = 0.5f;
		pVerts[0].nx = 0.0f;
		pVerts[0].ny = 2.0f;
		pVerts[0].nz = 0.0f;

		// left front
		pVerts[1].x = -1.0f;
		pVerts[1].y = -1.0f;
		pVerts[1].z = 1.0f;//-1.0f;
		pVerts[1].s = 0.0f;
		pVerts[1].t = 0.0f;

		pVerts[1].nx = -3.0f;
		pVerts[1].ny = -5.0f;
		pVerts[1].nz = -3.0f;

		// right front
		pVerts[2].x = 1.0f;
		pVerts[2].y = -1.0f;
		pVerts[2].z = 1.0f;
		pVerts[2].s = 1.0f;
		pVerts[2].t = 0.0f;

		pVerts[2].nx = 3.0f;
		pVerts[2].ny = -5.0f;
		pVerts[2].nz = -3.0f;

		// left back
		pVerts[3].x = -1.0f;
		pVerts[3].y = -1.0f;
		pVerts[3].z = -1.0f;
		pVerts[3].s = 0.0f;
		pVerts[3].t = 1.0f;

		pVerts[3].nx = -3.0f;
		pVerts[3].ny = -5.0f;
		pVerts[3].nz = 3.0f;

		// right back
		pVerts[4].x = 1.0f;
		pVerts[4].y = -1.0f;
		pVerts[4].z = -1.0f;
		pVerts[4].s = 1.0f;
		pVerts[4].t = 1.0f;

		pVerts[4].nx = 3.0f;
		pVerts[4].ny = -5.0f;
		pVerts[4].nz = 3.0f;

		// front
		pList[0].v1 = 0;
		pList[0].v2 = 1;
		pList[0].v3 = 2;

		// left
		pList[1].v1 = 0;
		pList[1].v2 = 3;
		pList[1].v3 = 1;

		// right
		pList[2].v1 = 0;
		pList[2].v2 = 2;
		pList[2].v3 = 4;

		// back
		pList[3].v1 = 0;
		pList[3].v2 = 4;
		pList[3].v3 = 3;

		// bottom 1
		pList[4].v1 = 2;
		pList[4].v2 = 1;
		pList[4].v3 = 3;

		// bottom 2
		pList[5].v1 = 4;
		pList[5].v2 = 2;
		pList[5].v3 = 3;

		//Write the data to a file ---------------------

		FileHandle fh;
		FileError ferror;

		//-----------WRITE--------------------------------

		ferror = File::open(fh, "pyramid.vbo", FILE_WRITE);
		assert(ferror == FILE_SUCCESS);

		//Write the data
		ferror = File::write(fh, &pVerts, 5 * sizeof(pyramidVertexStride));
		assert(ferror == FILE_SUCCESS);

		ferror = File::write(fh, &pList, 6 * sizeof(triangleList));
		assert(ferror == FILE_SUCCESS);

		ferror = File::close(fh);
		assert(ferror == FILE_SUCCESS);

	#endif

	if (test)
	{
		for( int i = 0; i<5; i++)
		{
			Matrix Trans(TRANS, 0.0f, 1.0f, 0.0f);
			//Matrix Scale(SCALE, 1.0f, 0.5f, 1.0f);
			Matrix Scale(SCALE, 0.3f, 0.1f, 0.3f);
			Matrix Rot( ROT_X, 90.0f * MATH_PI_180);

			Matrix M = Trans * Scale * Rot;
     

			Matrix Scale2( SCALE, 13.0f, 13.0f, 109.43f);

			Matrix Rot2; 
			Rot2.set(ROT_ORIENT, Vect(1.0f,0.0f,0.0f), Vect( 0.0f, 1.0f, 0.0f) );

			M =  M * Scale2 * Rot2;
      
			//M =  M * Scale2 ;

			Vect vert( pVerts[i].x, pVerts[i].y, pVerts[i].z);
			Vect vertNorm( pVerts[i].nx, pVerts[i].ny, pVerts[i].nz);

			Vect vout;
			Vect voutNorm;

			vout = vert * M;
			voutNorm = vertNorm * M;

			pVerts[i].x  = vout[x] ;
			pVerts[i].y  = vout[y] ;
			pVerts[i].z  = vout[z] ;
			pVerts[i].nx = voutNorm[x];
			pVerts[i].ny = voutNorm[y];
			pVerts[i].nz = voutNorm[z];

			printf(" vert[%d]  %f  %f  %f\n",i, vout[x], vout[y], vout[z]);

		}
	}

	/* Allocate and assign a Vertex Array Object to our handle */
    glGenVertexArrays(1, &this->vao);
 
    /* Bind our Vertex Array Object as the current used object */
    glBindVertexArray(this->vao);

	//Create two buffers
	GLuint vbo[2];

    /* Allocate and assign two Vertex Buffer Objects to our handle */
    glGenBuffers(2, vbo);

	// Load the combined data: ---------------------------------------------------------

	/* Bind our first VBO as being the active buffer and storing vertex attributes (coordinates) */
	glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
 
	/* Copy the vertex data to our buffer */
    // glBufferData(type, size in bytes, data, usage) 
	glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidVertexStride) * 5, &pVerts, GL_STATIC_DRAW);

	// VERTEX data: ---------------------------------------------------------
		
	// Set Attribute to 0
    // WHY - 0? and not 1,2,3 (this is tied to the shader attribute, it is defined in GLShaderManager.h)
    // GLT_ATTRIBUTE_VERTEX = 0

    // Specifies the index of the generic vertex attribute to be enabled
	glEnableVertexAttribArray(0);  

	//Compute offset
	void *offsetVert = (void *)((unsigned int)&pVerts[0].x - (unsigned int)&pVerts);

	/* Specify that our coordinate data is going into attribute index 0, and contains 3 floats per vertex */
    // ( GLuint index,  GLint size,  GLenum type,  GLboolean normalized,  GLsizei stride,  const GLvoid * pointer);
	glVertexAttribPointer(0, 3, GL_FLOAT,  GL_FALSE, sizeof(pyramidVertexStride), offsetVert);

	// Texture data: ---------------------------------------------------------
		
	// Set Attribute to 3
    // WHY - 3? and not 1,2,3 (this is tied to the shader attribute, it is defined in GLShaderManager.h)
    // GLT_ATTRIBUTE_TEXTURE0 = 3

    // Specifies the index of the generic vertex attribute to be enabled
	glEnableVertexAttribArray(3);  

	//Compute offset
	void *offsetTex = (void *)((unsigned int)&pVerts[0].s - (unsigned int)&pVerts);

	/* Specify that our coordinate data is going into attribute index 3, and contains 2 floats per vertex */
    // ( GLuint index,  GLint size,  GLenum type,  GLboolean normalized,  GLsizei stride,  const GLvoid * pointer);
	glVertexAttribPointer(3, 2, GL_FLOAT,  GL_FALSE, sizeof(pyramidVertexStride), offsetTex);

	// Normal data: ---------------------------------------------------------
		
	// Set Attribute to 2
    // WHY - 2? and not 1,2,3 (this is tied to the shader attribute, it is defined in GLShaderManager.h)
    // GLT_ATTRIBUTE_NORMAL = 2

    // Specifies the index of the generic vertex attribute to be enabled
	glEnableVertexAttribArray(2);  

	//Compute offset
	void *offsetNorm = (void *)((unsigned int)&pVerts[0].nx - (unsigned int)&pVerts);

	/* Specify that our coordinate data is going into attribute index 3, and contains 2 floats per vertex */
    // ( GLuint index,  GLint size,  GLenum type,  GLboolean normalized,  GLsizei stride,  const GLvoid * pointer);
	glVertexAttribPointer(2, 3, GL_FLOAT,  GL_FALSE, sizeof(pyramidVertexStride), offsetNorm);

	// Load the index data: ---------------------------------------------------------
	
    /* Bind our 2nd VBO as being the active buffer and storing index ) */
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]);

	/* Copy the index data to our buffer */
    // glBufferData(type, size in bytes, data, usage) 
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleList)*6, pList, GL_STATIC_DRAW);

}
Exemplo n.º 14
0
TYPED_TEST(MathScale2FloatTest, LerpTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2( 0.0,  0.0), Lerp(Scale2(0, 0), Scale2(10, 20), TypeParam(0.00)));
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2( 2.5,  5.0), Lerp(Scale2(0, 0), Scale2(10, 20), TypeParam(0.25)));
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2( 5.0, 10.0), Lerp(Scale2(0, 0), Scale2(10, 20), TypeParam(0.50)));
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2( 7.5, 15.0), Lerp(Scale2(0, 0), Scale2(10, 20), TypeParam(0.75)));
	BKSGE_CONSTEXPR_EXPECT_EQ(Scale2(10.0, 20.0), Lerp(Scale2(0, 0), Scale2(10, 20), TypeParam(1.00)));
}
Exemplo n.º 15
0
TYPED_TEST(MathScale2Test, IteratorTest)
{
	using T = TypeParam;
	using Scale2 = bksge::math::Scale2<T>;

	// begin, end (non const)
	{
		Scale2 s{1, 2};
		auto it = s.begin();
		EXPECT_TRUE(it != s.end());
		EXPECT_EQ(1, *it);
		*it++ = 5;
		EXPECT_TRUE(it != s.end());
		EXPECT_EQ(2, *it);
		++it;
		EXPECT_TRUE(it == s.end());

		EXPECT_EQ(Scale2(5, 2), s);
	}
	// begin, end (const)
	{
		const Scale2 s{1, 2};
		auto it = s.begin();
		EXPECT_TRUE(it != s.end());

		EXPECT_EQ(1, it[0]);
		EXPECT_EQ(2, it[1]);

		EXPECT_EQ(1, *it++);
		EXPECT_TRUE(it != s.end());
		EXPECT_EQ(2, *it);
		EXPECT_EQ(1, *--it);
		EXPECT_TRUE(it != s.end());
		it += 2;
		EXPECT_TRUE(it == s.end());
	}
	// cbegin, cend
	{
		const Scale2 s{1, 2};
		auto it = s.cbegin();
		EXPECT_TRUE(it != s.cend());

		EXPECT_EQ(1, it[0]);
		EXPECT_EQ(2, it[1]);

		EXPECT_EQ(1, *it++);
		EXPECT_TRUE(it != s.cend());
		EXPECT_EQ(2, *it);
		EXPECT_TRUE(it != s.cend());
		++it;
		EXPECT_TRUE(it == s.cend());
	}
#if !defined(BKSGE_GCC)
	// begin, end (constexpr)
	{
		BKSGE_STATIC_CONSTEXPR Scale2 s{1, 2};
		BKSGE_CONSTEXPR_OR_CONST auto it = s.begin();
		BKSGE_CONSTEXPR_EXPECT_TRUE(it != s.end());

		BKSGE_CONSTEXPR_EXPECT_EQ(1, it[0]);
		BKSGE_CONSTEXPR_EXPECT_EQ(2, it[1]);

		BKSGE_CONSTEXPR_EXPECT_EQ(1, *it);
		BKSGE_CONSTEXPR_OR_CONST auto it2 = it + 1;
		BKSGE_CONSTEXPR_EXPECT_TRUE(it2 != s.end());
		BKSGE_CONSTEXPR_EXPECT_EQ(2, *it2);
		BKSGE_CONSTEXPR_OR_CONST auto it3 = it2 + 1;
		BKSGE_CONSTEXPR_EXPECT_TRUE(it3 == s.end());
	}
#endif
}
Exemplo n.º 16
0
/**
  Caculate the flux using the Monotonic Upstream-Centered Scheme for Conservation Laws (van Leer, 1979)
*/
PetscErrorCode CaculateLocalFunction_LS(DM dm,DM dmFace,DM dmCell,PetscReal time,Vec locX,Vec F,User user)
{
  DM                dmGrad = user->dmGrad;
  Model             mod    = user->model;
  Physics           phys   = mod->physics;
  const PetscInt    dof    = phys->dof;
  PetscErrorCode    ierr;
  const PetscScalar *facegeom, *cellgeom, *x;
  PetscScalar       *f;
  PetscInt          fStart, fEnd, face, cStart, cell;
  Vec               locGrad, locGradLimiter, Grad;
/*
    Here the localGradLimiter refers to the gradient that
    has been multiplied by the limiter function.
    The locGradLimiter is used to construct the uL and uR,
    and the locGrad is used to caculate the diffusion term
*/
  Vec               TempVec; /*a temperal vec for the vector restore*/

  PetscFunctionBeginUser;
  ierr = DMGetGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
  ierr = VecDuplicate(Grad, &TempVec);CHKERRQ(ierr);
  ierr = VecCopy(Grad, TempVec);CHKERRQ(ierr);
/*
  Backup the original vector and use it to restore the value of dmGrad,
  because I do not want to change the values of the cell gradient.
*/
  ierr = VecGetArrayRead(user->facegeom,&facegeom);CHKERRQ(ierr);
  ierr = VecGetArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
  ierr = VecGetArrayRead(locX,&x);CHKERRQ(ierr);
  ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
  ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);

  {
    PetscScalar *grad;
    ierr = VecGetArray(Grad,&grad);CHKERRQ(ierr);
    const PetscInt    *faces;
    PetscInt          numFaces,f;
    PetscReal         *cellPhi; // Scalar limiter applied to each component separately
    const PetscScalar *cx;
    const CellGeom    *cg;
    PetscScalar       *cgrad;
    PetscInt          i;

    ierr = PetscMalloc(phys->dof*sizeof(PetscScalar),&cellPhi);CHKERRQ(ierr);
    // Limit interior gradients. Using cell-based loop because it generalizes better to vector limiters.
    for (cell=cStart; cell<user->cEndInterior; cell++) {
      ierr = DMPlexGetConeSize(dm,cell,&numFaces);CHKERRQ(ierr);
      ierr = DMPlexGetCone(dm,cell,&faces);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dm,cell,x,&cx);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dmCell,cell,cellgeom,&cg);CHKERRQ(ierr);
      ierr = DMPlexPointGlobalRef(dmGrad,cell,grad,&cgrad);CHKERRQ(ierr);
      if (!cgrad) continue;     // ghost cell, we don't compute
      // Limiter will be minimum value over all neighbors
      for (i=0; i<dof; i++) cellPhi[i] = PETSC_MAX_REAL;

      for (f=0; f<numFaces; f++) {
        const PetscScalar *ncx;
        const CellGeom    *ncg;
        const PetscInt    *fcells;
        PetscInt          face = faces[f],ncell;
        PetscScalar       v[DIM];
        PetscBool         ghost;
        ierr = IsExteriorGhostFace(dm,face,&ghost);CHKERRQ(ierr);
        if (ghost) continue;
        ierr  = DMPlexGetSupport(dm,face,&fcells);CHKERRQ(ierr);
        ncell = cell == fcells[0] ? fcells[1] : fcells[0];
        // The expression (x ? y : z) has the value of y if x is nonzero, z otherwise
        ierr  = DMPlexPointLocalRead(dm,ncell,x,&ncx);CHKERRQ(ierr);
        ierr  = DMPlexPointLocalRead(dmCell,ncell,cellgeom,&ncg);CHKERRQ(ierr);
        Waxpy2(-1, cg->centroid, ncg->centroid, v);
        for (i=0; i<dof; i++) {
          // We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005
          PetscScalar phi,flim = 0.5 * (ncx[i] - cx[i]) / Dot2(&cgrad[i*DIM],v);
          phi        = (*user->Limit)(flim);
          cellPhi[i] = PetscMin(cellPhi[i],phi);
        }
      }
      // Apply limiter to gradient
      for (i=0; i<dof; i++) Scale2(cellPhi[i],&cgrad[i*DIM],&cgrad[i*DIM]);
    }
    ierr = PetscFree(cellPhi);CHKERRQ(ierr);
    ierr = VecRestoreArray(Grad,&grad);CHKERRQ(ierr);
  }
  ierr = DMGetLocalVector(dmGrad,&locGradLimiter);CHKERRQ(ierr);
  ierr = DMGlobalToLocalBegin(dmGrad,Grad,INSERT_VALUES,locGradLimiter);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(dmGrad,Grad,INSERT_VALUES,locGradLimiter);CHKERRQ(ierr);

  ierr = VecCopy(TempVec, Grad);CHKERRQ(ierr);//Restore the vector

  ierr = DMGetLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);
  ierr = DMGlobalToLocalBegin(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);

  ierr = DMRestoreGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
  ierr = VecDestroy(&TempVec);CHKERRQ(ierr);

  {
    const PetscScalar *grad, *gradlimiter;
    const PetscInt    *cells;
    PetscInt          ghost,i,j;
    PetscScalar       *fluxcon, *fluxdiff, *fx[2],*cf[2];
    const FaceGeom    *fg;
    const CellGeom    *cg[2];
    const PetscScalar *cx[2],*cgrad[2], *cgradlimiter[2];
    PetscScalar       *uL, *uR;
    PetscReal         FaceArea;

    ierr = VecGetArrayRead(locGrad,&grad);CHKERRQ(ierr);
    ierr = VecGetArrayRead(locGradLimiter,&gradlimiter);CHKERRQ(ierr);
    ierr = VecGetArray(F,&f);CHKERRQ(ierr);

    ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &fluxcon);CHKERRQ(ierr); // For the convection terms
    ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &fluxdiff);CHKERRQ(ierr); // For the diffusion terms
    ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &uL);CHKERRQ(ierr);
    ierr = PetscMalloc(phys->dof * sizeof(PetscScalar), &uR);CHKERRQ(ierr);// Please do not put the Malloc function into a for loop!!!!

    for (face=fStart; face<fEnd; face++) {
      fx[0] = uL; fx[1] = uR;

      ierr = DMPlexGetLabelValue(dm, "ghost", face, &ghost);CHKERRQ(ierr);
      if (ghost >= 0) continue;
      ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
      ierr = DMPlexPointLocalRead(dmFace,face,facegeom,&fg);CHKERRQ(ierr);
      for (i=0; i<2; i++) {
        PetscScalar dx[DIM];
        ierr = DMPlexPointLocalRead(dmCell,cells[i],cellgeom,&cg[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dm,cells[i],x,&cx[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dmGrad,cells[i],gradlimiter,&cgradlimiter[i]);CHKERRQ(ierr);
        ierr = DMPlexPointLocalRead(dmGrad,cells[i],grad,&cgrad[i]);CHKERRQ(ierr);
        ierr = DMPlexPointGlobalRef(dm,cells[i],f,&cf[i]);CHKERRQ(ierr);
        Waxpy2(-1,cg[i]->centroid,fg->centroid,dx);
        for (j=0; j<dof; j++) {
          fx[i][j] = cx[i][j] + Dot2(cgradlimiter[i],dx);
        }
        // fx[0] and fx[1] are the value of the variables on the left and right
        //  side of the face, respectively, that is u_L and u_R.
      }

      ierr = RiemannSolver_Rusanov(user, cgrad[0], cgrad[1], fg->centroid, cg[0]->centroid, cg[1]->centroid, fg->normal, fx[0], fx[1], fluxcon, fluxdiff);CHKERRQ(ierr);

      ierr = DMPlexComputeCellGeometryFVM(dm, face, &FaceArea, NULL, NULL);CHKERRQ(ierr);
        // Compute the face area

      for (i=0; i<phys->dof; i++) {
        if (cf[0]) cf[0][i] -= FaceArea*(fluxcon[i] + fluxdiff[i])/cg[0]->volume;
        if (cf[1]) cf[1][i] += FaceArea*(fluxcon[i] + fluxdiff[i])/cg[1]->volume;
       // The flux on the interface, for the cell[0], it is an outcome flux and for the cell[1], it is
       //   an income flux.
      }
//      ierr = PetscPrintf(PETSC_COMM_WORLD,"\n");CHKERRQ(ierr);
    }
    ierr = VecRestoreArrayRead(locGrad,&grad);CHKERRQ(ierr);
    ierr = VecRestoreArrayRead(locGradLimiter,&gradlimiter);CHKERRQ(ierr);
    ierr = VecRestoreArray(F,&f);CHKERRQ(ierr);
    ierr = PetscFree(fluxcon);CHKERRQ(ierr);
    ierr = PetscFree(fluxdiff);CHKERRQ(ierr);
    ierr = PetscFree(uL);CHKERRQ(ierr);
    ierr = PetscFree(uR);CHKERRQ(ierr);
//    VecView(F,PETSC_VIEWER_STDOUT_WORLD);
  }
  ierr = VecRestoreArrayRead(user->facegeom,&facegeom);CHKERRQ(ierr);
  ierr = VecRestoreArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
  ierr = VecRestoreArrayRead(locX,&x);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(dmGrad,&locGradLimiter);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}