Example #1
0
int main(void)
{
	int i,check;
	float *x,*f;

	x=vector(1,N);
	f=vector(1,N);
	x[1]=2.0;
	x[2]=0.5;
	newt(x,N,&check,funcv);
	funcv(N,x,f);
	if (check) printf("Convergence problems.\n");
	printf("%7s %3s %12s\n","Index","x","f");
	for (i=1;i<=N;i++) printf("%5d %12.6f %12.6f\n",i,x[i],f[i]);
	free_vector(f,1,N);
	free_vector(x,1,N);
	return 0;
}
Example #2
0
int main(void)  /* Program sphfpt */
{
	void newt(float x[], int n, int *check,
		void (*vecfunc)(int, float [], float []));
	void shootf(int n, float v[], float f[]);
	int check,i;
	float q1,*v1,*v2,*v;

	v=vector(1,NTOT);
	v1=v;
	v2 = &v[N2];
	nvar=NTOT;
	nn2=N2;
	dx=DXX;
	for (;;) {
		printf("input m,n,c-squared\n");
		if (scanf("%d %d %f",&m,&n,&c2) == EOF) break;
		if (n < m || m < 0) continue;
		gmma=1.0;
		q1=n;
		for (i=1;i<=m;i++) gmma *= -0.5*(n+i)*(q1--/i);
		v1[1]=n*(n+1)-m*(m+1)+c2/2.0;
		v2[2]=v1[1];
		v2[1]=gmma*(1.0-(v2[2]-c2)*dx/(2*(m+1)));
		x1 = -1.0+dx;
		x2=1.0-dx;
		xf=0.0;
		newt(v,NTOT,&check,shootf);
		if (check) {
			printf("shootf failed; bad initial guess\n");
		} else {
			printf("\tmu(m,n)\n");
			printf("%12.6f\n",v[1]);
		}
	}
	free_vector(v,1,NTOT);
	return 0;
}
Example #3
0
void Classtable::on_pushButton_4_clicked()
{
    cleartable();

    QSqlQuery query;
    QString subname,teacher,tname,classroom,type;
    int week,time,id;
   ui->label_2->setText(ui->comboBox->currentText());
        query.exec("select name,teacher,week,ttime,id,classroom,type from subject where tname='"+ui->comboBox->currentText()+"'");



        while(query.next())
        {


            qDebug() << query.value(0).toString() << query.value(1).toString()<<query.value(2).toInt()<<query.value(3).toInt();
            subname=query.value(0).toString();
            teacher=query.value(1).toString();
            week=query.value(2).toInt();
            time=query.value(3).toInt();
            tname=ui->comboBox->currentText();
            id=query.value(4).toInt();
            classroom=query.value(5).toString();
            type=query.value(6).toString();
            Table newt(id,subname,teacher,week,time,tname);
            table->append(newt);
            pos[time][week]=table->count()-1;

            ui->tableWidget->setItem(time,week,new QTableWidgetItem(subname+"("+classroom+") "+teacher));
            //  ui->label_2->setText(tname);



        }


}
Example #4
0
void PressureTable::ComputeDatabaseInputs_FromT( const double rho, const double Z, const double Zv, const double Yc, const double T,
                                           double &P,  double &Sz , double &C,
                                           double P0,  double C0) {
  // Compute normalized variance
  double eps = 1.0e-3;
  Sz = ComputeNormalizedVariance(Z, Zv);

  // Compute normalized progress variable
  Z_newton = Z;
  Zv_newton = Sz;
  Yc_newton = Yc;
  rho_newton = rho;
  T_newton = T;
  // I need to solve ( Yc(C,P) - Yc_target , rho(C,P) - rho_target) = (0 , 0)
  C_newton = C0;
  P_newton = P0/P_scale;// Pressure is rescaled
  verbose_newton = false;
  Bool check;
  VecDoub_IO sol2(1, 0.0);
  VecDoub_IO sol(2, 0.0);
  int Newton_mode;


  try {
    // Get Approximation of Yceq from highest pressure
    int nPress = GetDimension1();
    double P_max = GetCoordinate1(nPress-1);
    int nC = GetDimension4();
    double C_max = GetCoordinate4(nC-2); // Maximum C value before one
    double Yc_eq  = Lookup(P_max,Z_newton,Zv_newton,1.0,"PROG");
    double Yc_max = Lookup(P_max,Z_newton,Zv_newton,C_max,"PROG");
    // If Yc_eq is small, C is set to 1 and we look for pressure only
    if ((Yc_eq < eps)||(Yc > Yc_max)) {
      Newton_mode = 1;
      sol2[0] = P_newton;
      C_newton = 1.0;
      newt(sol2, check, usrfunc_wrapper4);
    } else {
      // usual
      Newton_mode = 2;
      sol[0] = C_newton;
      sol[1] = P_newton;
      try {newt(sol, check, usrfunc_wrapper3);}
      catch (int e) {
       cout << "relaunched with Newton_mode = 1" <<  endl;
       Newton_mode = 1;
       C_newton = sol[0];
       sol2[0] = P0/P_scale;
       newt(sol2, check, usrfunc_wrapper4);
      }
    }
  }
  catch (int e) {
    int tol_failure = 0;
    if (Newton_mode == 1) {
      VecDoub out(1, 0.0);
      out = DensityFromP(sol2);
      if (abs(out[0]) > 1.0e-5) tol_failure = 1;
    } else if (Newton_mode == 2) {
      VecDoub out(2, 0.0);
      out = YcAndDensityFromCP(sol);
     if ((abs(out[0])>1.0e-4)||(abs(out[1]))>1.0e-5) tol_failure = 1;
    }
    cout << "Newton error. Test tol = " << tol_failure << endl;
    if (tol_failure == 1) {
    cout << "Newton inputs : " << Z_newton   << " "
                               << Zv_newton  << " "
                               << Yc_newton  << " "
                               << rho_newton << " "
                               << C_newton   << " "
                               << P_newton*P_scale   << endl;
    cout << "C0 : " << C0 << " P0 : " << P0 << endl;
    cout << " Check : " << check << endl;
    if (Newton_mode == 2) {
      cout << " sol : C = " << sol[0] << " , P = " << sol[1]*P_scale << endl;
    } else if (Newton_mode == 1) {
      cout << " sol :  P = " << sol2[0]*P_scale << endl;
    }
    }
  }
  if (Newton_mode == 2) {
    C_newton = sol[0];
    P_newton = sol[1];
  } else if (Newton_mode == 1) {
    P_newton = sol2[0];
  }
  C = C_newton;
  P = P_newton*P_scale;

}
Example #5
0
void PressureTable::ComputeDatabaseInputs( const double rho, const double Z, const double Zv, const double Yc,
                                           double &P,  double &Sz , double &C,
                                           double P0,  double C0) {
  // Compute normalized variance
  double eps = 1.0e-3;
  if ((Z <= 1-eps)||(Z >= eps)) {
    Sz = Zv/(Z*(1.0-Z));
  } else {
    Sz = 0.0;
  }
  Sz = max(0.0, min(1.0, Sz));

  // Compute normalized progress variable
  Z_newton = Z;
  Zv_newton = Sz;
  Yc_newton = Yc;
  rho_newton = rho;
  // I need to solve ( Yc(C,P) - Yc_target , rho(C,P) - rho_target) = (0 , 0)
  C_newton = C0;
  P_newton = P0/P_scale;// Pressure is rescaled
  verbose_newton = false;
  Bool check;
  VecDoub_IO sol2(1, 0.0);
  VecDoub_IO sol(2, 0.0);
  int Newton_mode;

  try {
    //Newton  : newt(sol, check, usrfunc_wrapper);
    //Broyden : broydn(sol, check, usrfunc_wrapper);

    // Get Approximation of Yceq from highest pressure
    int nPress = GetDimension1();
    double P_max = GetCoordinate1(nPress-1);
    int nC = GetDimension4();
    double C_max = GetCoordinate4(nC-2); // Maximum C value before one
    double Yc_eq  = Lookup(P_max,Z_newton,Zv_newton,1.0,"PROG");
    double Yc_max = Lookup(P_max,Z_newton,Zv_newton,C_max,"PROG");
    // If Yc_eq is small, C is set to 1 and we look for pressure only
    if ((Yc_eq < eps)||(Yc > Yc_max)) {
      Newton_mode = 1;
      sol2[0] = P_newton;
      C_newton = 1.0;
      newt(sol2, check, usrfunc_wrapper2);
    } else {
      // usual
      Newton_mode = 2;
      sol[0] = C_newton;
      sol[1] = P_newton;
      try {newt(sol, check, usrfunc_wrapper);}
      catch (int e) {
       cout << "relaunched with Newton_mode = 1" <<  endl;
       Newton_mode = 1;
       C_newton = sol[0];
       sol2[0] = P0/P_scale;
       newt(sol2, check, usrfunc_wrapper2);
      }
    }
  }
  catch (int e) {
    int tol_failure = 0;
    if (Newton_mode == 1) {
      VecDoub out(1, 0.0);
      out = DensityFromP(sol2);
      if (abs(out[0]) > 1.0e-5) tol_failure = 1;
    } else if (Newton_mode == 2) {
      VecDoub out(2, 0.0);
      out = YcAndDensityFromCP(sol);
     if ((abs(out[0])>1.0e-4)||(abs(out[1]))>1.0e-5) tol_failure = 1;
    }
    cout << "Newton error. Test tol = " << tol_failure << endl;
    if (tol_failure == 1) { 
    cout << "Newton inputs : " << Z_newton   << " "
                               << Zv_newton  << " "
                               << Yc_newton  << " "
                               << rho_newton << " "
                               << C_newton   << " "
                               << P_newton*P_scale   << endl;
    cout << "C0 : " << C0 << " P0 : " << P0 << endl;
    cout << " Check : " << check << endl;
    if (Newton_mode == 2) {
      cout << " sol : C = " << sol[0] << " , P = " << sol[1]*P_scale << endl;
    } else if (Newton_mode == 1) {
      cout << " sol :  P = " << sol2[0]*P_scale << endl;
    }

    // Relaunch with verbose
    verbose_newton = true;
    try {
      Int MaxIter = 200;
      if (Newton_mode == 1) {
        sol2[0] = P_newton;
        C_newton = 1.0;
        cout << "Reinit with " <<  sol2[0] << " " << MaxIter <<  endl;
        newt(sol2, check, usrfunc_wrapper2, MaxIter);
      } else if (Newton_mode == 2) {
        // usual
        sol[0] = C_newton;
        sol[1] = P_newton;
        cout << "Reinit with " <<  sol[0] << " " << sol[1] << " " << MaxIter << endl;
        newt(sol, check, usrfunc_wrapper, MaxIter);
      } else {cerr << "Impossible value of Newton_mode: " << Newton_mode << endl;}

    }
    catch (int e2) {
      cout << "Bis Newton inputs : " << Z_newton   << " "
                                 << Zv_newton  << " "
                                 << Yc_newton  << " "
                                 << rho_newton << " "
                                 << C_newton   << " "
                                 << P_newton*P_scale   << endl;
      cout << "Bis Check : " << check << endl;
      if (Newton_mode == 2) {
        cout << "Bis sol : C = " << sol[0] << " , P = " << sol[1]*P_scale << endl;
      } else if (Newton_mode == 1) {
        cout << "Bis sol :  P = " << sol2[0]*P_scale << endl;
      }  
      throw(-1);
    }
    throw(-1);
    }
  }

  if (Newton_mode == 2) {
    C_newton = sol[0];
    P_newton = sol[1];
  } else if (Newton_mode == 1) {
    P_newton = sol2[0];
  }
  C = C_newton;
  P = P_newton*P_scale;
}