result_t zlib_base::gzip(Buffer_base *data, obj_ptr<Buffer_base> &retVal, exlib::AsyncEvent *ac) { if (switchToAsync(ac)) return CHECK_ERROR(CALL_E_NOSYNC); return gz().process(data, retVal); }
result_t zlib_base::gzipTo(Stream_base *src, Stream_base *stm, exlib::AsyncEvent *ac) { if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); return gz().process(src, stm, ac); }
int AppGunzip::run(int argc, char **argv) { _options.parse(argc, argv); ifstream ifs(argv[1]); ofstream ofs(argv[2]); BitInput2Stream bi(&ifs); GzipStream gz(&bi); gz.extractTo(ofs); ofs.close(); ifs.close(); return 0; }
int main(int argc, char** argv) { getOpts(argc, argv); logMsg("Hello World!"); // 1 gz or 2 gz if (gz2_path == "NONE") { logMsg("single gz"); GZ gz(gz1_path, output_path); gz.testFunc(row_num, is_compress); } else { logMsg("double gz"); GZPair gzp(gz1_path, gz2_path, output_path); gzp.testFunc(row_num, is_compress); } return 0; }
//-----------------------------------------------------------------------------------------// //--------------------------------DRAW NEW POINT TO THE GRAPHS-----------------------------// //-----------------------------------------------------------------------------------------// void RealTimePlotWindow::dataAvaible(QByteArray ext) { double przelicznik = 6103515625;//prescaler value double memory = 2; QVector<measurementData> data = ExtractData(ext);//extract data from dialogwinndow quint16 newDataSize = data.size(); QVector<double> key(newDataSize); QVector<double> ax(newDataSize); QVector<double> ay(newDataSize); QVector<double> az(newDataSize); QVector<double> gx(newDataSize); QVector<double> gy(newDataSize); QVector<double> gz(newDataSize); double keyPrescaler =0; keyPrescaler = keyCounter / 200; // qDebug()<<"znacznik"; for(double i = 0; i<newDataSize; i++) { key[i] = (double)keyPrescaler + (i/200); // key[i] = (double)keyCounter; ax[i] = ((double)data.at(i).ax.as_word)*przelicznik/100000000000; ay[i] = ((double)data.at(i).ay.as_word)*(-1)*przelicznik/100000000000; az[i] = ((double)data.at(i).az.as_word)*(-1)*przelicznik/100000000000; gx[i] = ((double)data.at(i).gx.as_word)*przelicznik/100000000000; gy[i] = ((double)data.at(i).gy.as_word)*(-1)*przelicznik/100000000000; gz[i] = ((double)data.at(i).gz.as_word)*(-1)*przelicznik/100000000000; } if((newDataSize>0)&(triger == true))//get instValues 5 times per secound(depend of timmer interval) { (*instValues)[0] = ax[0]; (*instValues)[1] = ay[0]; (*instValues)[2] = az[0]; (*instValues)[3] = gx[0]; (*instValues)[4] = gy[0]; (*instValues)[5] = gz[0]; triger = false; instValWindow->setInstValues(instValues); } // add new data to graphs ui->accPlot->graph(0)->addData(key, ax); ui->accPlot->graph(1)->addData(key, ay); ui->accPlot->graph(2)->addData(key, az); ui->gyroPlot->graph(0)->addData(key, gx); ui->gyroPlot->graph(1)->addData(key, gy); ui->gyroPlot->graph(2)->addData(key, gz); // remove data of lines that's outside visible range: ui->accPlot->graph(0)->removeDataBefore(keyPrescaler-memory); ui->accPlot->graph(1)->removeDataBefore(keyPrescaler-memory); ui->accPlot->graph(2)->removeDataBefore(keyPrescaler-memory); // qDebug()<<"znacznik 4\n"; // rescale value (vertical) axis to fit the current data: ui->accPlot->graph(0)->rescaleValueAxis(); ui->accPlot->graph(1)->rescaleValueAxis(true); ui->accPlot->graph(2)->rescaleValueAxis(true); // make key axis range scroll with the data (at a constant range size of 8): ui->accPlot->xAxis->setRange(keyPrescaler, memory, Qt::AlignRight); ui->accPlot->replot(); // remove data of lines that's outside visible range: ui->gyroPlot->graph(0)->removeDataBefore(keyPrescaler-memory); ui->gyroPlot->graph(1)->removeDataBefore(keyPrescaler-memory); ui->gyroPlot->graph(2)->removeDataBefore(keyPrescaler-memory); // rescale value (vertical) axis to fit the current data: ui->gyroPlot->graph(0)->rescaleValueAxis(); ui->gyroPlot->graph(1)->rescaleValueAxis(true); ui->gyroPlot->graph(2)->rescaleValueAxis(true); // make key axis range scroll with the data (at a constant range size of 8): ui->gyroPlot->xAxis->setRange(keyPrescaler, memory, Qt::AlignRight); ui->gyroPlot->replot(); keyCounter += newDataSize; MPS += newDataSize; qDebug()<<"keyCounter:"<<keyCounter; }
// This does a tri-linear interpolation of the derivatives at each of the 8 // grid points around the given point. This avoids additional calls to the // function and speeds things up by a factor of 2 or so. GLvoid MarchingCubes::surfaceNormal(GLvector &norm, GLfloat const& x, GLfloat const& y, GLfloat const& z) { // needs offloading to the Grid class GLvector V000, V001, V010, V011, V100, V101, V110, V111; GLfloat gx(x/m_stepSize); GLfloat gy(y/m_stepSize); GLfloat gz(z/m_stepSize); GLint x0( std::floor(gx) ); GLint y0( std::floor(gy) ); GLint z0( std::floor(gz) ); GLint x1(x0+1); GLint y1(y0+1); GLint z1(z0+1); int i = x0; int j = y0; int k = z0; V000.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V000.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V000.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x0; j = y0; k = z1; V001.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V001.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V001.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x0; j = y1; k = z0; V010.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V010.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V010.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x0; j = y1; k = z1; V011.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V011.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V011.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x1; j = y0; k = z0; V100.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V100.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V100.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x1; j = y0; k = z1; V101.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V101.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V101.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x1; j = y1; k = z0; V110.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V110.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V110.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); i = x1; j = y1; k = z1; V111.fX = (*m_grid)(i+1, j, k ) - (*m_grid)(i-1, j, k ); V111.fY = (*m_grid)(i, j+1, k ) - (*m_grid)(i, j-1, k ); V111.fZ = (*m_grid)(i, j, k+1) - (*m_grid)(i, j, k-1); GLvector p0; p0.fX = gx - x0; p0.fY = gy - y0; p0.fZ = gz - z0; GLvector p1; p1.fX = x1 - gx; p1.fY = y1 - gy; p1.fZ = z1 - gz; GLfloat dX, dY, dZ; dX = V000.fX * p1.fX * p1.fY * p1.fZ + V001.fX * p1.fX * p1.fY * p0.fZ + V010.fX * p1.fX * p0.fY * p1.fZ + V011.fX * p1.fX * p0.fY * p0.fZ + V100.fX * p0.fX * p1.fY * p1.fZ + V101.fX * p0.fX * p1.fY * p0.fZ + V110.fX * p0.fX * p0.fY * p1.fZ + V111.fX * p0.fX * p0.fY * p0.fZ; dY = V000.fY * p1.fX * p1.fY * p1.fZ + V001.fY * p1.fX * p1.fY * p0.fZ + V010.fY * p1.fX * p0.fY * p1.fZ + V011.fY * p1.fX * p0.fY * p0.fZ + V100.fY * p0.fX * p1.fY * p1.fZ + V101.fY * p0.fX * p1.fY * p0.fZ + V110.fY * p0.fX * p0.fY * p1.fZ + V111.fY * p0.fX * p0.fY * p0.fZ; dZ = V000.fZ * p1.fX * p1.fY * p1.fZ + V001.fZ * p1.fX * p1.fY * p0.fZ + V010.fZ * p1.fX * p0.fY * p1.fZ + V011.fZ * p1.fX * p0.fY * p0.fZ + V100.fZ * p0.fX * p1.fY * p1.fZ + V101.fZ * p0.fX * p1.fY * p0.fZ + V110.fZ * p0.fX * p0.fY * p1.fZ + V111.fZ * p0.fX * p0.fY * p0.fZ; norm.fX = -dX; norm.fY = -dY; norm.fZ = -dZ; normalizeVector(norm, norm); }
int main(int argc, char* argv[]) { //init MPI MPI_Init( &argc, &argv); int rank, size; MPI_Comm_rank( MPI_COMM_WORLD, &rank); MPI_Comm_size( MPI_COMM_WORLD, &size); //create a grid and some data if( size != 4){ std::cerr << "Please run with 4 threads!\n"; return -1;} double Tmax=2.*M_PI; double NT = 10; double dt = Tmax/NT; dg::Grid1d<double> gx( 0, 2.*M_PI, 3, 10); dg::Grid1d<double> gy( 0, 2.*M_PI, 3, 10); dg::Grid1d<double> gz( 0, 2.*M_PI, 1, 20); dg::Grid3d<double> g( gx, gy, gz); std::string hello = "Hello world\n"; thrust::host_vector<double> data = dg::evaluate( function, g); //create NetCDF File int ncid; file::NC_Error_Handle err; MPI_Info info = MPI_INFO_NULL; err = nc_create_par( "testmpi.nc", NC_NETCDF4|NC_MPIIO|NC_CLOBBER, MPI_COMM_WORLD, info, &ncid); err = nc_put_att_text( ncid, NC_GLOBAL, "input", hello.size(), hello.data()); int dimids[4], tvarID; err = file::define_dimensions( ncid, dimids, &tvarID, g); int dataID; err = nc_def_var( ncid, "data", NC_DOUBLE, 4, dimids, &dataID); /* Write metadata to file. */ err = nc_enddef(ncid); //err = nc_enddef( ncid); size_t start[4] = {0, rank*g.Nz()/size, 0, 0}; size_t count[4] = {1, g.Nz()/size, g.Ny()*g.n(), g.Nx()*g.n()}; if( rank==0) std::cout<< "Write from "<< start[0]<< " "<<start[1]<<" "<<start[2]<<" "<<start[3]<<std::endl; if( rank==0) std::cout<< "Number of elements "<<count[0]<< " "<<count[1]<<" "<<count[2]<<" "<<count[3]<<std::endl; err = nc_var_par_access(ncid, dataID , NC_COLLECTIVE); err = nc_var_par_access(ncid, tvarID , NC_COLLECTIVE); size_t Tcount=1, Tstart=0; double time = 0; //err = nc_close(ncid); for(unsigned i=0; i<=NT; i++) { if(rank==0)std::cout<<"Write timestep "<<i<<"\n"; //err = nc_open_par( "testmpi.nc", NC_WRITE|NC_MPIIO, MPI_COMM_WORLD, info, &ncid); //doesn't work I don't know why time = i*dt; Tstart = i; data = dg::evaluate( function, g); dg::blas1::scal( data, cos( time)); start[0] = i; //write dataset (one timeslice) err = nc_put_vara_double( ncid, dataID, start, count, data.data() + start[1]*count[2]*count[3]); err = nc_put_vara_double( ncid, tvarID, &Tstart, &Tcount, &time); //err = nc_close(ncid); } err = nc_close(ncid); MPI_Finalize(); return 0; }
int main(int argc, char* argv[]) { stream::zip::GZIP gzip(stream::zip::OPEN_FOR_COMPRESS); io::Blob<unsigned char> buffer; gzip.Init(); FILE* file = fopen("C:\\pru.gz","wb"); buffer = gzip.Update((unsigned char*)"Hoal chavalote",13); fwrite(buffer.get(),buffer.length(),1,file); buffer = gzip.Finish(); fwrite(buffer.get(),buffer.length(),1,file); fclose(file); /*stream::zip::GZOutputStream fileOut("C:\\pru.gz"); fileOut.write((unsigned char*)"Hoal chavalote",14); fileOut.close();*/ /*io::Blob<char> buff = stream::util::FileUtil::File2Buffer("C:\\pru.gz"); stream::zip::GZIP gzip2(stream::zip::OPEN_FOR_UNCOMPRESS); gzip2.Init(); buffer = gzip2.Update((unsigned char*)buff.get(),buff.length()-10); buffer = gzip2.Update((unsigned char*)buff.get()+buff.length()-10,10); buffer = gzip2.Finish();*/ io::FileInputStream fileIn("c:\\pru.gz"); zip::GZIPInputStream gz(zip::OPEN_FOR_UNCOMPRESS,&fileIn); while(gz.available()) { /*unsigned char buffer4[100]; int readed = gz.read(buffer4,100);*/ int c = gz.read(); } /* CoInitialize(NULL); LiteLog* log = util::LiteLog::getInstance(); log->traceOn("CON|HOla consola|5","Prueba"); log->trace(string("CON|HOla consola|5")); std::string va = SystemInfo::getIEVersion(); std::string s = SystemInfo::readRegistryString("HKEY_CURRENT_USER\\Software\\HermesWeb\\Configuracion\\PathDocs"); try { auto_ptr<Connection> connection = Connection::createConnection("DSN=BakeMail"); auto_ptr<PreparedStatement> statement = connection->prepareStatement("SELECT * FROM servers WHERE id_server >= ?"); statement->setLong(1,3); auto_ptr<ResultSet> result = statement->executeQuery(); result->moveFirst(); while(!result->eof()) { cout << result->getString("server") << endl; cout << result->getString("port") << endl; cout << result->getString("user") << endl; cout << result->getString("password") << endl; cout << result->getString("check2") << endl << endl; result->moveNext(); } result->close(); auto_ptr<PreparedStatement> statement2 = connection->prepareStatement("INSERT INTO servers VALUES('','servidor','puerto','usuario','password12','not check')"); statement2->execute(); auto_ptr<PreparedStatement> statement3 = connection->prepareStatement("INSERT INTO servers VALUES('','servidor','?','usuario','password12','not check')"); for(int i = 30 ; i < 32 ; i++) { statement3->setLong(1,i); statement3->execute(); } connection->close(); }catch(exceptions::StreamException* e) { cout << e->what() << endl; } CoUninitialize();*/ return 0; }
void grid_eval(cl::program_t& prog, size_t block0, size_t block1, size_t refine) { const size_t Nx = px.size(); const size_t Ny = py.size(); const size_t Nz = pz.size(); cl::ctx_t ctx(prog.context()); cl::queue_t queue(ctx, ctx.device(0)); init_mesh(); { cl::kernel_t kern = prog.kernel("grid"); boost::scoped_array<uint8_t> out(new uint8_t[block0 * block0 * block0]); boost::scoped_array<float> gx(new float[block0]); boost::scoped_array<float> gy(new float[block0]); boost::scoped_array<float> gz(new float[block0]); cl::mem_t gx_mem = ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL); cl::mem_t gy_mem = ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL); cl::mem_t gz_mem = ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL); cl::mem_t out_mem = ctx.create_buffer(CL_MEM_READ_WRITE, block0 * block0 * block0, NULL); const size_t Py = block0; const size_t Pz = block0 * block0; kern.arg(0, gx_mem); kern.arg(1, gy_mem); kern.arg(2, gz_mem); kern.arg(3, out_mem); kern.arg(4, Py); kern.arg(5, Pz); for (size_t _z = 0; _z < Nz; _z += block0 - 1) { const size_t Wz = std::min(block0, Nz - _z); for (size_t i = 0; i < Wz; ++i) gz[i] = (float)pz[_z + i]; queue.sync(cl::cmd_write_buffer(gz_mem, CL_FALSE, 0, Wz * sizeof(float), gz.get())); for (size_t _y = 0; _y < Ny; _y += block0 - 1) { const size_t Wy = std::min(block0, Ny - _y); for (size_t i = 0; i < Wy; ++i) gy[i] = (float)py[_y + i]; queue.sync(cl::cmd_write_buffer(gy_mem, CL_FALSE, 0, Wy * sizeof(float), gy.get())); for (size_t _x = 0; _x < Nx; _x += block0 - 1) { std::cerr << "block: " << _x << "," << _y << "," << _z << std::endl; const size_t Wx = std::min(block0, Nx - _x); for (size_t i = 0; i < Wx; ++i) gx[i] = (float)px[_x + i]; queue.sync(cl::cmd_write_buffer(gx_mem, CL_FALSE, 0, Wx * sizeof(float), gx.get())); queue.sync(cl::cmd_task(kern).wrk_sz(Wx, Wy, Wz)); queue.sync(cl::cmd_read_buffer( out_mem, CL_FALSE, 0, block0 * block0 * block0, out.get())); for (size_t z = 0; z < Wz - 1; ++z) { for (size_t y = 0; y < Wy - 1; ++y) { for (size_t x = 0; x < Wx - 1; ++x) { size_t bits = 0; if (out[(x + 0) + (y + 0) * Py + (z + 0) * Pz]) bits |= 0x01; if (out[(x + 1) + (y + 0) * Py + (z + 0) * Pz]) bits |= 0x02; if (out[(x + 0) + (y + 1) * Py + (z + 0) * Pz]) bits |= 0x04; if (out[(x + 1) + (y + 1) * Py + (z + 0) * Pz]) bits |= 0x08; if (out[(x + 0) + (y + 0) * Py + (z + 1) * Pz]) bits |= 0x10; if (out[(x + 1) + (y + 0) * Py + (z + 1) * Pz]) bits |= 0x20; if (out[(x + 0) + (y + 1) * Py + (z + 1) * Pz]) bits |= 0x40; if (out[(x + 1) + (y + 1) * Py + (z + 1) * Pz]) bits |= 0x80; const uint8_t* row = mc_table[bits]; size_t n_tri = *row++; if (n_tri == 0) continue; { for (size_t t = 0; t < n_tri; ++t) { uint8_t ea, eb, ec; size_t va, vb, vc; ea = *row++; eb = *row++; ec = *row++; va = get_intersection( decode_edge(_x + x, _y + y, _z + z, ea)); vb = get_intersection( decode_edge(_x + x, _y + y, _z + z, eb)); vc = get_intersection( decode_edge(_x + x, _y + y, _z + z, ec)); mesh->add_tri(va, vb, vc); } } } } } } } } } std::vector<std::pair<edgeint_t, size_t> > vert_calc; vert_calc.reserve(int_idx.size()); for (typename edgeint_map_t::iterator i = int_idx.begin(); i != int_idx.end(); ++i) { vert_calc.push_back(std::make_pair((*i).first, (*i).second)); } { boost::scoped_array<cl_float3> a(new cl_float3[block1]); boost::scoped_array<cl_float3> b(new cl_float3[block1]); boost::scoped_array<cl_float3> c(new cl_float3[block1]); cl::mem_t a_mem = ctx.create_buffer(CL_MEM_READ_ONLY, block1 * sizeof(cl_float3), NULL); cl::mem_t b_mem = ctx.create_buffer(CL_MEM_READ_ONLY, block1 * sizeof(cl_float3), NULL); cl::mem_t c_mem = ctx.create_buffer(CL_MEM_READ_WRITE, block1 * sizeof(cl_float3), NULL); cl::kernel_t kern = prog.kernel("chop"); kern.arg(0, a_mem); kern.arg(1, b_mem); kern.arg(2, c_mem); kern.arg(3, refine); for (size_t i = 0; i < vert_calc.size(); i += block1) { const size_t N = std::min(vert_calc.size() - i, block1); for (size_t j = 0; j < N; ++j) { edgeint_t& e = vert_calc[i + j].first; v3d_t a_pos = position_of_vertex(e.a[0], e.a[1], e.a[2]); v3d_t b_pos = position_of_vertex(e.b[0], e.b[1], e.b[2]); a[j].x = (float)a_pos.x; a[j].y = (float)a_pos.y; a[j].z = (float)a_pos.z; b[j].x = (float)b_pos.x; b[j].y = (float)b_pos.y; b[j].z = (float)b_pos.z; } queue.sync(cl::cmd_write_buffer(a_mem, CL_FALSE, 0, N * sizeof(cl_float3), a.get())); queue.sync(cl::cmd_write_buffer(b_mem, CL_FALSE, 0, N * sizeof(cl_float3), b.get())); queue.sync(cl::cmd_task(kern).wrk_sz(N)); queue.sync(cl::cmd_read_buffer(c_mem, CL_FALSE, 0, N * sizeof(cl_float3), c.get())); for (size_t j = 0; j < N; ++j) { mesh->vertices[vert_calc[i + j].second].pos = v3d_t::init(c[j].x, c[j].y, c[j].z); } } } }
double Pk(double k) /* P(k) power spectrum*/ { double A=8312175.553892076015472412; return (A * pow(BBKS(k),2.0) *pow(gz(0.0),2.0)* k ); }
double Dz(double z) /*D(Z) GrowthFactor */ { return (gz(z) / (gz(0.0) * (1.0 +z))); }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { // Initialize full objective function. int nx = 256; // Set spatial discretization. RealT alpha = 1.e-3; // Set penalty parameter. RealT nu = 1e-2; // Viscosity parameter. Objective_BurgersControl<RealT> obj(alpha,nx); // Initialize equality constraints EqualityConstraint_BurgersControl<RealT> con(nx,nu); Teuchos::ParameterList list; list.sublist("SimOpt").sublist("Solve").set("Residual Tolerance",1.e2*ROL::ROL_EPSILON); con.setSolveParameters(list); // Initialize iteration vectors. Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > gz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > yz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); for (int i=0; i<nx+2; i++) { (*z_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; (*yz_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; } ROL::StdVector<RealT> z(z_rcp); ROL::StdVector<RealT> gz(gz_rcp); ROL::StdVector<RealT> yz(yz_rcp); Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(&z,false); Teuchos::RCP<ROL::Vector<RealT> > gzp = Teuchos::rcp(&z,false); Teuchos::RCP<ROL::Vector<RealT> > yzp = Teuchos::rcp(&yz,false); Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > gu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > yu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); for (int i=0; i<nx; i++) { (*u_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; (*yu_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; } ROL::StdVector<RealT> u(u_rcp); ROL::StdVector<RealT> gu(gu_rcp); ROL::StdVector<RealT> yu(yu_rcp); Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > gup = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > yup = Teuchos::rcp(&yu,false); Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > l_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); ROL::StdVector<RealT> c(c_rcp); ROL::StdVector<RealT> l(l_rcp); ROL::Vector_SimOpt<RealT> x(up,zp); ROL::Vector_SimOpt<RealT> g(gup,gzp); ROL::Vector_SimOpt<RealT> y(yup,yzp); // Check derivatives. obj.checkGradient(x,x,y,true,*outStream); obj.checkHessVec(x,x,y,true,*outStream); con.checkApplyJacobian(x,y,c,true,*outStream); con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream); con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream); // Initialize reduced objective function. Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); ROL::StdVector<RealT> p(p_rcp); Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(&p,false); Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj = Teuchos::rcp(&obj,false); Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon = Teuchos::rcp(&con,false); ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,pp); // Check derivatives. robj.checkGradient(z,z,yz,true,*outStream); robj.checkHessVec(z,z,yz,true,*outStream); // Get parameter list. std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); parlist->sublist("Status Test").set("Gradient Tolerance",1.e-14); parlist->sublist("Status Test").set("Constraint Tolerance",1.e-14); parlist->sublist("Status Test").set("Step Tolerance",1.e-16); parlist->sublist("Status Test").set("Iteration Limit",1000); // Declare ROL algorithm pointer. Teuchos::RCP<ROL::Algorithm<RealT> > algo; // Run optimization with Composite Step. algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Composite Step",*parlist,false)); RealT zerotol = std::sqrt(ROL::ROL_EPSILON); z.zero(); con.solve(c,u,z,zerotol); c.zero(); l.zero(); algo->run(x, g, l, c, obj, con, true, *outStream); Teuchos::RCP<ROL::Vector<RealT> > zCS = z.clone(); zCS->set(z); // Run Optimization with Trust-Region algorithm. algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Trust Region",*parlist,false)); z.zero(); algo->run(z,robj,true,*outStream); // Check solutions. Teuchos::RCP<ROL::Vector<RealT> > err = z.clone(); err->set(*zCS); err->axpy(-1.,z); errorFlag += ((err->norm()) > 1.e-8) ? 1 : 0; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
double *BP_Test(double *resid, int obs, double** X, int nvar, bool InclConst) { DenseVector e(resid, obs, false), g(obs), sqvar(obs); DenseVector *x = new DenseVector [nvar]; int i = 0, j = 0; double ns = 0; // Assign x = X * X x[0].alloc(obs); for (j = 0; j < obs; j++) { x[0].setAt(j, 1.0); } for (i = 1; i < nvar; i++) { if (InclConst) { x[i].absorb(X[i], obs); } else { x[i].absorb(X[i - 1], obs); } ns = x[i].norm(); for (j = 0; j < obs; j++) { x[i].setAt(j, geoda_sqr(x[i].getValue(j) / sqrt(ns))); } } double **cov = new double * [nvar]; for (i = 0; i < nvar; i++) alloc(cov[i], nvar); // use SVD to compute inverse(Z'Z) // use dgesvd_ char jobu = 'S', jobvt = 'S'; long int m = obs, n = nvar; long int lda = obs, ldu = obs, ldvt = nvar, lwork = 5 * obs, info = 0; double *a = new double [obs * nvar]; double *s = new double [nvar]; double *u = new double [ldu * nvar]; double *vt = new double [ldvt * nvar]; double *work = new double [lwork]; for (i = 0; i < obs; i++) { for (j = 0; j < nvar; j++) { a[i + obs * j] = x[j].getValue(i); } } // use __WXMAC__ to call vecLib //#ifdef WORDS_BIGENDIAN #ifdef __WXMAC__MMM dgesvd_(&jobu, &jobvt, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, &info); #else dgesvd_(&jobu, &jobvt, (integer*)&m, (integer*)&n, (doublereal*)a, (integer*)&lda, (doublereal*)s, (doublereal*)u, (integer*)&ldu, (doublereal*)vt, (integer*)&ldvt, (doublereal*)work, (integer*)&lwork, (integer*)&info); #endif if (!info) { // (z'z)^(-1) = VW^(-2)V' for (i = 0; i < nvar; i++) { for (j = 0; j < nvar; j++) { for (m = 0; m < nvar; m++) { cov[i][j] += (vt[i * nvar + m] * vt[m + j * nvar]) / (s[m] * s[m]); } } } } else { // do nothing } double mse = e.norm() / obs; // compute gi for (j = 0; j < obs; j++) { double e2 = geoda_sqr(e.getValue(j)); g.setAt(j, e2 - mse); sqvar.setAt(j, geoda_sqr(e2 - mse)); } double mean = sqvar.sum() / obs; DenseVector gz(nvar), gzizz(nvar); for (i = 0; i < nvar; i++) gz.setAt(i, g.product(x[i])); // gz = g'z (1 x expl) gz.squareTimesColumn(gzizz, cov); // gzizz = g'z * inv(cov) double bp = gz.product(gzizz); // bp = g'z[(z'z)^-1]z'g double *rslt = new double [6]; rslt[0] = 1. / (2 * geoda_sqr(mse)) * bp; // Breusch-Pagan rslt[1] = InclConst ? nvar - 1 : nvar; rslt[2] = gammp(rslt[1] / 2.0, rslt[0] / 2.0); rslt[3] = (1.0 / mean) * bp; // Koenker-Basset rslt[4] = rslt[1]; rslt[5] = gammp(rslt[1] / 2.0, rslt[3] / 2.0); release(&x); release(&cov); return rslt; }