示例#1
0
文件: misc.cpp 项目: jfellus/agem
void generate_data(const char* genstr) {
	X.create(D,n);

	int k = 1;
	sscanf(genstr, "%d",&k);

	Matrix weights(k,1); weights.rand(); weights /= weights.sum();

	Matrix* cov = new Matrix[k];
	Matrix* mu = new Matrix[k];
	for(int kk=0; kk<k; kk++) {
		cov[kk].create(D,D);
		mu[kk].create(D,1);
		rand_covariance(cov[kk], .5);
		randvec(mu[kk],D,-1,1);
	}

	for(int i=0; i<n; i++) {
		float f = frand();
		int kk;	for(kk=0; kk<k && f>0; kk++) f-= weights[kk];
		kk--;
		randvec_gaussian(X.get_row(i), mu[kk], cov[kk]);
	}

	DBG("Generated " << n << " points (D=" << D << ") from " << k << " normal laws");

	distribute_data();
}
void Trilinos_Util_GenerateVbrProblem(int nx, int ny, int npoints, int * xoff, int * yoff, 
																			int nsizes, int * sizes, int nrhs,
																			const Epetra_Comm  &comm, 
																			Epetra_BlockMap *& map, 
																			Epetra_VbrMatrix *& A, 
																			Epetra_MultiVector *& x, 
																			Epetra_MultiVector *& b,
																			Epetra_MultiVector *&xexact) {

	int i, j;

	// Number of global equations is nx*ny.  These will be distributed in a linear fashion
	int numGlobalEquations = nx*ny;
  Epetra_Map ptMap(numGlobalEquations, 0, comm); // Create map with equal distribution of equations.

	int numMyElements = ptMap.NumMyElements();

	Epetra_IntVector elementSizes(ptMap); // This vector will have the list of element sizes
	for (i=0; i<numMyElements; i++) 
		elementSizes[i] = sizes[ptMap.GID64(i)%nsizes]; // cycle through sizes array

	map = new Epetra_BlockMap(-1, numMyElements, ptMap.MyGlobalElements(), elementSizes.Values(),
														ptMap.IndexBase(), ptMap.Comm());

  
  A = new Epetra_VbrMatrix(Copy, *map, 0); // Construct matrix

	int * indices = new int[npoints];
//	double * values = new double[npoints];

//	double dnpoints = (double) npoints;

	// This section of code creates a vector of random values that will be used to create
	// light-weight dense matrices to pass into the VbrMatrix construction process.

	int maxElementSize = 0;
	for (i=0; i< nsizes; i++) maxElementSize = EPETRA_MAX(maxElementSize, sizes[i]);

	Epetra_LocalMap lmap(maxElementSize*maxElementSize, ptMap.IndexBase(), ptMap.Comm());
	Epetra_Vector randvec(lmap);
	randvec.Random();
	randvec.Scale(-1.0); // Make value negative


	for (i=0; i<numMyElements; i++) {
		int rowID = map->GID(i);
		int numIndices = 0;
		int rowDim = sizes[rowID%nsizes];
		for (j=0; j<npoints; j++) {
			int colID = rowID + xoff[j] + nx*yoff[j]; // Compute column ID based on stencil offsets
			if (colID>-1 && colID<numGlobalEquations)
				indices[numIndices++] = colID;
		}
			
		A->BeginInsertGlobalValues(rowID, numIndices, indices);
		
		for (j=0; j < numIndices; j++) {
			int colDim = sizes[indices[j]%nsizes];
			A->SubmitBlockEntry(&(randvec[0]), rowDim, rowDim, colDim);
		}
		A->EndSubmitEntries();
	}

	delete [] indices;

  A->FillComplete();

	// Compute the InvRowSums of the matrix rows
	Epetra_Vector invRowSums(A->RowMap());
	Epetra_Vector rowSums(A->RowMap());
	A->InvRowSums(invRowSums);
	rowSums.Reciprocal(invRowSums);

	// Jam the row sum values into the diagonal of the Vbr matrix (to make it diag dominant)
	int numBlockDiagonalEntries;
	int * rowColDims;
	int * diagoffsets = map->FirstPointInElementList();
	A->BeginExtractBlockDiagonalView(numBlockDiagonalEntries, rowColDims);
	for (i=0; i< numBlockDiagonalEntries; i++) {
		double * diagVals;
		int diagLDA;
		A->ExtractBlockDiagonalEntryView(diagVals, diagLDA);
		int rowDim = map->ElementSize(i);
		for (j=0; j<rowDim; j++) diagVals[j+j*diagLDA] = rowSums[diagoffsets[i]+j];
	}

	if (nrhs<=1) {  
		x = new Epetra_Vector(*map);
		b = new Epetra_Vector(*map);
		xexact = new Epetra_Vector(*map);
	}
	else {
		x = new Epetra_MultiVector(*map, nrhs);
		b = new Epetra_MultiVector(*map, nrhs);
		xexact = new Epetra_MultiVector(*map, nrhs);
	}

	xexact->Random(); // Fill xexact with random values

  A->Multiply(false, *xexact, *b);

  return;
}
示例#3
0
void GenerateVbrProblem(int numNodesX, int numNodesY, int numProcsX, int numProcsY, int numPoints,
			int * xoff, int * yoff,
			int nsizes, int * sizes, int nrhs,
			const Epetra_Comm  &comm, bool verbose, bool summary,
			Epetra_BlockMap *& map,
			Epetra_VbrMatrix *& A,
			Epetra_MultiVector *& b,
			Epetra_MultiVector *& bt,
			Epetra_MultiVector *&xexact, bool StaticProfile, bool MakeLocalOnly) {

  int i;

  // Determine my global IDs
  long long * myGlobalElements;
  GenerateMyGlobalElements(numNodesX, numNodesY, numProcsX, numProcsY, comm.MyPID(), myGlobalElements);

  int numMyElements = numNodesX*numNodesY;

  Epetra_Map ptMap((long long)-1, numMyElements, myGlobalElements, 0, comm); // Create map with 2D block partitioning.
  delete [] myGlobalElements;

  Epetra_IntVector elementSizes(ptMap); // This vector will have the list of element sizes
  for (i=0; i<numMyElements; i++)
    elementSizes[i] = sizes[ptMap.GID64(i)%nsizes]; // cycle through sizes array

  map = new Epetra_BlockMap((long long)-1, numMyElements, ptMap.MyGlobalElements64(), elementSizes.Values(),
			    ptMap.IndexBase64(), ptMap.Comm());

  int profile = 0; if (StaticProfile) profile = numPoints;

// FIXME: Won't compile until Epetra_VbrMatrix is modified.
#if 0
  int j;
  long long numGlobalEquations = ptMap.NumGlobalElements64();

  if (MakeLocalOnly)
    A = new Epetra_VbrMatrix(Copy, *map, *map, profile); // Construct matrix rowmap=colmap
  else
    A = new Epetra_VbrMatrix(Copy, *map, profile); // Construct matrix

  long long * indices = new long long[numPoints];

  // This section of code creates a vector of random values that will be used to create
  // light-weight dense matrices to pass into the VbrMatrix construction process.

  int maxElementSize = 0;
  for (i=0; i< nsizes; i++) maxElementSize = EPETRA_MAX(maxElementSize, sizes[i]);

  Epetra_LocalMap lmap((long long)maxElementSize*maxElementSize, ptMap.IndexBase(), ptMap.Comm());
  Epetra_Vector randvec(lmap);
  randvec.Random();
  randvec.Scale(-1.0); // Make value negative
  int nx = numNodesX*numProcsX;


  for (i=0; i<numMyElements; i++) {
    long long rowID = map->GID64(i);
    int numIndices = 0;
    int rowDim = sizes[rowID%nsizes];
    for (j=0; j<numPoints; j++) {
      long long colID = rowID + xoff[j] + nx*yoff[j]; // Compute column ID based on stencil offsets
      if (colID>-1 && colID<numGlobalEquations)
	indices[numIndices++] = colID;
    }
			
    A->BeginInsertGlobalValues(rowID, numIndices, indices);
		
    for (j=0; j < numIndices; j++) {
      int colDim = sizes[indices[j]%nsizes];
      A->SubmitBlockEntry(&(randvec[0]), rowDim, rowDim, colDim);
    }
    A->EndSubmitEntries();
  }

  delete [] indices;

  A->FillComplete();

  // Compute the InvRowSums of the matrix rows
  Epetra_Vector invRowSums(A->RowMap());
  Epetra_Vector rowSums(A->RowMap());
  A->InvRowSums(invRowSums);
  rowSums.Reciprocal(invRowSums);

  // Jam the row sum values into the diagonal of the Vbr matrix (to make it diag dominant)
  int numBlockDiagonalEntries;
  int * rowColDims;
  int * diagoffsets = map->FirstPointInElementList();
  A->BeginExtractBlockDiagonalView(numBlockDiagonalEntries, rowColDims);
  for (i=0; i< numBlockDiagonalEntries; i++) {
    double * diagVals;
    int diagLDA;
    A->ExtractBlockDiagonalEntryView(diagVals, diagLDA);
    int rowDim = map->ElementSize(i);
    for (j=0; j<rowDim; j++) diagVals[j+j*diagLDA] = rowSums[diagoffsets[i]+j];
  }

  if (nrhs<=1) {
    b = new Epetra_Vector(*map);
    bt = new Epetra_Vector(*map);
    xexact = new Epetra_Vector(*map);
  }
  else {
    b = new Epetra_MultiVector(*map, nrhs);
    bt = new Epetra_MultiVector(*map, nrhs);
    xexact = new Epetra_MultiVector(*map, nrhs);
  }

  xexact->Random(); // Fill xexact with random values

  A->Multiply(false, *xexact, *b);
  A->Multiply(true, *xexact, *bt);

#endif // EPETRA_NO_32BIT_GLOBAL_INDICES

  return;
}
示例#4
0
文件: init.c 项目: sirianray/lblc
void init(double *f, double *Q0)
{
		  int i, j, k, ii, jj, ip, jp, kp, im, jm, km, index;
		  int points = Nx*Ny*Nz;

		  //For hybrid initial condition
		  double theta0, theta, thetalocal, lambda1, lambda2, thetalocali, nlocal[3]={0};
		  theta0 = ntop[0]*nbot[0]+ntop[1]*nbot[1]+ntop[2]*nbot[2];
		  theta0 = theta0/sqrt(ntop[0]*ntop[0]+ntop[1]*ntop[1]+ntop[2]*ntop[2]);        
		  theta0 = theta0/sqrt(nbot[0]*nbot[0]+nbot[1]*nbot[1]+nbot[2]*nbot[2]);
		  theta0 = acos(theta0);

		  if(newrun==1){

					 for(i=0;i<(points);i++) Rho[i] = rho;

					 for (k=0; k<Nz; k++) {
								for (j=0; j<Ny; j++) {
										  for (i=0; i<Nx; i++) {

													 index = i + Nx*j + Ny*Nx*k;

													 if(cav_on && cav_flag[index]==-1){
																//for(ii=0;ii<3;ii++) u[3*index+ii] = 0;
																u[3*index+0] = 0;
																u[3*index+1] = uy_top*((double)k+0.5)/((double)Nz);
																u[3*index+2] = 0;
													 }


													 k_eng += 0.5*( u[3*index+0]*+u[3*index+0] + u[3*index+1]*+u[3*index+1] + u[3*index+2]*+u[3*index+2])*Rho[index];

													 if(Q_on || (cav_on && cav_flag[index]>=0)){

																if(rand_init){

																		  double randx, randy, randz;
																		  randx = randvec();
																		  randy = randvec();
																		  randz = randvec();

																		  double mag = randx*randx + randy*randy + randz*randz;
																		  mag = sqrt(mag);

																		  randx /= mag;
																		  randy /= mag;
																		  randz /= mag;

																		  double Srand = 0.5*S;

																		  Q0[5*index+0] = Srand*(randx*randx - third);
																		  Q0[5*index+1] = Srand*(randx*randy);
																		  Q0[5*index+2] = Srand*(randx*randz);
																		  Q0[5*index+3] = Srand*(randy*randy - third);
																		  Q0[5*index+4] = Srand*(randy*randz);

																}

																else {
																		  if (theta0<1e-2 && theta0>-1e-2) {
																					 lambda1 = 0.5;
																					 lambda2 = 0.5;
																		  } else {
																					 theta = theta0 * ((double)k+0.5)/((double)Nz);
																					 lambda1=(cos(theta)-cos(theta0)*cos(theta0-theta));
																					 lambda2=(cos(theta0-theta)-cos(theta)*cos(theta0));
																		  }
																		  nlocal[0] = lambda1 * nbot[0] + lambda2 * ntop[0];
																		  nlocal[1] = lambda1 * nbot[1] + lambda2 * ntop[1];
																		  nlocal[2] = lambda1 * nbot[2] + lambda2 * ntop[2];
																		  thetalocal = nlocal[0]*nlocal[0] + nlocal[1]*nlocal[1] + nlocal[2]*nlocal[2];
																		  thetalocali= 1.0/thetalocal;

																		  Q0[5*index+0] =  S*(nlocal[0]*nlocal[0]*thetalocali - third);
																		  Q0[5*index+1] =  S*(nlocal[0]*nlocal[1]*thetalocali);
																		  Q0[5*index+2] =  S*(nlocal[0]*nlocal[2]*thetalocali);
																		  Q0[5*index+3] =  S*(nlocal[1]*nlocal[1]*thetalocali - third);
																		  Q0[5*index+4] =  S*(nlocal[1]*nlocal[2]*thetalocali);
																}

													 }
										  }
								}
					 }

					 //For finite anchoring
					 for(i=0;i<Nx;i++){
								for(j=0;j<Ny;j++){

										  index = i + j*Nx;

										  if(!rand_init){

													 Qsurfbot[5*index+0] =  S*(nbot[0]*nbot[0] - third);
													 Qsurfbot[5*index+1] =  S*(nbot[0]*nbot[1]);
													 Qsurfbot[5*index+2] =  S*(nbot[0]*nbot[2]);
													 Qsurfbot[5*index+3] =  S*(nbot[1]*nbot[1] - third);
													 Qsurfbot[5*index+4] =  S*(nbot[1]*nbot[2]);

													 Qsurftop[5*index+0] =  S*(ntop[0]*ntop[0] - third);
													 Qsurftop[5*index+1] =  S*(ntop[0]*ntop[1]);
													 Qsurftop[5*index+2] =  S*(ntop[0]*ntop[2]);
													 Qsurftop[5*index+3] =  S*(ntop[1]*ntop[1] - third);
													 Qsurftop[5*index+4] =  S*(ntop[1]*ntop[2]);
										  }

										  if(rand_init){

													 double randx, randy, randz;
													 randx = randvec();
													 randy = randvec();
													 randz = randvec();

													 double mag = randx*randx + randy*randy + randz*randz;
													 mag = sqrt(mag);

													 randx /= mag;
													 randy /= mag;
													 randz /= mag;

													 double Srand = 0.5*S;

													 Qsurfbot[5*index+0] = Srand*(randx*randx - third);
													 Qsurfbot[5*index+1] = Srand*(randx*randy);
													 Qsurfbot[5*index+2] = Srand*(randx*randz);
													 Qsurfbot[5*index+3] = Srand*(randy*randy - third);
													 Qsurfbot[5*index+4] = Srand*(randy*randz);

													 randx = randvec();
													 randy = randvec();
													 randz = randvec();

													 mag = sqrt(mag);

													 randx /= mag;
													 randy /= mag;
													 randz /= mag;

													 Qsurftop[5*index+0] = Srand*(randx*randx - third);
													 Qsurftop[5*index+1] = Srand*(randx*randy);
													 Qsurftop[5*index+2] = Srand*(randx*randz);
													 Qsurftop[5*index+3] = Srand*(randy*randy - third);
													 Qsurftop[5*index+4] = Srand*(randy*randz);

										  }
								}
					 }
		  }
		  else  read_restart(Q0,Rho,u);

		  //Preferred anchoring
		  Qo_bottom[0] =  S*(nbot[0]*nbot[0] - third);
		  Qo_bottom[1] =  S*(nbot[0]*nbot[1]);
		  Qo_bottom[2] =  S*(nbot[0]*nbot[2]);
		  Qo_bottom[3] =  S*(nbot[1]*nbot[1] - third);
		  Qo_bottom[4] =  S*(nbot[1]*nbot[2]);

		  Qo_top[0] =  S*(ntop[0]*ntop[0] - third);
		  Qo_top[1] =  S*(ntop[0]*ntop[1]);
		  Qo_top[2] =  S*(ntop[0]*ntop[2]);
		  Qo_top[3] =  S*(ntop[1]*ntop[1] - third);
		  Qo_top[4] =  S*(ntop[1]*ntop[2]);

		  //populate Q for sides of cavity
		  Qtop[0] = S*(ncavtop[0]*ncavtop[0] - third);
		  Qtop[1] = S*(ncavtop[0]*ncavtop[1]);
		  Qtop[2] = S*(ncavtop[0]*ncavtop[2]);
		  Qtop[3] = S*(ncavtop[1]*ncavtop[1] - third);
		  Qtop[4] = S*(ncavtop[1]*ncavtop[2]);

		  Qsides[0] = S*(ncavsides[0]*ncavsides[0] - third);
		  Qsides[1] = S*(ncavsides[0]*ncavsides[1]);
		  Qsides[2] = S*(ncavsides[0]*ncavsides[2]);
		  Qsides[3] = S*(ncavsides[1]*ncavsides[1] - third);
		  Qsides[4] = S*(ncavsides[1]*ncavsides[2]);

		  cal_dQ(Q0);

		 // if(!newrun){
		 //  		 cal_stress(Q0);
		 //  		 cal_dtau(Q0);
		 // }

		  cal_sigma();
		  cal_fequ(f,u);
		  cal_W();

}
示例#5
0
// Generate a random point on the field
static vec2 rand_field(void) {
	return randvec(field_size-2.0*field_edge)+vec2(field_edge,field_edge);
}
示例#6
0
文件: init.c 项目: sirianray/lblc
void init()
{
	int i, j, k, ii, ip, id;
	time_t t;
	double nx, ny, nz, q[6], sita0, sita, lambda1, lambda2;
	
	if (newrun_on!=0) {
		if (myid==0) {
			sita0 = n_top[0]*n_bot[0]+n_top[1]*n_bot[1]+n_top[2]*n_bot[2];
			sita0 = sita0/sqrt(n_top[0]*n_top[0]+n_top[1]*n_top[1]+n_top[2]*n_top[2]);        
			sita0 = sita0/sqrt(n_bot[0]*n_bot[0]+n_bot[1]*n_bot[1]+n_bot[2]*n_bot[2]);
			sita0 = acos(sita0);
			
			if (rand_init==0) {				// random random seed
				srand((unsigned)time(&t));
			} else if (rand_init>0) {		// random seed from input
				srand((unsigned)rand_seed);
			}
			
			if ( (wall_on!=0 || npar>0 || patch_on!=0) && Q_on!=0) {
				for (i=0; i<nsurf; i++) {
					if (rand_init>=0) {
						nx = randvec();
						ny = randvec();
						nz = randvec();
	                                        ntoq(nx, ny, nz, &q[0], &q[1], &q[2], &q[3], &q[4], &q[5]);
						for (j=0; j<5; j++) Qsurf[i*5+j] = q[j];
					} else {
						for (j=0; j<5; j++) Qsurf[i*5+j] = surf[i*10+5+j];
					}
				}
			}
			
			for (k=0; k<Nz; k++) {
				if (sita0<1e-2 && sita0>-1e-2) {
					lambda1 = 0.5;
					lambda2 = 0.5;
				} else {
					sita = sita0 * ((double)k+0.5)/((double)Nz);
					lambda1=(cos(sita)-cos(sita0)*cos(sita0-sita));
					lambda2=(cos(sita0-sita)-cos(sita)*cos(sita0));
				}
				nx = lambda1 * n_bot[0] + lambda2 * n_top[0];
				ny = lambda1 * n_bot[1] + lambda2 * n_top[1];
				nz = lambda1 * n_bot[2] + lambda2 * n_top[2];
				for (j=0; j<Ny; j++) {
					for (i=0; i<Nx; i++) {
						id = (i + (j+k*Ny)*Nx);
						if (rand_init>=0) {
							nx = randvec();
							ny = randvec();
							nz = randvec();
						}
						ntoq(nx, ny, nz, &q[0], &q[1], &q[2], &q[3], &q[4], &q[5]);
						for (ii=0; ii<5; ii++) Q[id*5+ii] = q[ii];
						if (q[0]+q[3]+q[5]>1e-15 || q[0]+q[3]+q[5]<-1e-15 ) {
							printf("initial bulk Q not right\n");
							exit(-1);
						}
					}
				}
			}

			if (flow_on!=0) {
				for (k=0; k<Nz; k++) {
					for (j=0; j<Ny; j++) {
						for (i=0; i<Nx; i++) {
							id = i + (j+k*Ny)*Nx;
							Rho[id]  = rho;
							u[id*3]  = 0;
//							u[id*3+1]= uy_bottom + (uy_top-uy_bottom)*((double)k+0.5)/(double)Nz;
							u[id*3+1]= 0;
							u[id*3+2]= 0;
						}
					}
				}			
			}
		}		
	} else {
		if (myid==0) read_restart();
	}

        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Win_fence(0, winq);
        MPI_Win_fence(0, winr);
        MPI_Win_fence(0, winu);
	if ( (wall_on!=0 || npar>0) && Q_on!=0 ) MPI_Win_fence(0, winQsurf);
}
示例#7
0
	vgl_vector_3d<double> GetRandomVec(void)
	{
		vgl_vector_3d<double> randvec(drand48(), drand48(), drand48());
		normalize(randvec);
		return randvec;
	}