Esempio n. 1
0
 static base::Vector<1>::Type sol( const VecDim& x,
                                   const double radius, const double alpha1,
                                   const double alpha2 )
 {
     VecDim dummy;
     return (interface( x, radius, dummy ) ?
             sol1( x, radius, alpha1, alpha2 ) :
             sol2( x, radius, alpha1, alpha2 ) );
 }
Esempio n. 2
0
void sol3(double t)  //计算Bezier曲线上点的坐标  
{  
    double x=0,y=0,Ber;  
    int k;  
    for(k=0;k<n;k++)  
    {  
        Ber=sol2(n-1,k)*pow(t,k)*pow(1-t,n-1-k);  
        x+=point[k].x*Ber;  
        y+=point[k].y*Ber;  
    }  
    putpixel((int)x,(int)y,GREEN);  
}  
Esempio n. 3
0
void TestSolver::circle4() {
	const ExprSymbol& x=ExprSymbol::new_("x");
	const ExprSymbol& y=ExprSymbol::new_("y");
	const ExprSymbol& r2=ExprSymbol::new_("r2");

	SystemFactory f;
	f.add_var(x);
	f.add_var(y);
	f.add_var(r2);
	f.add_ctr(sqr(x)+sqr(y)=r2);
	f.add_ctr(sqr(x-1)+sqr(y)=r2);

	double cospi6=0.5;
	double sinpi6=::sqrt(3)/2;

	double _sol1[]={cospi6,sinpi6,1};
	double _sol2[]={cospi6,-sinpi6,1};

	Vector sol1(3,_sol1);
	Vector sol2(3,_sol2);

	System sys(f);
	RoundRobin rr(1e-3);
	CellStack stack;
	CtcHC4 hc4(sys);

	VarSet params(sys.f_ctrs,sys.args[2],false);
	Vector prec(3,1e-3);
	Solver solver(sys,hc4,rr,stack,prec,prec);
	solver.set_params(params);
	IntervalVector box(3);
	box[0]=Interval(-10,10);
	box[1]=Interval(-10,10);
	box[2]=Interval(1,1);
	solver.start(box);

	CovSolverData::BoxStatus status;

	bool res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==1);
	CPPUNIT_ASSERT(solver.get_data().solution(0).is_superset(sol1));

	res=solver.next(status);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==2);
	CPPUNIT_ASSERT(solver.get_data().solution(1).is_superset(sol2));

	res=solver.next(status);
	CPPUNIT_ASSERT(!res);
}
Esempio n. 4
0
void TestSolver::circle3() {
	const ExprSymbol& x=ExprSymbol::new_("x");
	const ExprSymbol& y=ExprSymbol::new_("y");

	SystemFactory f;
	f.add_var(x);
	f.add_var(y);
	f.add_ctr(sqr(x)+sqr(y)=1);
	f.add_ctr(sqr(x-1)+sqr(y)=1);

	double cospi6=0.5;
	double sinpi6=(sqrt(Interval(3))/2).lb();
	f.add_ctr(4*y*abs(y)<=3); // a rigorous way to impose y<=sin(pi/6).

	double _sol1[]={cospi6,sinpi6};
	double _sol2[]={cospi6,-sinpi6};

	Vector sol1(2,_sol1);
	Vector sol2(2,_sol2);
	System sys(f);
	RoundRobin rr(1e-3);
	CellStack stack;
	CtcHC4 hc4(sys);
	Vector prec(2,1e-3);
	Solver solver(sys,hc4,rr,stack,prec,prec);

	solver.start(IntervalVector(2,Interval(-10,10)));


	CovSolverData::BoxStatus status;

	bool res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::UNKNOWN);
	CPPUNIT_ASSERT(solver.get_data().nb_unknown()==1);
	CPPUNIT_ASSERT(solver.get_data().unknown(0).is_superset(sol1));

	res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==1);
	CPPUNIT_ASSERT(solver.get_data().solution(0).is_superset(sol2));

	res=solver.next(status);
	CPPUNIT_ASSERT(!res);
}
Esempio n. 5
0
int main()
{
    eoPop<DualVector> pop;

    // fixed test
    DualVector sol1(2,-1);
    DualVector sol2(2,-1);
    DualVector sol3(2,1);
    DualVector sol4(2,1);
    pop.push_back( sol1 );
    pop.push_back( sol2 );
    pop.push_back( sol3 );
    pop.push_back( sol4 );
    // on the sphere function everyone has the same fitness of 1
    if( test(pop, 0) != 0 ) {
        exit(1);
    }

    pop.erase(pop.begin(),pop.end());

    // fixed test
    sol1 = DualVector(2,0);
    sol2 = DualVector(2,0);
    sol3 = DualVector(2,1);
    sol4 = DualVector(2,1);
    pop.push_back( sol1 );
    pop.push_back( sol2 );
    pop.push_back( sol3 );
    pop.push_back( sol4 );
    if( test(pop, 1) != 1 ) {
        exit(1);
    }

    // test on a random normal distribution
    eoNormalGenerator<double> normal(1,rng);
    eoInitFixedLength<DualVector> init_N(2, normal);
    pop = eoPop<DualVector>( 1000000, init_N );
    double iqr = test(pop, 1.09);
    if( iqr < 1.08 || iqr > 1.11 ) {
        exit(1);
    }
}
Esempio n. 6
0
/**
 * @return One or two intersection points between given entities.
 */
RS_VectorSolutions RS_Information::getIntersectionLineArc(RS_Line* line,
        RS_Arc* arc) {

    RS_VectorSolutions ret;

    if (line==NULL || arc==NULL) {
        return ret;
    }

    double dist=0.0;
    RS_Vector nearest;
    nearest = line->getNearestPointOnEntity(arc->getCenter(), false, &dist);

    // special case: arc touches line (tangent):
    if (fabs(dist - arc->getRadius()) < 1.0e-4) {
        ret = RS_VectorSolutions(nearest);
        ret.setTangent(true);
        return ret;
    }

    RS_Vector p = line->getStartpoint();
    RS_Vector d = line->getEndpoint() - line->getStartpoint();
    if (d.magnitude()<1.0e-6) {
        return ret;
    }

    RS_Vector c = arc->getCenter();
    double r = arc->getRadius();
    RS_Vector delta = p - c;

    // root term:
    double term = RS_Math::pow(RS_Vector::dotP(d, delta), 2.0)
                  - RS_Math::pow(d.magnitude(), 2.0)
                  * (RS_Math::pow(delta.magnitude(), 2.0) - RS_Math::pow(r, 2.0));

    // no intersection:
    if (term<0.0) {
        RS_VectorSolutions s;
        ret = s;
    }

    // one or two intersections:
    else {
        double t1 = (- RS_Vector::dotP(d, delta) + sqrt(term))
                    / RS_Math::pow(d.magnitude(), 2.0);
        double t2;
        bool tangent = false;

        // only one intersection:
        if (fabs(term)<RS_TOLERANCE) {
            t2 = t1;
            tangent = true;
        }

        // two intersections
        else {
            t2 = (-RS_Vector::dotP(d, delta) - sqrt(term))
                 / RS_Math::pow(d.magnitude(), 2.0);
        }

        RS_Vector sol1;
        RS_Vector sol2(false);

        sol1 = p + d * t1;

        if (!tangent) {
            sol2 = p + d * t2;
        }

        ret = RS_VectorSolutions(sol1, sol2);
        ret.setTangent(tangent);
    }

    return ret;
}
Esempio n. 7
0
 int maxDepth(TreeNode* root) {
     
     //return sol1(root);
     return sol2(root);
     
 }
Esempio n. 8
0
 bool isValidBST(TreeNode* root) {
     /****** Sol 2. compare current node with the previous one ******/
     TreeNode *prev = NULL;
     return sol2(root, prev);
 }
Esempio n. 9
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;

}
Esempio n. 10
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;
}
Esempio n. 11
0
bool SIMoutput::dumpResults (const Vector& psol, double time,
                             utl::LogStream& os, const ResPointVec& gPoints,
                             bool formatted, std::streamsize precision) const
{
  if (gPoints.empty())
    return true;

  size_t i, j, k;
  Matrix sol1, sol2;
  Vector reactionFS;
  const Vector* reactionForces = myEqSys->getReactions();

  for (i = 0; i < myModel.size(); i++)
  {
    if (myModel[i]->empty()) continue; // skip empty patches

    ResPointVec::const_iterator p;
    std::array<RealArray,3> params;
    IntVec points;

    // Find all evaluation points within this patch, if any
    for (j = 0, p = gPoints.begin(); p != gPoints.end(); j++, p++)
      if (this->getLocalPatchIndex(p->patch) == (int)(i+1))
        if (opt.discretization >= ASM::Spline)
        {
          points.push_back(p->inod > 0 ? p->inod : -(j+1));
          for (k = 0; k < myModel[i]->getNoParamDim(); k++)
            params[k].push_back(p->u[k]);
        }
        else if (p->inod > 0)
          points.push_back(p->inod);

    if (points.empty()) continue; // no points in this patch

    myModel[i]->extractNodeVec(psol,myProblem->getSolution());
    if (opt.discretization >= ASM::Spline)
    {
      // Evaluate the primary solution variables
      if (!myModel[i]->evalSolution(sol1,myProblem->getSolution(),
                                    params.data(),false))
        return false;

      // Evaluate the secondary solution variables
      LocalSystem::patch = i;
      if (myProblem->getNoFields(2) > 0)
      {
        const_cast<SIMoutput*>(this)->setPatchMaterial(i+1);
        if (!myModel[i]->evalSolution(sol2,*myProblem,params.data(),false))
          return false;
      }
    }
    else
      // Extract nodal primary solution variables
      if (!myModel[i]->getSolution(sol1,myProblem->getSolution(),points))
        return false;

    // Formatted output, use scientific notation with fixed field width
    std::streamsize flWidth = 8 + precision;
    std::streamsize oldPrec = os.precision(precision);
    std::ios::fmtflags oldF = os.flags(std::ios::scientific | std::ios::right);
    for (j = 0; j < points.size(); j++)
    {
      if (!formatted)
        os << time <<" ";
      else if (points[j] < 0)
        os <<"  Point #"<< -points[j] <<":\tsol1 =";
      else
      {
        points[j] = myModel[i]->getNodeID(points[j]);
        os <<"  Node #"<< points[j] <<":\tsol1 =";
      }

      for (k = 1; k <= sol1.rows(); k++)
        os << std::setw(flWidth) << utl::trunc(sol1(k,j+1));

      if (opt.discretization >= ASM::Spline)
      {
        if (formatted && sol2.rows() > 0)
          os <<"\n\t\tsol2 =";
        for (k = 1; k <= sol2.rows(); k++)
          os << std::setw(flWidth) << utl::trunc(sol2(k,j+1));
      }

      if (reactionForces && points[j] > 0)
        // Print nodal reaction forces for nodes with prescribed DOFs
        if (mySam->getNodalReactions(points[j],*reactionForces,reactionFS))
        {
          if (formatted)
            os <<"\n\t\treac =";
          for (k = 0; k < reactionFS.size(); k++)
            os << std::setw(flWidth) << utl::trunc(reactionFS[k]);
        }

      os << std::endl;
    }
    os.precision(oldPrec);
    os.flags(oldF);
  }

  return true;
}