Exemple #1
0
void ida_decode_free(struct ida_decode *ida)
{
	if(!ida)
		return;
	if(ida->key)
		free_matrix(ida->key);
	free_matrix2(ida->output);
	free_matrix2(ida->input);
	free(ida);
}
Exemple #2
0
/*
 * Free an HLL model.
 */
void hll_free(hll_t *hll)
{
  if (hll->Q)
    free_matrix2(hll->Q);
  if (hll->X)
    free_matrix2(hll->X);
  if (hll->x0)
    free(hll->x0);
  if (hll->S0)
    free(hll->S0);

  hll_free_cache(hll);
}
Exemple #3
0
void test_fit(int argc, char *argv[])
{
  if (argc < 6) {
    printf("usage: %s <n> <s1> <s2> <s3> <s4>\n", argv[0]);
    return;
  }

  int i, n = atoi(argv[1]);
  double s1 = atof(argv[2]);
  double s2 = atof(argv[3]);
  double s3 = atof(argv[4]);
  double s4 = atof(argv[5]);

  double dx, **X = new_matrix2(n, 4);
  for (i = 0; i < n; i++) {
    X[i][0] = normrand(0, s1);
    X[i][1] = normrand(0, s2);
    X[i][2] = normrand(0, s3);
    X[i][3] = normrand(0, s4);
    dx = norm(X[i], 4);
    mult(X[i], X[i], 1/dx, 4);
  }

  bingham_t B;
  bingham_fit(&B, X, n, 4);

  free_matrix2(X);
}
Exemple #4
0
struct ida_decode *ida_decode_create(int width, int m)
{
	struct ida_decode *ret;
	if((width != 1) && (width != 2))
		return NULL;
	ret = malloc(sizeof(struct ida_decode));
	if(!ret)
		return NULL;

	memset(ret, 0, sizeof(struct ida_decode));
	ret->input = alloc_empty_matrix(width, m, m);
	if(!ret->input)
	{
		free(ret);
		return NULL;
	}
	ret->output = alloc_empty_matrix(width, m, m);
	if(!ret->output)
	{
		free_matrix2(ret->input);
		free(ret);
		return NULL;
	}
	ret->width = width;
	ret->m = m;
	ret->size = width * m * m;
	return ret;
}
Exemple #5
0
/*
 * Free an HLL cache.
 */
void hll_free_cache(hll_t *hll)
{
  if (hll->cache.n > 0) {
    if (hll->cache.Q_kdtree)
      kdtree_free(hll->cache.Q_kdtree);
    if (hll->cache.Q)
      free_matrix2(hll->cache.Q);
    if (hll->cache.X)
      free_matrix2(hll->cache.X);
    int i;
    if (hll->cache.S) {
      for (i = 0; i < hll->cache.n; i++)
	if (hll->cache.S[i])
	  free_matrix2(hll->cache.S[i]);
      free(hll->cache.S);
    }
    hll->cache.n = 0;
  }
}
Exemple #6
0
struct ida_encode *ida_encode_create(int width, int n, int m)
{
	struct ida_encode *ret;
	if(n < m)
		return NULL;
	if((width != 1) && (width != 2))
		return NULL;
	galois_init_table(width*8);
	ret = malloc(sizeof(struct ida_encode));
	if(!ret)
		return NULL;

	memset(ret, 0, sizeof(struct ida_encode));
	ret->key = generate_cauchy_matrix(width, n, m);
	if(!ret->key)
	{
		free(ret);
		return NULL;
	}
	ret->input = alloc_empty_matrix(width, m, m);
	if(!ret->input)
	{
		free_matrix(ret->key);
		free(ret);
		return NULL;
	}
	ret->output = alloc_matrix(width, n, m);
	if(!ret->output)
	{
		free_matrix2(ret->input);
		free_matrix(ret->key);
		free(ret);
		return NULL;
	}
	ret->width = width;
	ret->n = n;
	ret->m = m;
	ret->size = width * n * m;
	return ret;
}
Exemple #7
0
/*
 * Sample n Gaussians (with means X and covariances S) from HLL at sample points Q.
 */
void hll_sample(double **X, double ***S, double **Q, hll_t *hll, int n)
{
  int i, j;
  if (hll->cache.n > 0) {
    for (i = 0; i < n; i++) {
      j = kdtree_NN(hll->cache.Q_kdtree, Q[i]);
      memcpy(X[i], hll->cache.X[j], hll->dx * sizeof(double));
      matrix_copy(S[i], hll->cache.S[j], hll->dx, hll->dx);
    }
    return;
  }

  double r = hll->r;
  int nx = hll->dx;
  int nq = hll->dq;

  double **WS = new_matrix2(nx, nx);

  for (i = 0; i < n; i++) {

    //printf("q = [%.2f %.2f %.2f %.2f]\n", Q[0][0], Q[0][1], Q[0][2], Q[0][3]);

    //for (j = 0; j < hll->n; j++)
    //  printf("hll->Q[%d] = [%.2f %.2f %.2f %.2f]\n", j, hll->Q[j][0], hll->Q[j][1], hll->Q[j][2], hll->Q[j][3]);

    // compute weights
    double dq, qdot;
    double w[hll->n];
    //printf("w = [");
    for (j = 0; j < hll->n; j++) {
      qdot = fabs(dot(Q[i], hll->Q[j], nq));
      dq = acos(MIN(qdot, 1.0));
      w[j] = exp(-(dq/r)*(dq/r));
      //printf("%.2f ", w[j]);
    }
    //printf("]\n");

    // threshold weights
    double wmax = arr_max(w, hll->n);
    double wthresh = wmax/50;  //dbug: make this a parameter?
    for (j = 0; j < hll->n; j++)
      if (w[j] < wthresh)
	w[j] = 0;
    double wtot = hll->w0 + sum(w, hll->n);

    // compute posterior mean
    mult(X[i], hll->x0, hll->w0, nx);  // X[i] = w0*x0
    for (j = 0; j < hll->n; j++) {
      if (w[j] > 0) {
	double wx[nx];
	mult(wx, hll->X[j], w[j], nx);
	add(X[i], X[i], wx, nx);       // X[i] += wx
      }
    }
    mult(X[i], X[i], 1/wtot, nx);  // X[i] /= wtot

    // compute posterior covariance matrix
    mult(S[i][0], hll->S0[0], hll->w0, nx*nx);  // S[i] = w0*S0

    for (j = 0; j < hll->n; j++) {
      if (w[j] > 0) {
	double wdx[nx];
	sub(wdx, hll->X[j], X[i], nx);
	mult(wdx, wdx, w[j], nx);
	outer_prod(WS, wdx, wdx, nx, nx);    // WS = wdx'*wdx
	matrix_add(S[i], S[i], WS, nx, nx);  // S[i] += WS
      }
    }

    mult(S[i][0], S[i][0], 1/wtot, nx*nx);  // S[i] /= wtot
  }

  free_matrix2(WS);
}
Exemple #8
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[])
{
	if (nrhs != 1){
		mexErrMsgTxt("Error only 1 input expected");
	}
	
	if (mxGetN(prhs[0]) != 4){
		mexErrMsgTxt("Input data must be an Nx4 array");
	}
	
	
	int i, j, n, d;
	double F;
	double *input, *V, *Z, *raw;
	double **X;
	bingham_t B;
		
	n = mxGetM(prhs[0]);
    d = mxGetN(prhs[0]);
    /*
    mexPrintf("n=%d,   d=%d\n",n,d); 
    */
    input = mxGetPr(prhs[0]);
/*
	mexPrintf("%f %f %f %f\n",input[0],input[0+n],input[0+2*n],input[0+3*n]);
*/

    X=new_matrix2(n,d);
    
    for (i=0;i<n;i++){
		for (j=0;j<d;j++){
			X[i][j]=input[i+j*n];
		}
	}
/*	mexPrintf("%f %f %f %f\n",X[0][0],X[0][1],X[0][2],X[0][3]); */
		
	
	bingham_fit(&B, X, n, d);
	
/*	mexPrintf("%f\n",B.F);*/ 
		
	free_matrix2(X);
	if (nlhs > 0) {
		plhs[0] = mxCreateDoubleMatrix(d, d-1, mxREAL);
		V = mxGetPr(plhs[0]);
		memcpy(V, B.V[0], d*(d-1)*sizeof(double));
	}

	if (nlhs > 1) {
		plhs[1] = mxCreateDoubleMatrix(d-1, 1, mxREAL);
		Z = mxGetPr(plhs[1]);
		memcpy(Z, B.Z, (d-1)*sizeof(double));
	}
	
	if (nlhs > 2) {
		plhs[2] = mxCreateDoubleScalar(B.F);

	}



	
}
Exemple #9
0
int main(int argn, char** args){
	if (argn !=2 ) {
		printf("When running the simulation, please give a valid geometry file name!\n");
		return 1;
	}


	//set the geometry file
	char *filename = NULL;
	filename = args[1];

	//initialize variables
	double t = 0; /*time start*/
	int it, n = 0; /*iteration and time step counter*/
	double res; /*residual for SOR*/
	/*arrays*/
	double ***U, ***V, ***W, ***P;
	double ***RS, ***F, ***G, ***H;

	int ***Flag; //additional data structure for arbitrary geometry
	int ***S; //additional data structure for arbitrary geometry
	int matrix_output = 0;
	/*those to be read in from the input file*/
	double Re, UI, VI, WI, PI, GX, GY, GZ, t_end, xlength, ylength, zlength, dt, dx, dy, dz, alpha, omg, tau, eps, dt_value;
	int  imax, jmax, kmax, itermax;

	//double presLeft, presRight, presDelta; //for pressure stuff  ...TODO: not allowed for now
	int wl, wr, wt, wb, wf, wh;
	char problemGeometry[200];
	//in case of a given inflow or wall velocity  TODO: will we have this? needs to be a vector?
	double velIN;
	double velMW[3]; // the moving wall velocity is a vector

	struct particleline *Partlines = (struct particleline *)malloc((unsigned)(1 * sizeof(struct particleline)));
	//char szFileName[80];

	//read the parameters, using problem.dat, including wl, wr, wt, wb
	read_parameters(filename, &Re, &UI, &VI, &WI, &PI, &GX, &GY, &GZ, &t_end, &xlength, &ylength, &zlength, &dt, &dx, &dy, &dz, &imax,
			&jmax, &kmax, &alpha, &omg, &tau, &itermax, &eps, &dt_value, &wl, &wr,  &wf, &wh, &wt, &wb, problemGeometry, &velIN, &velMW[0]); //&presLeft, &presRight, &presDelta, &vel);


	//printf("d: %f, %f, %f\n", dx,dy,dz);


	//int pics = dt_value/dt; //just a helping variable for outputing vtk
	double last_output_t = -dt_value;
	//double every = t_end/10; //helping variable. Can be used for displaying info every tenth of the progress during simulation

	//allocate memory, including Flag
	U = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1);
	V = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1);
	W = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1);
	P = matrix2(0, imax+1, 0, jmax+1, 0, kmax+1);
	RS = matrix2(1, imax, 1, jmax, 1, kmax);

	F = matrix2(0, imax, 1, jmax, 1, kmax);
	G = matrix2(1, imax, 0, jmax, 1, kmax);
	H = matrix2(1, imax, 1, jmax, 0, kmax);


	Flag = imatrix2(0, imax+1, 0, jmax+1, 0, kmax+1); // or Flag = imatrix(1, imax, 1, jmax);

	S    = imatrix2(0, imax+1, 0, jmax+1, 0, kmax+1);
	//S = Flag -> adjust C_x Flags in helper.h

	//initialisation, including **Flag


	init_flag(problemGeometry, imax, jmax, kmax, Flag, wl, wr, wf, wh, wt, wb);  //presDelta, Flag);

	init_particles(Flag,dx,dy,dz,imax,jmax,kmax,3,Partlines);

	printf("!!!!!!!!%d\n",binMatch(B_NO,B_N));


	init_uvwp(UI, VI, WI, PI, Flag,imax, jmax, kmax, U, V, W, P, problemGeometry);

	write_particles("particles_init",0, 1, Partlines);

	write_imatrix2("Flag_start.txt",t,Flag, 0, imax, 1, jmax, 1, kmax);
	//write_flag_imatrix("Flag.txt",t,Flag, 0, imax+1, 0, jmax+1, 0, kmax+1);
	write_vtkFile(filename, -1, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P,Flag);

	//write_vtkFile("init", n, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P);
	//going through all time steps
	/*
	write_matrix2("P_start.txt",t,P, 0, imax+1, 0, jmax+1, 0, kmax+1);
	write_matrix2("U_start.txt",t,U, 0, imax+1, 0, jmax+1, 0, kmax+1);
	write_matrix2("V_start.txt",t,V, 0, imax+1, 0, jmax+1, 0, kmax+1);
	write_matrix2("W_start.txt",t,W, 0, imax+1, 0, jmax+1, 0, kmax+1);
	*/
	//setting bound.values
	boundaryvalues(imax, jmax, kmax, U, V, W, P, F, G, H, problemGeometry, Flag, velIN, velMW); //including P, wl, wr, wt, wb, F, G, problem
	//    printf("calc bc \n");


	while(t < t_end){
		/*if(t - every >= 0){
      	  	  printf("Calculating time %f ... \n", t);
      	  	  every += t;
    	}*/

		//adaptive time stepping
		calculate_dt(Re, tau, &dt, dx, dy, dz, imax, jmax, kmax, U, V, W);
		//    printf("calc dt \n");

		/*
		mark_cells(Flag,dx,dy,dz,imax,jmax,kmax,1,Partlines);


		set_uvwp_surface(U,V,W,P,Flag,dx,dy,dz,imax,jmax,kmax,GX,GY,GZ,dt,Re);
*/

		//computing F, G, H and right hand side of pressue eq.
		calculate_fgh(Re, GX, GY, GZ, alpha, dt, dx, dy, dz, imax, jmax, kmax, U, V, W, F, G, H, Flag);
		//    printf("calc fgh \n");

		calculate_rs(dt, dx, dy, dz, imax, jmax, kmax, F, G, H, RS,Flag);
		//    printf("calc rs \n");

		//iteration counter
		it = 0;

		//write_matrix2("RS.txt",t,RS, 1, imax, 1, jmax, 1, kmax);
		//write_matrix2("F.txt",t,F, 0, imax, 1, jmax, 1, kmax);
		//write_matrix2("G.txt",t,G, 1, imax, 0, jmax, 1, kmax);
		//write_matrix2("H.txt",t,H, 1, imax, 1, jmax, 0, kmax);
		do{
			// sprintf( szFileName, "P_%d.txt",it );
			//write_matrix2(szFileName,1000+t,P, 0, imax+1, 0, jmax+1, 0, kmax+1);
			//perform SOR iteration, at same time set bound.values for P and new residual value
			sor(omg, dx, dy, dz, imax, jmax, kmax, P, RS, &res, Flag); //, presLeft, presRight);

			it++;
		}while(it<itermax && res>eps);

		/*if (it == itermax) {
	printf("Warning: sor while loop exits because it reaches the itermax. res = %f, time =%f\n", res, t);
		} */

		//write_matrix2("P.txt",t,P, 0, imax+1, 0, jmax+1, 0, kmax+1);
		//calculate U, V and W of this time step
		calculate_uvw(dt, dx, dy, dz, imax, jmax, kmax, U, V, W, F, G, H, P, Flag);
		//write_matrix2("U.txt",t,U, 0, imax+1, 0, jmax+1, 0, kmax+1);
		//write_matrix2("V.txt",t,V, 0, imax+1, 0, jmax+1, 0, kmax+1);
		//write_matrix2("W.txt",t,W, 0, imax+1, 0, jmax+1, 0, kmax+1);

		//    printf("calc uvw \n");


			//sprintf( szFileName, "simulation/%s.%i_debug.vtk", szProblem, timeStepNumber );


		//setting bound.values
		boundaryvalues(imax, jmax, kmax, U, V, W, P, F, G, H, problemGeometry, Flag, velIN, velMW); //including P, wl, wr, wt, wb, F, G, problem

		/*
		set_uvwp_surface(U,V,W,P,Flag,dx,dy,dz,imax,jmax,kmax,GX,GY,GZ,dt,Re);

		printf("advance_particles!\n");
		advance_particles(dx,dy, dz,imax,jmax,kmax, dt,U,V,W,1,Partlines);
*/

		//indent time and number of time steps
		printf("timer\n");
		n++;
		t += dt;

		//output of pics for animation
		if ( t-last_output_t  >= dt_value ){  //n%pics==0 ){
			printf("output\n!");









			write_particles(filename,n, 1, Partlines);
			write_vtkFile(filename, n, xlength, ylength, zlength, imax, jmax, kmax, dx, dy, dz, U, V, W, P,Flag);

			printf("output vtk (%d)\n",n);
			last_output_t = t;
			matrix_output++;
		}
		printf("timestep: %f   - next output: %f   (dt: %f) \n",t,dt_value- (t-last_output_t),dt);
	}

	//output of U, V, P at the end for visualization
	//write_vtkFile("DrivenCavity", n, xlength, ylength, imax, jmax, dx, dy, U, V, P);
	//free memory
	free_matrix2(U, 0, imax+1, 0, jmax+1, 0, kmax+1);
	free_matrix2(V, 0, imax+1, 0, jmax+1, 0, kmax+1);
	free_matrix2(W, 0, imax+1, 0, jmax+1, 0, kmax+1);
	free_matrix2(P, 0, imax+1, 0, jmax+1, 0, kmax+1);
	free_matrix2(RS, 1, imax, 1, jmax, 1, kmax);


	free_matrix2(F, 0, imax, 1, jmax, 1, kmax);
	free_matrix2(G, 1, imax, 0, jmax, 1, kmax);
	free_matrix2(H, 1, imax, 1, jmax,  0, kmax);


	free_imatrix2(Flag, 0, imax+1, 0, jmax+1, 0, kmax+1);
	free_imatrix2(S, 0, imax+1, 0, jmax+1, 0, kmax+1);
	//printf("\n-\n");
	return -1;
}