示例#1
0
bool CRzLog::_Open()
{
	if(_IsOpen())
		return true;

	std::string stmp(_timename);
	stmp += _filename;

	DFn_fopen_s(&_pfile,stmp.c_str(),_T("a"));
//	_pfile = Rzfopen(stmp.c_str(),_T("a"));

	if(!_pfile)
	{
		std::exception e("open logfile error");
		throw e;
	}


	return _IsOpen();
}
示例#2
0
void DumpModel::dump(Amr* parent, int force_dump)
{

  // Exit if it isn't the right time to do the dump

  if (force_dump == 0 &&
      parent->levelSteps(0) % interval != 0) {
    return;
  }

  // Create flattened list of ordered, disjoint boxes from the grid hierarchy

  list<Box> bxlist;
  list<int> lnlist;

  vector<int> rat(parent->finestLevel(), 1);

  for (int ln = 0; ln <= parent->finestLevel(); ln++) {

    // rat[lnp] will be ratio between levels lnp and ln

    if (ln > 0) {
      for (int lnp = 0; lnp < ln; lnp++) {
        rat[lnp] *= parent->refRatio(ln - 1)[0];
      }
    }

    // Insert grids of level ln into list, trimming coarser grids as needed

    const BoxArray& grids = parent->boxArray(ln);
    for (int igr = 0; igr < grids.size(); igr++) {
      const Box& reg = grids[igr];
      list<Box>::iterator bi = bxlist.begin();
      list<int>::iterator li = lnlist.begin();
      for ( ; bi != bxlist.end(); ) {
        if (*li == ln) {
          if (bi->smallEnd(0) > reg.bigEnd(0)) {
            // Insert reg before bi and break
            bxlist.insert(bi, reg);
            lnlist.insert(li, ln);
            break;
          }
          // Increment and continue loop as reg not inserted yet
        }
        else { // *li < ln
          Box bx(BoxLib::refine(*bi,rat[*li]));
          if (bx.bigEnd(0) >= reg.smallEnd(0)) {
            if (bx.smallEnd(0) < reg.smallEnd(0) &&
                bx.bigEnd(0) <= reg.bigEnd(0)) {
              // Trim overlap with reg from *bi leaving low end of *bi
              bx.setBig(0, reg.smallEnd(0) - 1);
              bx.coarsen(rat[*li]);
              *bi = bx;
              // Increment and continue loop as reg not inserted yet
            }
            else if (bx.smallEnd(0) >= reg.smallEnd(0) &&
                     bx.bigEnd(0) <= reg.bigEnd(0)) {
              // Remove *bi from list as reg covers it completely
              list<Box>::iterator bt = bi;
              list<int>::iterator lt = li;
              ++bi;
              ++li;
              bxlist.erase(bt);
              lnlist.erase(lt);
              // Continue loop as reg not inserted yet; but bypass
              // increment at end of loop since we've done it already.
              continue;
            }
            else {
              // To get this far (bx.bigEnd(0) > reg.bigEnd(0)) must be true
              if (bx.smallEnd(0) < reg.smallEnd(0)) {
                // Trim overlap with reg from middle of *bi, extract low frag
                Box bxx(bx);
                bxx.setBig(0, reg.smallEnd(0) - 1);
                bxx.coarsen(rat[*li]);
                // Insert this low fragment of *bi before remainder of *bi
                bxlist.insert(bi, bxx);
                lnlist.insert(li, *li);
              }
              if (bx.smallEnd(0) <= reg.bigEnd(0)) {
                // Trim overlap with reg leaving end high end of *bi
                bx.setSmall(0, reg.bigEnd(0) + 1);
                bx.coarsen(rat[*li]);
                *bi = bx;
              }
              // Insert reg before high end of *bi and break
              bxlist.insert(bi, reg);
              lnlist.insert(li, ln);
              break;
            }
          }
        }

        // Increment and continue loop as reg not inserted yet
        ++bi;
        ++li;

      } // end of loop over bi, li

      if (bi == bxlist.end()) {
        // We made it to the end without finding another place to insert reg
        bxlist.push_back(reg);
        lnlist.push_back(ln);
      }
    }
  }

  // bxlist now contains an ordered list of disjoint boxes representing
  // the exposed portions of all levels of the hierarchy.  Each box is
  // at the resolution of its respective level, and the corresponding
  // entry of lnlist contains the level number.

  if (ParallelDescriptor::IOProcessor() && verbose >= 2) {
    cout << "Printing disjoint box list" << endl;

    list<Box>::iterator bi = bxlist.begin();
    list<int>::iterator li = lnlist.begin();
    for ( ; bi != bxlist.end(); ++bi, ++li) {
      cout << "Level " << *li << ", box " << *bi << endl;
    }
  }

  if (ParallelDescriptor::IOProcessor() && verbose >= 1) {
    cout << "Creating modelDump" << endl;
  }

  ofstream dumpfile;
  if (ParallelDescriptor::IOProcessor()) {
    dumpfile.open("modelDump", std::ios::out);
  }

  const int bufsiz = 200; // must hold 9 fields of width 16 plus a little more
  char buf[bufsiz];

  // The following are not parallel loops.
  // We are doing the Fab copy portion on all processors because
  // all must participate, but the write is done only on IOProcessor.

  int k = 1;
  list<Box>::iterator bi = bxlist.begin();
  list<int>::iterator li = lnlist.begin();
  for ( ; bi != bxlist.end(); ++bi, ++li) {
    const Box& reg = *bi;
    int ln         = *li;
    Castro *castro = dynamic_cast<Castro*>(&parent->getLevel(ln));

    MultiFab& S_new = castro->get_new_data(State_Type);

    Fab stmp(reg, S_new.nComp());

    S_new.copy(stmp);

    if (ParallelDescriptor::IOProcessor()) {
      for (int i = reg.smallEnd(0); i <= reg.bigEnd(0); i++) {
        IntVect p(i);

        // Quantities written as 0.0 are not currently used, so we
        // don't bother to derive them.

        sprintf(buf,
                "%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E%16.8E %d\n",
                parent->Geom(ln).CellCenter(i, 0), // radius
                stmp(p, Density),                      // density
                stmp(p, Xmom) / stmp(p, Density),      // velocity
                0.0,                               // pressure
                stmp(p, Temp),                    // temperature (MeV ?)
                stmp(p, Eint) / stmp(p, Density),      // internal energy e
                0.0,                               // entropy
                0.0,                               // cumulative mass
                stmp(p, FirstAux / stmp(p, Density),       // Ye
                k++);
        std::string Buf = buf;
        dumpfile << Buf;
      }
    }
  }

  if (ParallelDescriptor::IOProcessor()) {
    dumpfile << Radiation::nGroups << '\n';
  }

  k = 1;
  bi = bxlist.begin();
  li = lnlist.begin();
  for ( ; bi != bxlist.end(); ++bi, ++li) {
    const Box& reg = *bi;
    int ln         = *li;
    Castro *castro = dynamic_cast<Castro*>(&parent->getLevel(ln));

    MultiFab& R_new = castro->get_new_data(Rad_Type);

    Fab rtmp(reg, R_new.nComp());

    R_new.copy(rtmp);

    if (ParallelDescriptor::IOProcessor()) {
      for (int i = reg.smallEnd(0); i <= reg.bigEnd(0); i++) {
        IntVect p(i);
        char* bufp = buf;
        for (int j = 0; ; j++) {
          sprintf(bufp, "%16.8E", rtmp(p, j));
          bufp += 16;
          if (j + 1 == Radiation::nGroups) {
            sprintf(bufp, " %d\n", k++);
            std::string Buf = buf;
            dumpfile << Buf;
            break;
          }
          else if (j % 4 == 3) {
            sprintf(bufp, "\n");
            std::string Buf = buf;
            dumpfile << Buf;
            bufp = buf;
          }
        }
      }
    }
  }

  if (ParallelDescriptor::IOProcessor()) {
    dumpfile.close();
  }
}
示例#3
0
QString rtf2html(const QString& iString)
      {
      QString oString;
      try {
            std::string str_in = iString.toStdString();

            std::string::iterator buf_in=str_in.begin(), buf_in_end=str_in.end();
            colorvect colortbl;
            fontmap fonttbl;
            std::string title;

            bool bAsterisk=false;
            fo_stack foStack;
            formatting_options cur_options;
            std::string html;
            html_text par_html(cur_options);

   /* CellDefs in rtf are really queer. We'll keep a list of them in main()
      and will give an iterator into this list to a row */
   table_cell_defs_list CellDefsList;
   table_cell_defs_list::iterator CurCellDefs;
   table_cell_def *tcdCurCellDef=new table_cell_def;
   table_cell *tcCurCell=new table_cell;
   table_row *trCurRow=new table_row;
   table *tblCurTable=new table;
   int iLastRowLeft=0, iLastRowHeight=0;
   std::string t_str;

   bool bInTable=false;
   int iDocWidth=12240;
   int iMarginLeft=1800;
   while(buf_in!=buf_in_end)
   {
      switch (*buf_in)
      {
      case '\\':
      {
         rtf_keyword kw(++buf_in);
         if (kw.is_control_char())
            switch (kw.control_char())
            {
            case '\\': case '{': case '}':
               par_html.write(kw.control_char());
               break;
            case '\'':
            {
               std::string stmp(1,*buf_in++);
               stmp+=*buf_in++;
               int code=std::strtol(stmp.c_str(), NULL, 16);
               switch (code)
               {
                  case 167:
                     par_html.write("&bull;");
                     break;
                  case 188:
                     par_html.write("&hellip;");
                     break;
                  default:
                     par_html.write((char)code);
               }
               break;
            }
            case '*':
               bAsterisk=true;
               break;
            case '~':
               par_html.write("&nbsp;");
               break;
            }
         else //kw.is_control_char
            if (bAsterisk)
            {
               bAsterisk=false;
               skip_group(buf_in);
            }
            else
            {
               switch (kw.keyword())
               {
               case rtf_keyword::rkw_filetbl:
               case rtf_keyword::rkw_stylesheet:
               case rtf_keyword::rkw_header:
               case rtf_keyword::rkw_footer: case rtf_keyword::rkw_headerf:
               case rtf_keyword::rkw_footerf: case rtf_keyword::rkw_pict:
               case rtf_keyword::rkw_object:
                  // we'll skip such groups
                  skip_group(buf_in);
                  break;
               // document title
               case rtf_keyword::rkw_info:
               {
                  int depth=1;
                  bool in_title=false;
                  while (depth>0)
                  {
//                     std::cout<<std::string(buf_in).substr(0,20)<<"\t"<<depth<<std::endl;
                     switch (*buf_in)
                     {
                     case '\\':
                     {
                        rtf_keyword kw(++buf_in);
                        if (kw.keyword()==rtf_keyword::rkw_title)
                           in_title=true;
                        break;
                     }
                     case '{': ++depth; ++buf_in; break;
                     case '}': --depth; ++buf_in; in_title=false; break;
                     default: if (in_title) title+=*buf_in; ++buf_in; break;
                     }
                  }
                  break;
               }
               // color table
               case rtf_keyword::rkw_colortbl:
               {
                  color clr;
                  while (*buf_in!='}')
                  {
                     switch (*buf_in)
                     {
                     case '\\':
                     {
                        rtf_keyword kw(++buf_in);
                        switch (kw.keyword())
                        {
                        case rtf_keyword::rkw_red:
                           clr.r=kw.parameter();
                           break;
                        case rtf_keyword::rkw_green:
                           clr.g=kw.parameter();
                           break;
                        case rtf_keyword::rkw_blue:
                           clr.b=kw.parameter();
                           break;
                        }
                        break;
                     }
                     case ';':
                        colortbl.push_back(clr);
                        ++buf_in;
                        break;
                     default:
                        ++buf_in;
                        break;
                     }
                  }
                  ++buf_in;
                  break;
               }
               // font table
               case rtf_keyword::rkw_fonttbl:
               {
                  font fnt;
                  int font_num;
                  bool full_name=false;
                  bool in_font=false;
                  while (! (*buf_in=='}' && !in_font))
                  {
                     switch (*buf_in)
                     {
                     case '\\':
                     {
                        rtf_keyword kw(++buf_in);
                        if (kw.is_control_char() && kw.control_char()=='*')
                           skip_group(buf_in);
                        else
                           switch (kw.keyword())
                           {
                           case rtf_keyword::rkw_f:
                              font_num=kw.parameter();
                              break;
                           case rtf_keyword::rkw_fprq:
                              fnt.pitch=kw.parameter();
                              break;
                           case rtf_keyword::rkw_fcharset:
                              fnt.charset=kw.parameter();
                              break;
                           case rtf_keyword::rkw_fnil:
                              fnt.family=font::ff_none;
                              break;
                           case rtf_keyword::rkw_froman:
                              fnt.family=font::ff_serif;
                              break;
                           case rtf_keyword::rkw_fswiss:
                              fnt.family=font::ff_sans_serif;
                              break;
                           case rtf_keyword::rkw_fmodern:
                              fnt.family=font::ff_monospace;
                              break;
                           case rtf_keyword::rkw_fscript:
                              fnt.family=font::ff_cursive;
                              break;
                           case rtf_keyword::rkw_fdecor:
                              fnt.family=font::ff_fantasy;
                              break;
                           }
                        break;
                     }
                     case '{':
                        in_font=true;
                        ++buf_in;
                        break;
                     case '}':
                        in_font=false;
                        fonttbl.insert(std::make_pair(font_num, fnt));
                        fnt=font();
                        full_name=false;
                        ++buf_in;
                        break;
                     case ';':
                        full_name=true;
                        ++buf_in;
                        break;
                     default:
                        if (!full_name && in_font)
                           fnt.name+=*buf_in;
                        ++buf_in;
                        break;
                     }
                  }
                  ++buf_in;
                  break;
               }
               // special characters
               case rtf_keyword::rkw_line: case rtf_keyword::rkw_softline:
                  par_html.write("<br>");
                  break;
               case rtf_keyword::rkw_tab:
                  par_html.write("&nbsp;&nbsp;");  // maybe, this can be done better
                  break;
               case rtf_keyword::rkw_enspace: case rtf_keyword::rkw_emspace:
                  par_html.write("&nbsp;");
                  break;
               case rtf_keyword::rkw_qmspace:
                  par_html.write("&thinsp;");
                  break;
               case rtf_keyword::rkw_endash:
                  par_html.write("&ndash;");
                  break;
               case rtf_keyword::rkw_emdash:
                  par_html.write("&mdash;");
                  break;
               case rtf_keyword::rkw_bullet:
                  par_html.write("&bull;");
                  break;
               case rtf_keyword::rkw_lquote:
                  par_html.write("&lsquo;");
                  break;
               case rtf_keyword::rkw_rquote:
                  par_html.write("&rsquo;");
                  break;
               case rtf_keyword::rkw_ldblquote:
                  par_html.write("&ldquo;");
                  break;
               case rtf_keyword::rkw_rdblquote:
                  par_html.write("&rdquo;");
                  break;
               // paragraph formatting
               case rtf_keyword::rkw_ql:
                  cur_options.papAlign=formatting_options::align_left;
                  break;
               case rtf_keyword::rkw_qr:
                  cur_options.papAlign=formatting_options::align_right;
                  break;
               case rtf_keyword::rkw_qc:
                  cur_options.papAlign=formatting_options::align_center;
                  break;
               case rtf_keyword::rkw_qj:
                  cur_options.papAlign=formatting_options::align_justify;
                  break;
               case rtf_keyword::rkw_fi:
                  cur_options.papFirst=(int)rint(kw.parameter()/20);
                  break;
               case rtf_keyword::rkw_li:
                  cur_options.papLeft=(int)rint(kw.parameter()/20);
                  break;
               case rtf_keyword::rkw_ri:
                  cur_options.papRight=(int)rint(kw.parameter()/20);
                  break;
               case rtf_keyword::rkw_sb:
                  cur_options.papBefore=(int)rint(kw.parameter()/20);
                  break;
               case rtf_keyword::rkw_sa:
                  cur_options.papAfter=(int)rint(kw.parameter()/20);
                  break;
               case rtf_keyword::rkw_pard:
                  cur_options.papBefore=cur_options.papAfter=0;
                  cur_options.papLeft=cur_options.papRight=0;
                  cur_options.papFirst=0;
                  cur_options.papAlign=formatting_options::align_left;
                  cur_options.papInTbl=false;
                  break;
               case rtf_keyword::rkw_par: case rtf_keyword::rkw_sect:
                  t_str=cur_options.get_par_str()+par_html.str()
                        +"&nbsp;"+par_html.close()+"</p>\n";
                  if (!bInTable)
                  {
                     html+=t_str;
                  }
                  else
                  {
                     if (cur_options.papInTbl)
                     {
                        tcCurCell->Text+=t_str;
                     }
                     else
                     {
                        html+=tblCurTable->make()+t_str;
                        bInTable=false;
                        delete tblCurTable;
                        tblCurTable=new table;
                     }
                  }
                  par_html.clear();
                  break;
               // character formatting
               case rtf_keyword::rkw_super:
                  cur_options.chpVAlign=
                     kw.parameter()==0?formatting_options::va_normal
                                      :formatting_options::va_sup;
                  break;
               case rtf_keyword::rkw_sub:
                  cur_options.chpVAlign=
                     kw.parameter()==0?formatting_options::va_normal
                                      :formatting_options::va_sub;
                  break;
               case rtf_keyword::rkw_b:
                  cur_options.chpBold=!(kw.parameter()==0);
                  break;
               case rtf_keyword::rkw_i:
                  cur_options.chpItalic=!(kw.parameter()==0);
                  break;
               case rtf_keyword::rkw_ul:
                  cur_options.chpUnderline=!(kw.parameter()==0);
                  break;
               case rtf_keyword::rkw_ulnone:
                  cur_options.chpUnderline=false;
                  break;
               case rtf_keyword::rkw_fs:
                  cur_options.chpFontSize=kw.parameter();
                  break;
               case rtf_keyword::rkw_cf:
                  cur_options.chpFColor=colortbl[kw.parameter()];
                  break;
               case rtf_keyword::rkw_cb:
                  cur_options.chpBColor=colortbl[kw.parameter()];
                  break;
               case rtf_keyword::rkw_highlight:
                  cur_options.chpHighlight=kw.parameter();
                  break;
               case rtf_keyword::rkw_f:
                  cur_options.chpFont=fonttbl[kw.parameter()];
                  break;
               case rtf_keyword::rkw_plain:
                  cur_options.chpBold=cur_options.chpItalic
                  	=cur_options.chpUnderline=false;
                  cur_options.chpVAlign=formatting_options::va_normal;
                  cur_options.chpFontSize=cur_options.chpHighlight=0;
                  cur_options.chpFColor=cur_options.chpBColor=color();
                  cur_options.chpFont=font();
                  break;
               // table formatting
               case rtf_keyword::rkw_intbl:
                  cur_options.papInTbl=true;
                  break;
               case rtf_keyword::rkw_trowd:
                  CurCellDefs=CellDefsList.insert(CellDefsList.end(),
                                                  table_cell_defs());
               case rtf_keyword::rkw_row:
                  if (!trCurRow->Cells.empty())
                  {
                     trCurRow->CellDefs=CurCellDefs;
                     if (trCurRow->Left==-1000)
                        trCurRow->Left=iLastRowLeft;
                     if (trCurRow->Height==-1000)
                        trCurRow->Height=iLastRowHeight;
                     tblCurTable->push_back(trCurRow);
                     trCurRow=new table_row;
                  }
                  bInTable=true;
                  break;
               case rtf_keyword::rkw_cell:
                  t_str=cur_options.get_par_str()+par_html.str()
                        +"&nbsp;"+par_html.close()+"</p>\n";
                  tcCurCell->Text+=t_str;
                  par_html.clear();
                  trCurRow->Cells.push_back(tcCurCell);
                  tcCurCell=new table_cell;
                  break;
               case rtf_keyword::rkw_cellx:
                  tcdCurCellDef->Right=kw.parameter();
                  CurCellDefs->push_back(tcdCurCellDef);
                  tcdCurCellDef=new table_cell_def;
                  break;
               case rtf_keyword::rkw_trleft:
                  trCurRow->Left=kw.parameter();
                  iLastRowLeft=kw.parameter();
                  break;
               case rtf_keyword::rkw_trrh:
                  trCurRow->Height=kw.parameter();
                  iLastRowHeight=kw.parameter();
                  break;
               case rtf_keyword::rkw_clvmgf:
                  tcdCurCellDef->FirstMerged=true;
                  break;
               case rtf_keyword::rkw_clvmrg:
                  tcdCurCellDef->Merged=true;
                  break;
               case rtf_keyword::rkw_clbrdrb:
                  tcdCurCellDef->BorderBottom=true;
                  tcdCurCellDef->ActiveBorder=&(tcdCurCellDef->BorderBottom);
                  break;
               case rtf_keyword::rkw_clbrdrt:
                  tcdCurCellDef->BorderTop=true;
                  tcdCurCellDef->ActiveBorder=&(tcdCurCellDef->BorderTop);
                  break;
               case rtf_keyword::rkw_clbrdrl:
                  tcdCurCellDef->BorderLeft=true;
                  tcdCurCellDef->ActiveBorder=&(tcdCurCellDef->BorderLeft);
                  break;
               case rtf_keyword::rkw_clbrdrr:
                  tcdCurCellDef->BorderRight=true;
                  tcdCurCellDef->ActiveBorder=&(tcdCurCellDef->BorderRight);
                  break;
               case rtf_keyword::rkw_brdrnone:
                  if (tcdCurCellDef->ActiveBorder!=NULL)
                  {
                     *(tcdCurCellDef->ActiveBorder)=false;
                  }
                  break;
               case rtf_keyword::rkw_clvertalt:
                  tcdCurCellDef->VAlign=table_cell_def::valign_top;
                  break;
               case rtf_keyword::rkw_clvertalc:
                  tcdCurCellDef->VAlign=table_cell_def::valign_center;
                  break;
               case rtf_keyword::rkw_clvertalb:
                  tcdCurCellDef->VAlign=table_cell_def::valign_bottom;
                  break;
               // page formatting
               case rtf_keyword::rkw_paperw:
                  iDocWidth=kw.parameter();
                  break;
               case rtf_keyword::rkw_margl:
                  iMarginLeft=kw.parameter();
                  break;
               }
            }
         break;
      }
      case '{':
         // perform group opening actions here
         foStack.push(cur_options);
         ++buf_in;
         break;
      case '}':
         // perform group closing actions here
         cur_options=foStack.top();
         foStack.pop();
         ++buf_in;
         break;
      case 13: case 10:
         ++buf_in;
         break;
      case '<':
         par_html.write("&lt;");
         ++buf_in;
         break;
      case '>':
         par_html.write("&gt;");
         ++buf_in;
         break;
/*      case ' ':
         par_html.write("&ensp;");
         ++buf_in;
         break;*/
      default:
         par_html.write(*buf_in++);
      }
   }

            QString qTitle(QString::fromStdString(title));
            QString qHtml(QString::fromStdString(html));

            oString = QString("<html><head><STYLE type=\"text/css\">body {padding-left:"
                  "%1"
                  "pt;width: %2"
                  "pt}"
                 " p {margin-top:0pt;margin-bottom:0pt}</STYLE>"
                 "<title>%3</title></head>\n"
                  "<body>%4</body></html>"
                  )
                 .arg(rint(iMarginLeft/20))
                 .arg(rint((iDocWidth/20)))
                 .arg(qTitle)
                 .arg(qHtml);

            delete tcCurCell;
            delete trCurRow;
            delete tblCurTable;
            delete tcdCurCellDef;
            return oString;
            }
      catch (std::exception &e) {
            std::cerr<<"Error: "<<e.what()<<std::endl;
            }
      catch (...) {
            std::cerr<<"Something really bad happened!";
            }
      return "";
      }
示例#4
0
  Int Interface::innerNormalDirection (Real & infeasible_gradient) {
    Real oldnormc = c->norm();
    Vector d(*env), dcp(*env), dn(*env);
    Real ndn;
    Real alpha, Ared, Pred;
    Real one[2] = {1,0};
    Real zero[2] = {0,0};
    Int iout, naflag;
    Bool dnavail;
    Vector gtmp (*env);
    Vector xtmp (*xc), ctmp (*c), stmp (*env);
    Real normgtmp = 0;

    Aavail = dciFalse;

    Real scalingMatrix[nvar + nconI];
    gtmp.sdmult (*J, 1, one, zero, ctmp); // g = J'*c
    pReal gtmpx = gtmp.get_doublex();
    for (Int i = 0; i < nvar+nconI; i++) {
      Real gi = gtmpx[i], zi = xcx[i], ui = u_bndx[i], li = l_bndx[i];
      if ( (gi < 0) && (ui < dciInf) ) {
        scalingMatrix[i] = 1.0/sqrt(ui - zi);
      } else if ( (gi > 0) && (li > -dciInf) ) {
        scalingMatrix[i] = 1.0/sqrt(zi - li);
      } else {
        scalingMatrix[i] = 1;
      }
    }
    normgtmp = gtmp.norm ();
    Vector gtmp_proj(*env);
    gtmp_proj.scale(gtmp, -1.0);
    projectBounds_xc(gtmp_proj);
    infeasible_gradient = gtmp_proj.norm();
//    DeltaV = normgtmp;

    if (normgtmp < dciTiny) {
      normc = oldnormc;
      iout = 6;
//      std::cout << "iout = 6" << std::endl;
      return iout;
    }
    //Now with the infinity norm
    Real lower[nvar + nconI], upper[nvar + nconI];
    for (Int i = 0; i < nvar+nconI; i++) {
      Real zi = xcx[i], li = l_bndx[i], ui = u_bndx[i];
      lower[i] = Max( -DeltaV,
          (li > -dciInf ? (li - zi) * (1 - epsmu) : -dciInf) );
      upper[i] = Min(  DeltaV,
          (ui <  dciInf ? (ui - zi) * (1 - epsmu) :  dciInf) );
    }
    Vector aux(*env);
    d = gtmp;
    pReal dx = 0;
    gtmpx = gtmp.get_doublex();
    dx = d.get_doublex();
    for (Int i = 0; i < nvar + nconI; i++) {
      gtmpx[i] /= scalingMatrix[i];
      dx[i] = -dx[i]/pow(scalingMatrix[i], 2);
    }

    aux.sdmult(*J, 0, one, zero, d);
    alpha = Min(gtmp.dot(gtmp)/aux.dot(aux), DeltaV/gtmp.norm());
    dcp.scale (d, alpha);
    pReal dcpx = dcp.get_doublex();

//    alpha = -d.dot(gtmp)/aux.dot(aux);
//    alpha = Min(alpha, DeltaV/d.norm());
    alpha = 1.0;
    for (int i = 0; i < nvar + nconI; i++) {
      Real di = dcpx[i], ui = upper[i], li = lower[i];
      if (di > dciEps) {
        alpha = Min(alpha, ui/(di));
      } else if (di < -dciEps) {
        alpha = Min(alpha, li/(di));
      } else
        dcpx[i] = 0;
    }
    Real theta = 0.99995;
    if (alpha < 1) {
      alpha = Max(theta, 1 - dcp.norm())*alpha;
      dcp.scale(alpha);
    }
/*     for (Int i = 0; i < nvar + nconI; i++)
 *       dcpx[i] *= scalingMatrix[i];
 */
    dnavail = dciFalse;
    ndn = 0;
    Ared = 0;
    Pred = 1;
//    DeltaV = DeltaV/kappa2;
    iout = 0;

    // For the inequalities
    pReal dnx = 0;

    dnavail = dciFalse;
    if (!dnavail) {
      naflag = naStep (*c, dn);

      dnavail = dciTrue;

      dnx = dn.get_doublex();
      //Project this step
      alpha = 1.0;
      for (Int i = 0; i < nvar + nconI; i++) {
        Real di = dnx[i], ui = upper[i], li = lower[i];
        if (di > dciEps) {
          alpha = Min(alpha, ui/di);
        } else if (di < -dciEps) {
          alpha = Min(alpha, li/di);
        } else
          di = 0;
      }
      if (alpha < 1) {
        alpha = Max(theta, 1 - dn.norm())*alpha;
        dn.scale(alpha);
      }

      if (naflag > 1)
        ndn = 0;
      else
        ndn = dn.norm (0);
      assert(ndn <= DeltaV || "ndn > DeltaV");
    }

    /* ||a + b||^2 = <a+b,a+b> = <a,a> + 2*<a,b> + <b,b> */
    /* m(d) = 0.5*||J*d + h||^2
     * dtr = t*dn + (1 - t)*dcp
     * m(dtr) = 0.5*||J*(t*dn + (1-t)*dcp) + h||^2
     *   = 0.5*||J*dcp + h + t*J*(dn - dcp)||^2
     *   = 0.5*||J*dcp + h||^2 + t*(J*dcp + h)'*J*(dn - dcp) + 0.5*t^2*||J*(dn - dcp)||^2 */
    Vector Adcph(*c), difdcdn(dn), Adif(*env);
    Adcph.sdmult(*J, 0, one, one, dcp);
    difdcdn.saxpy(dcp, -1);
    Adif.sdmult(*J, 0, one, zero, difdcdn);

    Real objValAdcph = 0.5*Adcph.dot(Adcph);
    Real dotAdcphAdif = Adcph.dot(Adif);
    Real halfSqrNormAdif = 0.5*Adif.dot(Adif);

    Real cauchyReduction = 0.5*oldnormc*oldnormc - objValAdcph;
    Real newtonReduction = cauchyReduction - dotAdcphAdif - halfSqrNormAdif;

    Real factor = 1.0;
    while (newtonReduction/cauchyReduction < beta1) {
      // Line search among from newton to cauchy
      factor *= 0.9;
      newtonReduction = cauchyReduction - factor*dotAdcphAdif - pow(factor,2)*halfSqrNormAdif;
      if (factor < 1e-8) {
        factor = 0;
        break;
      }
    }

    Vector xtemp(*xc);

    for (Int i = 0; i < nvar+nconI; i++) {
      if (l_bndx[i] - u_bndx[i] > -dciEps)
        continue;
      xcx[i] += (factor*dnx[i] + (1 - factor)*dcpx[i]);
      if (xcx[i] >= u_bndx[i])
        xcx[i] = u_bndx[i] - dciEps;
      else if (xcx[i] <= l_bndx[i])
        xcx[i] = l_bndx[i] + dciEps;
    }

#ifndef NDEBUG
    checkInfactibility();
#endif

    call_ccfsg_xc(dciFalse);
    normc = c->norm ();

    Ared = 0.5*(oldnormc*oldnormc - normc*normc);
    Pred = newtonReduction;

    if (Ared/Pred < beta2) {
      DeltaV /= 4;
      *xc = xtmp;
      call_ccfsg_xc(dciFalse);
      normc = c->norm();
    } else if (Ared/Pred > 0.75) {
      DeltaV *= 2;
    }

    if (normc < rho)
      return 0;

    current_time = getTime() - start_time;

    return 0;
  }