Esempio n. 1
0
void compare(CompareCmd *env, FE_Field *field_a, PointField *field_b)
{
    if (env->verbose)
    {
        cout << "Comparing FE_Field against PointField" << endl;
    }
    assert(env->quadrature != 0);

    QuadratureRule *qr = env->quadrature;
    Residuals *residuals = env->residuals;

    // dimensions
    int nel = qr->meshPart->nel;
    int nq = qr->points->n_points();
    int valdim = field_a->n_rank();

    assert((nel*nq) == field_b->points->n_points());
    assert(valdim == field_b->values->n_dim());

    // field values at quadrature points
    Points phi_a, phi_b;
    double *values_a = new double[nq * valdim];
    double *values_b = new double[nq * valdim];
    phi_a.set_data(values_a, nq, valdim);
    phi_b.set_data(values_b, nq, valdim);

    residuals->reset_accumulator();
    env->start_timer(nel, "elts");

    // main loop
    for (int e = 0; e < nel; e++)
    {
        qr->select_cell(e);
        for (int q = 0; q < nq; q++)
        {
            double *globalPoint = (*(qr->points))[q];
            bool ca = field_a->eval(globalPoint, &values_a[q*valdim]);
            //bool cb = field_b->eval(globalPoint, &values_b[q*valdim]);
            for (int i = 0; i < valdim; i++)
            {
                values_b[q*valdim + i] = field_b->values->data[q*valdim + i];
            }
        }
        double err2 = qr->L2(phi_a, phi_b);
        double vol = qr->meshPart->cell->volume();
        //residuals->update(e, sqrt(err2)/vol);
        //residuals->update(e, sqrt(err2));
        residuals->update(e, sqrt(err2), vol);
        env->update_timer(e);
    }
    env->end_timer();

    // clean up
    delete [] values_a;
    delete [] values_b;
}
Esempio n. 2
0
 /**
  * Sets the residuals.
  */  
 void Point::SetResiduals(const Residuals& r)
 {
   if (!this->mp_Data)
     this->SetData(Point::Data::New(static_cast<int>(r.rows())));
   this->mp_Data->SetResiduals(r);
   this->Modified();
 };
Esempio n. 3
0
void compare(CompareCmd *env, FE_Field *field_a, FE_Field *field_b)
{
    if (env->verbose)
    {
        cout << "Comparing FE_Field against FE_Field" << endl;
    }
    assert(env->quadrature != 0);

    QuadratureRule *qr = env->quadrature;
    Residuals *residuals = env->residuals;

    // dimensions
    int nel = qr->meshPart->nel;
    int nq = qr->points->n_points();
    int valdim = field_a->n_rank();

    // field values at quadrature points
    Points phi_a, phi_b;
    double *values_a = new double[nq * valdim];
    double *values_b = new double[nq * valdim];
    phi_a.set_data(values_a, nq, valdim);
    phi_b.set_data(values_b, nq, valdim);

    field_a->fe->initialize_basis_tab();
    residuals->reset_accumulator();

    // main loop
    env->start_timer(nel,"elts");
    for (int e = 0; e < nel; e++)
    {
        qr->select_cell(e);
        field_a->tabulate_element(e, phi_a.data);
        bool cb = field_b->Field::eval(*(qr->points), phi_b);
        double err2 = qr->L2(phi_a, phi_b);
        double vol = qr->meshPart->cell->volume();
        //residuals->update(e, sqrt(err2)/vol);
        //residuals->update(e, sqrt(err2));
        residuals->update(e, sqrt(err2), vol);
        env->update_timer(e);
    }
    env->end_timer();

    // clean up
    delete [] values_a;
    delete [] values_b;
}
Esempio n. 4
0
bool pinpal(char* dataFile, char* initFile, char* outFile,
	    char* paraFile, bool isInitFile, bool isInitSparse,
	    bool isDataSparse, bool isParameter,
	    Parameter::parameterType parameterType,
	    FILE* Display)
{
  TimeStart(TOTAL_TIME_START1);
  TimeStart(FILE_READ_START1);
  ComputeTime com;

  FILE* fpData      = NULL;
  FILE* fpOut       = NULL;

  if ((fpOut=fopen(outFile,"w"))==NULL) {
    rError("Cannot open out file " << outFile);
  }
  Parameter param;
  param.setDefaultParameter(parameterType);
  if (isParameter) {
    FILE* fpParameter = NULL;
    if ((fpParameter=fopen(paraFile,"r"))==NULL) {
      fprintf(Display,"Cannot open parameter file %s \n",
	      paraFile);
      exit(0);
    } else {
      param.readFile(fpParameter);
      fclose(fpParameter);
    }
  }
  
  // param.display(Display,param.infPrint);

  if ((fpData=fopen(dataFile,"r"))==NULL) {
    rError("Cannot open data file " << dataFile);
  }
  char titleAndComment[LengthOfBuffer];
  int m;
  time_t ltime;
  time( &ltime );
  fprintf(fpOut,"SDPA start at %s",ctime(&ltime));
  IO::read(fpData,fpOut,m,titleAndComment);
  fprintf(fpOut,"data      is %s\n",dataFile);
  if (paraFile) {
    fprintf(fpOut,"parameter is %s\n",paraFile);
  }
  if (initFile) {
    fprintf(fpOut,"initial   is %s\n",initFile);
  }
  fprintf(fpOut,"out       is %s\n",outFile);


#if 1 // 2007/11/28 nakata    for multi LP block

  int SDP_nBlock, SOCP_nBlock,LP_nBlock, nBlock;
  IO::read(fpData,nBlock);

  int* blockStruct          = NULL;
  IO::BlockType* blockType  = NULL;
  int* blockNumber          = NULL;
  int* SDP_blockStruct      = NULL;
  int* SOCP_blockStruct     = NULL;
  NewArray(blockStruct,int,nBlock);
  NewArray(blockType,  IO::BlockType, nBlock);
  NewArray(blockNumber,int,nBlock);
  IO::read(fpData,nBlock,blockStruct);

  SDP_nBlock  = 0;
  SOCP_nBlock = 0;
  LP_nBlock   = 0;
  for (int l=0; l<nBlock; l++){
	if (blockStruct[l] >= 2) {
	  blockType[l] = IO::btSDP;
	  blockNumber[l] = SDP_nBlock;
	  SDP_nBlock++;
	} else if (blockStruct[l] < 0) {
	  blockType[l] = IO::btLP;
	  blockStruct[l] = - blockStruct[l];
	  blockNumber[l] = LP_nBlock;
	  LP_nBlock += blockStruct[l];
	} else if (blockStruct[l] == 1) {
	  blockType[l] = IO::btLP;
	  blockNumber[l] = LP_nBlock;
	  LP_nBlock += blockStruct[l];
	} else {
	  rError("block struct");
	}
  }

  NewArray(SDP_blockStruct, int,SDP_nBlock);
  NewArray(SOCP_blockStruct,int,SOCP_nBlock);
  
  SDP_nBlock = 0;
  for (int l=0; l<nBlock; l++){
	if (blockType[l] == IO::btSDP) {
	  SDP_blockStruct[SDP_nBlock] = blockStruct[l];
	  SDP_nBlock++;
	} 
  }

  InputData inputData;
  //  rMessage("read input data: start");
  IO::read(fpData, m, SDP_nBlock, SDP_blockStruct,
	   SOCP_nBlock, SOCP_blockStruct, LP_nBlock,
	   nBlock, blockStruct, blockType, blockNumber,
	   inputData,isDataSparse);
  //  rMessage("read input data: end");
  inputData.initialize_index(SDP_nBlock,SOCP_nBlock,LP_nBlock,com);
#else 

  int SDP_nBlock, SOCP_nBlock,LP_nBlock;
  IO::read(fpData,SDP_nBlock,SOCP_nBlock,LP_nBlock);
  int* SDP_blockStruct;
  int* SOCP_blockStruct;
  NewArray(SDP_blockStruct ,int,SDP_nBlock);
  NewArray(SOCP_blockStruct,int,SOCP_nBlock);
  IO::read(fpData,SDP_nBlock,SDP_blockStruct,
	   SOCP_nBlock,SOCP_blockStruct, LP_nBlock);

  for (int l=0; l<SDP_nBlock-1; l++){
    if (SDP_blockStruct[l] < 0){
      rError("LP block must be in the last block");
    }
  }
  // muriyari nyuuryoku saseru
  if (SDP_blockStruct[SDP_nBlock-1] < 0){
    LP_nBlock = - SDP_blockStruct[SDP_nBlock-1];
    SDP_nBlock--;
  }

  InputData inputData;
  IO::read(fpData, m, SDP_nBlock, SDP_blockStruct,
	   SOCP_nBlock, SOCP_blockStruct, LP_nBlock,
	   inputData,isDataSparse);
  inputData.initialize_index(SDP_nBlock,SOCP_nBlock,LP_nBlock,com);

#endif
  fclose(fpData);
  
  // inputData.display();


#if 1
  TimeStart(FILE_CHANGE_START1);
  // if possible , change C and A to Dense
  inputData.C.changeToDense();
  for (int k=0; k<m; ++k) {
    inputData.A[k].changeToDense();
  }
  TimeEnd(FILE_CHANGE_END1);
  com.FileChange += TimeCal(FILE_CHANGE_START1,
			     FILE_CHANGE_END1);
#endif

  // rMessage("C = ");
  // inputData.C.display(Display);
  // for (int k=0; k<m; ++k) {
  //   rMessage("A["<<k<<"] = ");
  //   inputData.A[k].display(Display);
  //   }
  
  // the end of initialization of C and A

  Newton newton(m, SDP_nBlock, SDP_blockStruct,
		SOCP_nBlock, SOCP_blockStruct,	LP_nBlock);
  int nBlock2 = SDP_nBlock+SOCP_nBlock+LP_nBlock;
  // 2008/03/12 kazuhide nakata
  Chordal chordal;
  // rMessage("ordering bMat: start");
  chordal.ordering_bMat(m, nBlock2, inputData, fpOut);
  // rMessage("ordering bMat: end");
  newton.initialize_bMat(m, chordal,inputData, fpOut);
  chordal.terminate();

  //  rMessage("newton.computeFormula_SDP: start");
  newton.computeFormula_SDP(inputData,0.0,KAPPA);
  //  rMessage("newton.computeFormula_SDP: end");

  // set initial solutions.
  Solutions currentPt;
  WorkVariables work;
  DenseLinearSpace initPt_xMat;
  DenseLinearSpace initPt_zMat;

  currentPt.initialize(m, SDP_nBlock, SDP_blockStruct,
		       SOCP_nBlock, SOCP_blockStruct, LP_nBlock,
		       param.lambdaStar,com);
  work.initialize(m, SDP_nBlock, SDP_blockStruct,
		  SOCP_nBlock, SOCP_blockStruct, LP_nBlock);

  if (isInitFile) {
    FILE* fpInit = NULL;
    if ((fpInit=fopen(initFile,"r"))==NULL) {
      rError("Cannot open init file " << initFile);
    }
    IO::read(fpInit,currentPt.xMat,currentPt.yVec,currentPt.zMat,
	     SDP_nBlock,SDP_blockStruct,
	     SOCP_nBlock,SOCP_blockStruct,
	     LP_nBlock, nBlock, blockStruct,
	     blockType, blockNumber,
	     isInitSparse);
    #if 0
    rMessage("intial X = ");
    currentPt.xMat.display();
    rMessage("intial Z = ");
    currentPt.zMat.display();
    #endif
    fclose(fpInit);
    currentPt.computeInverse(work,com);

    initPt_xMat.initialize(SDP_nBlock, SDP_blockStruct,
			   SOCP_nBlock, SOCP_blockStruct, 
			   LP_nBlock);
    
    initPt_zMat.initialize(SDP_nBlock, SDP_blockStruct,
			   SOCP_nBlock, SOCP_blockStruct, 
			   LP_nBlock);
    initPt_xMat.copyFrom(currentPt.xMat);
    initPt_zMat.copyFrom(currentPt.zMat);

  }
  //  rMessage("initial xMat = "); initPt_xMat.display(Display);
  //  rMessage("initial yVec = "); currentPt.yVec.display(Display);
  //  rMessage("initial zMat = "); initPt_zMat.display(Display);
  //  rMessage("current pt = "); currentPt.display(Display);
  
  TimeEnd(FILE_READ_END1);
  com.FileRead += TimeCal(FILE_READ_START1,
			   FILE_READ_END1);
  // -------------------------------------------------------------
  // the end of file read
  // -------------------------------------------------------------
  
  Residuals initRes(m, SDP_nBlock, SDP_blockStruct,
		    SOCP_nBlock, SOCP_blockStruct,
		    LP_nBlock, inputData, currentPt);
  Residuals currentRes;
  currentRes.copyFrom(initRes);
  // rMessage("initial currentRes = ");
  // currentRes.display(Display);


  StepLength alpha;
  DirectionParameter beta(param.betaStar);
  Switch reduction(Switch::ON);
  AverageComplementarity mu(param.lambdaStar);

  // rMessage("init mu"); mu.display();

  if (isInitFile) {
    mu.initialize(currentPt);
  }

  RatioInitResCurrentRes theta(param, initRes);
  SolveInfo solveInfo(inputData, currentPt, 
			mu.initial, param.omegaStar);
  Phase phase(initRes, solveInfo, param, currentPt.nDim);

  int pIteration = 0;
  IO::printHeader(fpOut, Display);
  // -----------------------------------------------------
  // Here is MAINLOOP
  // -----------------------------------------------------

  TimeStart(MAIN_LOOP_START1);
  // explicit maxIteration for debug
  // param.maxIteration = 2;
  while (phase.updateCheck(currentRes, solveInfo, param)
	 && pIteration < param.maxIteration) {
    // rMessage(" turn hajimari " << pIteration );
    // Mehrotra's Predictor
    TimeStart(MEHROTRA_PREDICTOR_START1);
    // set variable of Mehrotra
    reduction.MehrotraPredictor(phase);
    beta.MehrotraPredictor(phase, reduction, param);

    // rMessage("reduction = "); reduction.display();
    // rMessage("phase = "); phase.display();
    // rMessage("beta.predictor.value = " << beta.value);
    // rMessage(" mu = " << mu.current);
    // rMessage("currentPt = "); currentPt.display();

    bool isSuccessCholesky;
    isSuccessCholesky = newton.Mehrotra(Newton::PREDICTOR,
					inputData, currentPt,
					currentRes,
					mu, beta, reduction,
					phase,work,com);
    if (isSuccessCholesky == false) {
      break;
    }
    // rMessage("newton predictor = "); newton.display();

    TimeEnd(MEHROTRA_PREDICTOR_END1);
    com.Predictor += TimeCal(MEHROTRA_PREDICTOR_START1,
			      MEHROTRA_PREDICTOR_END1);

    TimeStart(STEP_PRE_START1);
    alpha.MehrotraPredictor(inputData, currentPt, phase, newton,
			      work, com);
    // rMessage("alpha predictor = "); alpha.display();

    TimeStart(STEP_PRE_END1);
    com.StepPredictor += TimeCal(STEP_PRE_START1,STEP_PRE_END1);

    // rMessage("alphaStar = " << param.alphaStar);
    // Mehrotra's Corrector
    // rMessage(" Corrector ");

    TimeStart(CORRECTOR_START1);
    beta.MehrotraCorrector(phase,alpha,currentPt, newton,mu,param);

    // rMessage("beta corrector = " << beta.value);

#if 1 // 2007/08/29 kazuhide nakata
    // add stopping criteria: objValPrimal < ObjValDual
    //	if ((pIteration > 10) &&
    if (phase.value == SolveInfo::pdFEAS
	&& ( beta.value> 5.0
	     || solveInfo.objValPrimal < solveInfo.objValDual)){
      break;
    }
#endif

    newton.Mehrotra(Newton::CORRECTOR,
		    inputData, currentPt, currentRes,
		    mu, beta, reduction, phase,work,com);

    // rMessage("currentPt = "); currentPt.display();
    // rMessage("newton corrector = "); newton.display();

    TimeEnd(CORRECTOR_END1);
    com.Corrector += TimeCal(CORRECTOR_START1,
			      CORRECTOR_END1);
    TimeStart(CORRECTOR_STEP_START1);
    alpha.MehrotraCorrector(inputData, currentPt, phase,
			    reduction, newton, mu, theta,
			    work, param, com);
    // rMessage("alpha corrector = "); alpha.display();
    TimeEnd(CORRECTOR_STEP_END1);
    com.StepCorrector += TimeCal(CORRECTOR_STEP_START1,
				  CORRECTOR_STEP_END1);
    // the end of Corrector
    
    IO::printOneIteration(pIteration, mu, theta, solveInfo,
			   alpha, beta, fpOut, Display);

    if (currentPt.update(alpha,newton,work,com)==false) {
      // if step length is too short,
      // we finish algorithm
      rMessage("cannot move: step length is too short");
      //   memo by kazuhide nakata
      //   StepLength::MehrotraCorrector
      //   thetaMax*mu.initial -> thetamax*thetaMax*mu.initial
      break;
    }

    // rMessage("currentPt = "); currentPt.display();
    // rMessage("updated");

    theta.update(reduction,alpha);
    mu.update(currentPt);
    currentRes.update(m,inputData, currentPt, com);
    theta.update_exact(initRes,currentRes);

    if (isInitFile) {
      solveInfo.update(inputData, initPt_xMat, initPt_zMat, currentPt,
		       currentRes, mu, theta, param);
    } else {
      solveInfo.update(param.lambdaStar,inputData, currentPt,
		       currentRes, mu, theta, param);
    }
    // 2007/09/18 kazuhide nakata
    // print information of ObjVal, residual, gap, complementarity
    // solveInfo.check(inputData, currentPt,
    //                 currentRes, mu, theta, param);
    pIteration++;
  } // end of MAIN_LOOP

  TimeEnd(MAIN_LOOP_END1);

  com.MainLoop = TimeCal(MAIN_LOOP_START1,
			  MAIN_LOOP_END1);
  currentRes.compute(m,inputData,currentPt);
  TimeEnd(TOTAL_TIME_END1);
  
  com.TotalTime = TimeCal(TOTAL_TIME_START1,
			   TOTAL_TIME_END1);
  #if REVERSE_PRIMAL_DUAL
  phase.reverse();
  #endif
#if 1
  IO::printLastInfo(pIteration, mu, theta, solveInfo, alpha, beta,
		    currentRes, phase, currentPt, com.TotalTime,
			nBlock, blockStruct, blockType, blockNumber,
		    inputData, work, com, param, fpOut, Display);
#else
  IO::printLastInfo(pIteration, mu, theta, solveInfo, alpha, beta,
		    currentRes, phase, currentPt, com.TotalTime,
		    inputData, work, com, param, fpOut, Display);
#endif
  // com.display(fpOut);

  DeleteArray(SDP_blockStruct);
  DeleteArray(blockStruct);
  DeleteArray(blockType);
  DeleteArray(blockNumber);
  
  fprintf(Display,   "  main loop time = %.6f\n",com.MainLoop);
  fprintf(fpOut,   "    main loop time = %.6f\n",com.MainLoop);
  fprintf(Display,   "      total time = %.6f\n",com.TotalTime);
  fprintf(fpOut,   "        total time = %.6f\n",com.TotalTime);
  #if 0
  fprintf(Display,   "file  check time = %.6f\n",com.FileCheck);
  fprintf(fpOut,   "  file  check time = %.6f\n",com.FileCheck);
  fprintf(Display,   "file change time = %.6f\n",com.FileChange);
  fprintf(fpOut,   "  file change time = %.6f\n",com.FileChange);
  #endif
  fprintf(Display,   "file   read time = %.6f\n",com.FileRead);
  fprintf(fpOut,   "  file   read time = %.6f\n",com.FileRead);
  fclose(fpOut);


#if 0
  rMessage("memory release");
  currentRes.terminate();
  initRes.terminate();
  currentPt.terminate();
  initPt_xMat.terminate();
  initPt_zMat.terminate();
  newton.terminate();
  work.terminate();
  inputData.terminate();
  com.~ComputeTime();
  param.~Parameter();
  alpha.~StepLength();
  beta.~DirectionParameter();
  reduction.~Switch();
  mu.~AverageComplementarity();
  theta.~RatioInitResCurrentRes();
  solveInfo.~SolveInfo();
  phase.~Phase();
#endif

  return true;
}
Esempio n. 5
0
void compare(CompareCmd *env, Field *field_a, Field *field_b)
{
    const bool debug = false;

    if (env->verbose)
    {
        cout << "Comparing Field against Field" << endl;
    }
    assert(env->quadrature != 0);

    QuadratureRule *qr = env->quadrature;
    Residuals *residuals = env->residuals;

    // dimensions
    int nel = qr->meshPart->nel;
    int nq = qr->points->n_points();
    int ndim = field_a->n_dim();
    int valdim = field_a->n_rank();

    // field values at quadrature points
    Points phi_a, phi_b;
    double *values_a = new double[nq * valdim];
    double *values_b = new double[nq * valdim];
    phi_a.set_data(values_a, nq, valdim);
    phi_b.set_data(values_b, nq, valdim);

    residuals->reset_accumulator();
    env->start_timer(nel, "elts");

    // main loop
    for (int e = 0; e < nel; e++)
    {
        qr->select_cell(e);
        for (int q = 0; q < nq; q++)
        {
            double *globalPoint = (*(qr->points))[q];
            field_a->eval(globalPoint, &values_a[q*valdim]);
            field_b->eval(globalPoint, &values_b[q*valdim]);
            if (debug)
            {
                int j;
                cerr << e << " " << q << "  ";
                for (j = 0; j < ndim; j++)
                {
                    cerr << globalPoint[j] << " ";
                }
                cerr << "\t";
                for (j = 0; j < valdim; j++)
                {
                    cerr << values_a[valdim*q + j] << " ";
                }
                cerr << "\t";
                for (j = 0; j < valdim; j++)
                {
                    cerr << values_b[valdim*q + j] << " ";
                }
                cerr << endl;
            }
        }
        double err2 = qr->L2(phi_a, phi_b);
        double vol = qr->meshPart->cell->volume();
        //residuals->update(e, sqrt(err2)/vol);
        //residuals->update(e, sqrt(err2));
        residuals->update(e, sqrt(err2), vol);
        env->update_timer(e);
    }
    env->end_timer();

    // clean up
    delete [] values_a;
    delete [] values_b;
}