예제 #1
0
파일: hessian.hpp 프로젝트: cran/RMC
Vector ADFun<Base>::Hessian(const Vector &x, size_t i)
{	size_t j;
	size_t k;
	size_t l;

	size_t n = Domain();
	size_t m = Range();

	// check Vector is Simple Vector class with Base type elements
	CheckSimpleVector<Base, Vector>();

	CppADUsageError(
		x.size() == n,
		"Hessian: length of x not equal domain dimension for f"
	); 
	CppADUsageError(
		i < m,
		"Hessian: index i is not less than range dimension for f"
	);

	// point at which we are evaluating the Hessian
	Forward(0, x);

	// define the return value
	Vector hes(n * n);

	// direction vector for calls to forward
	Vector u(n);
	for(j = 0; j < n; j++)
		u[j] = Base(0);

	// direction vector for calls to reverse
	Vector w(m);
	for(l = 0; l < m; l++)
		w[l] = Base(0);
	w[i] = Base(1);

	// location for return values from Reverse
	Vector ddw(n * 2);

	// loop over forward direstions
	for(j = 0; j < n; j++)
	{	// evaluate partials of entire function w.r.t. j-th coordinate
		u[j] = Base(1);
		Forward(1, u);
		u[j] = Base(0);

		// evaluate derivative of partial corresponding to F_i
		ddw = Reverse(2, w);

		// return desired components
		for(k = 0; k < n; k++)
			hes[k * n + j] = ddw[k * 2 + 1];
	}
		
	return hes;
}
예제 #2
0
파일: hessian.hpp 프로젝트: barak/CppAD-1
Vector ADFun<Base>::Hessian(const Vector &x, const Vector &w)
{	size_t j;
	size_t k;

	size_t n = Domain();

	// check Vector is Simple Vector class with Base type elements
	CheckSimpleVector<Base, Vector>();

	CPPAD_ASSERT_KNOWN(
		size_t(x.size()) == n,
		"Hessian: length of x not equal domain dimension for f"
	);
	CPPAD_ASSERT_KNOWN(
		size_t(w.size()) == Range(),
		"Hessian: length of w not equal range dimension for f"
	);

	// point at which we are evaluating the Hessian
	Forward(0, x);

	// define the return value
	Vector hes(n * n);

	// direction vector for calls to forward
	Vector u(n);
	for(j = 0; j < n; j++)
		u[j] = Base(0);


	// location for return values from Reverse
	Vector ddw(n * 2);

	// loop over forward directions
	for(j = 0; j < n; j++)
	{	// evaluate partials of entire function w.r.t. j-th coordinate
		u[j] = Base(1);
		Forward(1, u);
		u[j] = Base(0);

		// evaluate derivative of partial corresponding to F_i
		ddw = Reverse(2, w);

		// return desired components
		for(k = 0; k < n; k++)
			hes[k * n + j] = ddw[k * 2 + 1];
	}

	return hes;
}
예제 #3
0
int lo_nahraj_materialy_out(EDIT_MATERIAL ** p_mat, int max_mat,
  EDIT_TEXT * p_text, int max_text, FFILE f, int save)
{
  EDIT_MATERIAL *p_tmp;
  int i = 0;
  int m = 0, 
      nm;

  while (1) {
    if (save) {
      if ((m = lo_najdi_prazdny_material(p_mat, max_mat)) == K_CHYBA)
        chyba("Malo materialu!");
    }
    p_tmp = lo_nahraj_material_out(f, p_text, max_text, save);
    if (!p_tmp) {
      break;
    }
    if (save) {
      if ((nm = lo_najdi_material(p_mat, max_mat, p_tmp->jmeno)) == K_CHYBA)
        p_mat[m] = p_tmp;
      else {
        /* Nahravany material byl nalezen na pozici nm
         */
        if (hlasit_kolize) {
          ddw("Kolize materialu - krizi se materialy %d \"%s\" a %d \"%s\"!",
            m, p_tmp->jmeno, nm, p_mat[nm]->jmeno);
          //p_mat[nm] - puvodni material
          //doe_cti_jmeno(NULL,p_mat[nm]->jmeno);
          //}
        }
        zrus_material(&p_tmp);
      }
    }
    i++;
  }
  return (i);
}
예제 #4
0
Drawing PieDraw::GetDrawing() {
	DrawingDraw ddw(3*GetSize());
	PaintPie(ddw, 3);
	return ddw;
}
예제 #5
0
void BenderQuad(
	const BAvector   &x     , 
	const BAvector   &y     , 
	Fun               fun   , 
	BAvector         &g     ,
	BAvector         &gx    ,
	BAvector         &gxx   )
{	// determine the base type
	typedef typename BAvector::value_type Base;

	// check that BAvector is a SimpleVector class
	CheckSimpleVector<Base, BAvector>();

	// declare the ADvector type
	typedef CPPAD_TESTVECTOR(AD<Base>) ADvector;

	// size of the x and y spaces
	size_t n = size_t(x.size());
	size_t m = size_t(y.size());

	// check the size of gx and gxx
	CPPAD_ASSERT_KNOWN(
		g.size() == 1,
		"BenderQuad: size of the vector g is not equal to 1"
	);
	CPPAD_ASSERT_KNOWN(
		size_t(gx.size()) == n,
		"BenderQuad: size of the vector gx is not equal to n"
	);
	CPPAD_ASSERT_KNOWN(
		size_t(gxx.size()) == n * n,
		"BenderQuad: size of the vector gxx is not equal to n * n"
	);

	// some temporary indices
	size_t i, j;

	// variable versions x
	ADvector vx(n);
	for(j = 0; j < n; j++)
		vx[j] = x[j];
	
	// declare the independent variables
	Independent(vx);

	// evaluate h = H(x, y) 
	ADvector h(m);
	h = fun.h(vx, y);

	// evaluate dy (x) = Newton step as a function of x through h only
	ADvector dy(m);
	dy = fun.dy(x, y, h);

	// variable version of y
	ADvector vy(m);
	for(j = 0; j < m; j++)
		vy[j] = y[j] + dy[j];

	// evaluate G~ (x) = F [ x , y + dy(x) ] 
	ADvector gtilde(1);
	gtilde = fun.f(vx, vy);

	// AD function object that corresponds to G~ (x)
	// We will make heavy use of this tape, so optimize it
	ADFun<Base> Gtilde;
	Gtilde.Dependent(vx, gtilde); 
	Gtilde.optimize();

	// value of G(x)
	g = Gtilde.Forward(0, x);

	// initial forward direction vector as zero
	BAvector dx(n);
	for(j = 0; j < n; j++)
		dx[j] = Base(0);

	// weight, first and second order derivative values
	BAvector dg(1), w(1), ddw(2 * n);
	w[0] = 1.;


	// Jacobian and Hessian of G(x) is equal Jacobian and Hessian of Gtilde
	for(j = 0; j < n; j++)
	{	// compute partials in x[j] direction
		dx[j] = Base(1);
		dg    = Gtilde.Forward(1, dx);
		gx[j] = dg[0];

		// restore the dx vector to zero
		dx[j] = Base(0);

		// compute second partials w.r.t x[j] and x[l]  for l = 1, n
		ddw = Gtilde.Reverse(2, w);
		for(i = 0; i < n; i++)
			gxx[ i * n + j ] = ddw[ i * 2 + 1 ];
	}

	return;
}
예제 #6
0
파일: rev_two.hpp 프로젝트: barak/CppAD-1
VectorBase ADFun<Base>::RevTwo(
    const VectorBase   &x,
    const VectorSize_t &i,
    const VectorSize_t &j)
{   size_t i1;
    size_t j1;
    size_t k;
    size_t l;

    size_t n = Domain();
    size_t m = Range();
    size_t p = i.size();

    // check VectorBase is Simple Vector class with Base elements
    CheckSimpleVector<Base, VectorBase>();

    // check VectorSize_t is Simple Vector class with size_t elements
    CheckSimpleVector<size_t, VectorSize_t>();

    CPPAD_ASSERT_KNOWN(
        x.size() == n,
        "RevTwo: Length of x not equal domain dimension for f."
    );
    CPPAD_ASSERT_KNOWN(
        i.size() == j.size(),
        "RevTwo: Lenght of the i and j vectors are not equal."
    );
    // point at which we are evaluating the second partials
    Forward(0, x);

    // dimension the return value
    VectorBase ddw(n * p);

    // direction vector in argument space
    VectorBase dx(n);
    for(j1 = 0; j1 < n; j1++)
        dx[j1] = Base(0);

    // direction vector in range space
    VectorBase w(m);
    for(i1 = 0; i1 < m; i1++)
        w[i1] = Base(0);

    // place to hold the results of a reverse calculation
    VectorBase r(n * 2);

    // check the indices in i and j
    for(l = 0; l < p; l++)
    {   i1 = i[l];
        j1 = j[l];
        CPPAD_ASSERT_KNOWN(
            i1 < m,
            "RevTwo: an eleemnt of i not less than range dimension for f."
        );
        CPPAD_ASSERT_KNOWN(
            j1 < n,
            "RevTwo: an element of j not less than domain dimension for f."
        );
    }

    // loop over all forward directions
    for(j1 = 0; j1 < n; j1++)
    {   // first order forward mode calculation done
        bool first_done = false;
        for(l = 0; l < p; l++) if( j[l] == j1 )
            {   if( ! first_done )
                {   first_done = true;

                    // first order forward mode in j1 direction
                    dx[j1] = Base(1);
                    Forward(1, dx);
                    dx[j1] = Base(0);
                }
                // execute a reverse in this component direction
                i1    = i[l];
                w[i1] = Base(1);
                r     = Reverse(2, w);
                w[i1] = Base(0);

                // place the reverse result in return value
                for(k = 0; k < n; k++)
                    ddw[k * p + l] = r[k * 2 + 1];
            }
    }
    return ddw;
}
예제 #7
0
int lo_nahraj_objekty_out(EDIT_MATERIAL ** p_mat, int max_mat, FFILE f,
  EDIT_KONTEJNER * p_kont, int mat)
{
  char string[50];
  int i, j, k;
  word r1;

  for (i = 0; i < p_kont->max_objektu; i++) {
    if (!lo_je_tag(f, "JMOB"))
      break;
    else
      str_read(string, f);

    if (!lo_je_tag(f, "TMSH"))
      continue;                 //je to objekt ale neni to mes

    if ((p_kont->p_obj[i] = lo_nacti_vec_FILE_out(f)) == NULL)
      chyba("Nacitani objektu...");

    strcpy(p_kont->p_obj[i]->jmeno, string);

    for (k = 0; k < 2; k++) {
      //nactenej jeden objekt - pricist jeste material
      if (!lo_je_tag(f, "FMAT")) {
        break;                  // neni material - na dalsi objekt
      }
      else
        str_read(string, f);

      if (mat) {
        if (!k) {
          if (!strcasecmp(string, S_NIC)) {
            p_kont->p_obj[i]->material = 0;
            p_kont->p_obj[i]->m1flag = 0;
            p_kont->p_obj[i]->m2flag = 0;
            p_kont->p_obj[i]->oflag = 0;
          }
          else {
            for (j = 0; j < max_mat; j++) {
              if (p_mat[j] && !strcasecmp(string, p_mat[j]->jmeno)) {
                p_kont->p_obj[i]->material = (word) j;
                p_kont->p_obj[i]->m1flag = p_mat[j]->flag;
                p_kont->p_obj[i]->m2flag = p_mat[j]->flag2;
                break;
              }
            }
            if (j == max_mat) {
              ddw("Neznamy material '%s' v objektu '%s'", string,
                p_kont->p_obj[i]->jmeno);
              p_kont->p_obj[i]->material = 0;
              p_kont->p_obj[i]->m1flag = 0;
              p_kont->p_obj[i]->m2flag = 0;
            }
          }
        }
      }
      else {
        p_kont->p_obj[i]->material = 0;
        p_kont->p_obj[i]->m1flag = 0;
        p_kont->p_obj[i]->m2flag = 0;
      }
    }
    if (lo_je_tag(f, "RECT")) { // sou tam recy
      ffread(&r1, sizeof(word), 1, f);
      ffread(&r1, sizeof(word), 1, f);
    }
  }
  return (i);
}
예제 #8
0
파일: sph.c 프로젝트: huahbo/navierstokes
int main(){
  px = malloc(sizeof(float)*n);
  py = malloc(sizeof(float)*n);
  vx = malloc(sizeof(float)*n);
  vy = malloc(sizeof(float)*n);
  mass = malloc(sizeof(float)*n);
  density = malloc(sizeof(float)*n);
  pressure = malloc(sizeof(float)*n);
  fx = malloc(sizeof(float)*n);
  fy = malloc(sizeof(float)*n);


  // initialize SDL
  SDL_Surface *screen;
  SDL_Event event;
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
  if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE))){
    SDL_Quit();
    return 1;
  }

  // initialize
  srand(time(NULL));
  for(i=0;i<n;i++){
    px[i]=0.4*(i % (int)sqrt(n) / sqrt(n))+0.3+randf()/100;
    py[i]=0.4*(float)i/(float)n+0.05+randf()/100;
    vx[i]=0;
    vy[i]=0;
    mass[i]=0.2;
    density[i]=0;
    pressure[i]=0;
  }

  for(iter=0;iter<maxiter;iter++){

    // compute density
    #pragma omp parallel for private(j) schedule(static) num_threads(4)
    for(i=0; i<n; i++){
      density[i] = 0;
      for(j=0; j<n; j++){
        if(dist(i,j)<=h){
          density[i] += mass[j]*w(px[i]-px[j], py[i]-py[j]);
        }
      }
      pressure[i] = k*(density[i]-density0);
    }

    // compute interactions
    #pragma omp parallel for private(j) schedule(static) num_threads(4)
    for(i=0;i<n;i++){

      float fpx = 0;
      float fpy = 0;
      float fvx = 0;
      float fvy = 0;
      for(j=0;j<n;j++){
        // check for kernel proximity
        if(dist(i,j)<=h){
          struct Point p = dw(px[i]-px[j],py[i]-py[j]);
          fpx -= mass[j]*(pressure[i]+pressure[j])/(2*density[j])*p.x;
          fpy -= mass[j]*(pressure[i]+pressure[j])/(2*density[j])*p.y;

          fvx -= my*mass[j]*(vx[j]-vx[i])/density[j]*ddw(px[i]-px[j],py[i]-py[j]);
          fvy -= my*mass[j]*(vy[j]-vy[i])/density[j]*ddw(px[i]-px[j],py[i]-py[j]);
        }
      }

      fx[i]=fpx+fvx;
      fy[i]=fpy+fvy+g;

      if(px[i] < xmin){ // outside WEST
        fy[i] -= signum(fy[i])*min(f*abs(fx[i]),abs(fy[i]));
        fx[i] = (xmin-px[i]-vx[i]*dt)/(dt*dt);
      }
      else if(xmax < px[i]){ // outside EAST
        fy[i] -= signum(fy[i])*min(f*abs(fx[i]),abs(fy[i]));
        fx[i] = (xmax-px[i]-vx[i]*dt)/(dt*dt);
      }
      else if(py[i] < ymin){ // outside SOUTH
        fx[i] -= signum(fx[i])*min(f*abs(fy[i]),abs(fx[i]));
        fy[i] = (ymin-py[i]-vy[i]*dt)/(dt*dt);
      }
      else if(xmax < px[i]){ // outside NORTH
        fx[i] -= signum(fx[i])*min(f*abs(fy[i]),abs(fx[i]));
        fy[i] = (ymax-py[i]-vy[i]*dt)/(dt*dt);
      }

      // do euler steps
      vx[i] += fx[i]*dt;
      vy[i] += fy[i]*dt;

      px[i] += vx[i]*dt;
      py[i] += vy[i]*dt;
    }
    DrawScreen(screen);
    while(SDL_PollEvent(&event)) 
    {      
      switch (event.type) 
      {
        case SDL_QUIT:
          SDL_Quit();
          return 0;
	        break;
        case SDL_KEYDOWN:
          SDL_Quit();
          return 0;
          break;
      }
    }
    // Uncomment to save screenshots of every 10th frame.
    //if(iter%10==0){
    //  char *a = malloc(sizeof(char)*100);
    //  sprintf(a, "tmp/FILE%05d.BMP", frame++);
    //  SDL_SaveBMP(screen, a);
    //}
  }

  SDL_Quit();
  return 0;
}
/* **************************************************************************
  // vybrane face objektu oriznu a vyrobim jako extra objekt
  // Vyrizne oznacene facy pomoci bodu jako extra objekt daneho kontejneru
*/
void oe_prikaz_vyrizni_face(K_EDITOR * p_cnf, int crop)
{
  EDIT_OBJEKT *p_obj;
  EDIT_OBJEKT *p_new;
  int *p_vertex;
  int k, o, v, i, f, vnum, fnum;
  int *p_face, *p_delete, del = 0;

  if (!p_cnf->groupnum) {
    ddw
      ("Musi byt oznaceny face, ktery se maji vyriznout ! Pouzij klavesy F a CTRL+F na oznaceni facu pomoci bodu !");
    return;
  }

  k = p_cnf->p_group[0].k;      //pouziju nulu
  o = p_cnf->p_group[0].o;
  p_obj = p_cnf->p_kont[k]->p_obj[o];

  p_face = _alloca(sizeof(int) * p_obj->facenum);
  memset(p_face, K_CHYBA, sizeof(sizeof(int)) * p_obj->facenum);

  if (crop) {
    p_delete = _alloca(sizeof(int) * p_obj->facenum / 3);
    memset(p_delete, 0, sizeof(sizeof(int)) * p_obj->facenum / 3);
  }

  p_vertex = _alloca(sizeof(int) * p_obj->vertexnum);
  memset(p_vertex, K_CHYBA, sizeof(sizeof(int)) * p_obj->vertexnum);

  // vyberu facy ktere jsou skutecne pouzity
  for (i = 0; i < p_cnf->groupnum; i++) {
    if (p_cnf->p_group[i].k == k && p_cnf->p_group[i].o == o) {
      v = p_cnf->p_group[i].v;
      for (f = 0; f < p_obj->facenum; f++) {
        if (p_obj->p_face[f] == v)
          p_face[f] = v;
      }
    }
  }

  // skopiruju face
  fnum = 0;
  for (f = 0, i = 0; f < p_obj->facenum; f += 3) {
    if (p_face[f] != K_CHYBA && p_face[f + 1] != K_CHYBA
      && p_face[f + 2] != K_CHYBA) {
      if (crop) {
        p_delete[del++] = f;
      }
      assert(i <= f);
      if (i != f) {
        p_face[i] = p_face[f];  // setridit
        p_face[i + 1] = p_face[f + 1];
        p_face[i + 2] = p_face[f + 2];
      }
      i += 3;
    }
  }

  assert(i > 0);
  fnum = i;
  vnum = 0;
  // najdu pocet jedinecnych bodu
  for (f = 0; f < i; f++) {
    for (v = 0; v < vnum; v++) {
      if (p_vertex[v] == p_face[f]) {
        p_face[f] = v;
        goto dale;
      }
    }
    p_vertex[vnum] = p_face[f];
    p_face[f] = vnum++;
  dale:;
  }

  if (crop) {
    for (f = del - 1; f >= 0; f--) {
      smaz_face_bez_vertexu(p_obj->p_face, p_obj->facenum, p_delete[f]);
    }
    smaz_divoke_vertexy_objektu_dir(p_obj);
  }

  if (!(p_new = vyrob_objekt_komplet(vnum, fnum))) {
    assert(p_new);
    chyba("Pamet");
  }

  for (v = 0; v < vnum; v++) {
    p_new->p_vertex[v] = p_obj->p_vertex[p_vertex[v]];
  }

  for (f = 0; f < fnum; f++) {
    p_new->p_face[f] = p_face[f];
  }

  p_new->material = p_obj->material;
  p_new->m1flag = p_obj->m1flag;
  p_new->m2flag = p_obj->m2flag;
  p_new->oflag = p_obj->oflag;
  strcpy(p_new->jmeno, "Vyrez");

  if ((o =
      lo_najdi_volny_kontejner(p_cnf->p_kont, p_cnf->max_kont)) == K_CHYBA) {
    ddw("Neni volny kontejner !");
    return;
  }

  p_cnf->p_kont[o] = vyrob_kontejner();
  p_cnf->p_kont[o]->p_obj[0] = p_new;
  strcpy(p_cnf->p_kont[o]->jmeno, "Vyrez");
  p_cnf->p_kont[o]->m1flag = p_cnf->p_kont[k]->m1flag;
  p_cnf->p_kont[o]->m2flag = p_cnf->p_kont[k]->m2flag;
  p_cnf->p_kont[o]->kflag = p_cnf->p_kont[k]->kflag;
  p_cnf->p_kont[o]->world = p_cnf->p_kont[k]->world;
  updatuj_kontejner_statistika(p_cnf->p_kont[o], TRUE);
  if (crop)
    updatuj_kontejner_statistika(p_cnf->p_kont[k], TRUE);
}