Esempio n. 1
0
void kepler_elements_to_state_t(
    const struct kepler_elements *elements,
    double t,
    double *pos,
    double *vel) {
    // mean anomaly
    double M = kepler_orbit_mean_anomaly_at_time(elements, t);

    // eccentric anomaly
    double E = kepler_anomaly_mean_to_eccentric(elements->eccentricity, M);

    // position and velocity in orbital plane
    double pos2d[3], vel2d[3];
    kepler_elements_to_state_E(elements, E, pos2d, vel2d);

    // position and velocity in 3d
    double matrix[9];
    kepler_orbit_matrix(elements, matrix);
    matrix_vector_product(matrix, pos2d, pos);
    matrix_vector_product(matrix, vel2d, vel);
}
Esempio n. 2
0
void cudaSolver() {

	int n = 100;
	int m = 50;
	int pMin = 10;
	int pMax = 40;
	double lambda = 1;
	double C = 1;

	int NMax = 1000;

	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	generateRandomProblem(&A_h, &R_Idx_h, &C_Idx_h, &C_Count_h, n, m, pMin,
			pMax - pMin, &nnz);

	double* L;
	double* Li;
	computeLipsitzConstantsForSparseRegression(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz);
	//		printMatrixA(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, nnz);
	//	print_double_array(&L[0], n);
	double b[m];
	double xOpt[n];
	double x[n];
	for (j = 0; j < n; j++) {
		xOpt[j] = 2 * ((double) rand() / RAND_MAX) - 1;
	}
	// set b = A*x
	matrix_vector_product(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, xOpt, b,
			1);
	//	print_double_array(&xOpt[0], n);
	//	print_double_array(&b[0], m);
	double value = 0;
	for (i = 0; i < n; i++)
		x[i] = 0;
	value = NRCDM_SR(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, b, x, lambda,
			Li, NMax * 100, value, 0);
	for (i = 0; i < n; i++)
		x[i] = 0;
	value = NRCDM_SR(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, b, x, lambda,
			Li, NMax, value, 1);

}
Esempio n. 3
0
/*solution saved in b */
void conjugate_gradient(sparse_matrix A, double *b, int size) {
	double *x, *r, *p, *Ap, *aux, rnew, rold, alfa;
	int i;

    x = (double*) malloc(size*sizeof(double));
    r = (double*) malloc(size*sizeof(double));
    p = (double*) malloc(size*sizeof(double));
    Ap = (double*) malloc(size*sizeof(double));
    aux = (double*) malloc(size*sizeof(double));

	for (i = 0; i < size; i ++) {
		x[i] = 0;
		r[i] = b[i];
		p[i] = b[i];
	}

	rold = inner_product(r, r, size);
    /* result of operations from all void functions used here are stored in the last argument */
	while (1) {
		matrix_vector_product(A, p, Ap);
		alfa = rold / inner_product(p, Ap, size); /*step length*/
		vector_scalar_product(p, alfa, size, aux); 
		vector_sum(x, aux, size, x);
		vector_scalar_product(Ap, alfa, size, aux);
		vector_subtraction(r, aux, size, r);
		rnew = inner_product(r, r, size);
		if (sqrt(rnew) < E)
                        break;
                vector_scalar_product(p, rnew / rold, size, p);
                vector_sum(p, r, size, p);
		rold = rnew;
	}

	for (i = 0; i < size; i ++) {
		b[i] = x[i];
	}

	free(x);
	free(r);
	free(p);
	free(Ap);
	free(aux);
}
Esempio n. 4
0
void Deriv::intercell_flux_sweep(const double *U, const double *P,
                                 const double *F, const double *A,
                                 double *Fiph, int dim)
{
  const int Nx = Mara->domain->get_N(dim);
  const int Ng = Mara->domain->get_Ng();

  // Local memory requirements for WENO flux calculation
  // ---------------------------------------------------------------------------
  double *lam = new double[NQ];       // Characteristic eigenvalues
  double *Piph = new double[NQ];      // Primitive variables at i+1/2 (averaged)
  double *Uiph = new double[NQ];      // Conserved " " (taken from prim)
  double *Liph = new double[NQ*NQ];   // Left eigenvectors at i+1/2
  double *Riph = new double[NQ*NQ];   // Right eigenvectors at i+1/2
  double *fp = new double[6*NQ];      // Characteristic split fluxes on local stencil
  double *fm = new double[6*NQ];      // " " Left going
  double *Fp = new double[NQ];        // Component-wise split fluxes on local cell
  double *Fm = new double[NQ];        // " " Left going

  double *f = new double[NQ];         // WENO characteristic flux
  double *fpT = new double[NQ*6];     // Transposed split flux to [wave, zone]
  double *fmT = new double[NQ*6];     // " " Left going
  // ---------------------------------------------------------------------------

  for (int i=Ng-1; i<Nx+Ng; ++i) {

    if (fluxsplit_method == FLUXSPLIT_MARQUINA) {

      // Hard-codes NQ=5 for now
      // -----------------------
      double laml[5], lamr[5];
      double Ll[5][5], Rl[5][5];
      double Lr[5][5], Rr[5][5];
      double ul[6][5], ur[6][5];
      double fl[6][5], fr[6][5];
      double fweno_p[5], fweno_m[5];
      double Fweno_p[5], Fweno_m[5];
      double Pl[5], Pr[5];
      double Ul[5], Ur[5];

      for (int q=0; q<NQ; ++q) {
	const int m = i*NQ + q;
	double v[6] = { P[m-2*NQ], P[m-NQ], P[m], P[m+NQ], P[m+2*NQ], P[m+3*NQ] };
	
	switch (GodunovOperator::reconstruct_method) {
	case RECONSTRUCT_PCM:
	  Pl[q] = v[2];
	  Pr[q] = v[3];
	  break;
	case RECONSTRUCT_PLM:
	  Pl[q] = reconstruct(&v[2], PLM_C2R);
	  Pr[q] = reconstruct(&v[3], PLM_C2L);
	  break;
	case RECONSTRUCT_WENO5:
	  Pl[q] = reconstruct(&v[2], WENO5_FV_C2R);
	  Pr[q] = reconstruct(&v[3], WENO5_FV_C2L);
	  break;
	}
      }
      Mara->fluid->PrimToCons(Pl, Ul);
      Mara->fluid->PrimToCons(Pr, Ur);
      Mara->fluid->Eigensystem(Ul, Pl, Ll[0], Rl[0], laml, dim);
      Mara->fluid->Eigensystem(Ur, Pr, Lr[0], Rr[0], lamr, dim);


      for (int j=0; j<6; ++j) {
        matrix_vector_product(Ll[0], &U[(i+j-2)*NQ], ul[j], NQ, NQ);
        matrix_vector_product(Lr[0], &U[(i+j-2)*NQ], ur[j], NQ, NQ);
        matrix_vector_product(Ll[0], &F[(i+j-2)*NQ], fl[j], NQ, NQ);
        matrix_vector_product(Lr[0], &F[(i+j-2)*NQ], fr[j], NQ, NQ);
      }

      for (int q=0; q<NQ; ++q) {
        if (laml[q] > 0.0 && lamr[q] > 0.0) {
          // No sign change, right-going waves only: set fm to zero and fp to f
          for (int j=0; j<6; ++j) {
            fp[j*NQ + q] = fl[j][q];
            fm[j*NQ + q] = 0.0;
          }
        }
        else if (laml[q] < 0.0 && lamr[q] < 0.0) {
          // No sign change, left-going waves only: set fp to zero and fm to f
          for (int j=0; j<6; ++j) {
            fp[j*NQ + q] = 0.0;
            fm[j*NQ + q] = fr[j][q];
          }
        }
        else {
          // There is a sign change in the speed of this characteristic field
          const double a = fabs(laml[q]) > fabs(lamr[q]) ? laml[q] : lamr[q];
          for (int j=0; j<6; ++j) {
            fp[j*NQ + q] = 0.5*(fl[j][q] + fabs(a)*ul[j][q]);
            fm[j*NQ + q] = 0.5*(fr[j][q] - fabs(a)*ur[j][q]);
          }
        }
      }

      for (int q=0; q<NQ; ++q) {
        for (int j=0; j<6; ++j) {
          fpT[q*6 + j] = fp[j*NQ + q];
          fmT[q*6 + j] = fm[j*NQ + q];
        }
      }

      for (int q=0; q<NQ; ++q) {
        fweno_p[q] = reconstruct(fpT+q*6+2, WENO5_FD_C2R);
        fweno_m[q] = reconstruct(fmT+q*6+3, WENO5_FD_C2L);
      }

      matrix_vector_product(Rl[0], fweno_p, Fweno_p, NQ, NQ);
      matrix_vector_product(Rr[0], fweno_m, Fweno_m, NQ, NQ);

      for (int q=0; q<NQ; ++q) {
	Fiph[i*NQ + q] = Fweno_p[q] + Fweno_m[q];
      }
    }

    else if (fluxsplit_method == FLUXSPLIT_LOCAL_LAX_FRIEDRICHS) {

      for (int q=0; q<NQ; ++q) {
        Piph[q] = 0.5*(P[i*NQ + q] + P[(i+1)*NQ + q]);
      }

      Mara->fluid->PrimToCons(Piph, Uiph);
      Mara->fluid->Eigensystem(Uiph, Piph, Liph, Riph, lam, dim);

      // Select the maximum wavespeed on the local stencil
      // -------------------------------------------------------------------------
      const double ml = *std::max_element(A+i-2, A+i+4);

      for (int j=0; j<6; ++j) {
        for (int q=0; q<NQ; ++q) {

          // Local Lax-Friedrichs flux splitting
          // ---------------------------------------------------------------------
          const int m = (i+j-2)*NQ + q;
          Fp[q] = 0.5*(F[m] + ml*U[m]);
          Fm[q] = 0.5*(F[m] - ml*U[m]);
        }
        matrix_vector_product(Liph, Fp, fp+j*NQ, NQ, NQ);
        matrix_vector_product(Liph, Fm, fm+j*NQ, NQ, NQ);
      }

      for (int q=0; q<NQ; ++q) {
        for (int j=0; j<6; ++j) {
          fpT[q*6 + j] = fp[j*NQ + q];
          fmT[q*6 + j] = fm[j*NQ + q];
        }
      }

      for (int q=0; q<NQ; ++q) {
        f[q] =
          reconstruct(fpT+q*6+2, WENO5_FD_C2R) +
          reconstruct(fmT+q*6+3, WENO5_FD_C2L);
      }

      matrix_vector_product(Riph, f, Fiph+i*NQ, NQ, NQ);
    }
  }

  // Clean up local memory requirements for WENO flux calculation
  // ---------------------------------------------------------------------------
  delete [] lam;
  delete [] Piph;
  delete [] Uiph;
  delete [] Liph;
  delete [] Riph;
  delete [] fp;
  delete [] fm;
  delete [] Fp;
  delete [] Fm;

  delete [] f;
  delete [] fpT;
  delete [] fmT;
  // ---------------------------------------------------------------------------
}
static void ac_update_curve_polygon(struct approximation_curve* ac)
{
  Grille_flottant* m;
  Grille_flottant* mt;
  Grille_flottant* mmt;

  Table_flottant* p;
  Table_flottant* a;
  Table_flottant* mp;

  Table_flottant* params;

  // Allocation de la table des points de contrôle de la courbe d'approximation
  if (ac->curve_polygon.nb != (ac->degree + 1))
  {
    free(ac->curve_polygon.table);

    ac->curve_polygon.nb = ac->degree + 1;
    ALLOUER(ac->curve_polygon.table, ac->curve_polygon.nb);
  }

  // Paramétrisation de la courbe d'approximation
  if (ac->use_uniform_parameterization)
    params = ac_uniform_parameterization(&ac->points);
  else
    params = ac_non_uniform_parameterization(&ac->points);

  // Construction de la matrice de Bernstein, de sa transposée et de leur produit
  m = matrix_create(ac->degree + 1, ac->points.nb);
  ac_bernstein_matrix(m, params);

  mt = matrix_transpose(m);
  mmt = matrix_product(m, mt);

  // Résolution des 3 systèmes d'équations (composantes x, y et z) des points de contrôle recherchés
  p = malloc_table_flottant(ac->points.nb);
  a = malloc_table_flottant(ac->degree + 1);
  mp = malloc_table_flottant(m->nb_lignes);

  // X
  get_triplets_x_values(&ac->points, p);
  matrix_vector_product(m, p, mp);
  if (resolution_systeme_lineaire(mmt, mp, a))
    EXIT;
  set_triplets_x_values(&ac->curve_polygon, a);

  // Y
  get_triplets_y_values(&ac->points, p);
  matrix_vector_product(m, p, mp);
  if (resolution_systeme_lineaire(mmt, mp, a))
    EXIT;
  set_triplets_y_values(&ac->curve_polygon, a);

  // Z
  get_triplets_z_values(&ac->points, p);
  matrix_vector_product(m, p, mp);
  if (resolution_systeme_lineaire(mmt, mp, a))
    EXIT;
  set_triplets_z_values(&ac->curve_polygon, a);

  // Libération des ressources
  matrix_delete(m);
  matrix_delete(mt);
  matrix_delete(mmt);

  free_table_flottant(a);
  free_table_flottant(p);
  free_table_flottant(mp);
  free_table_flottant(params);
}