Clusters* calculate_clusters(const arma::mat& dataset, double r, size_t d1, size_t d2) { Log::Info << "Starting clustering with parameters - " << "dimension1: " << d1 << ", dimension2: " << d2 << ", resolution: " << r << endl; size_t m = dataset.n_rows; size_t dim = 2; // 2 axis std::vector<arma::uvec*> col_sort_ind; for(size_t ax=0; ax<dim; ax++) { arma::colvec col_cur = dataset.unsafe_col(ax); col_sort_ind.push_back(new arma::uvec(arma::sort_index(col_cur, 0))); } arma::vec dens(m); // density for each point arma::uvec clusts(m); //id clust for each point dens.zeros(); clusts.zeros(); size_t clust_id=0; // start clusters count from 1, 0 means no cluster for(int ax=0; ax<1; ax++) { for(size_t i=0; i<(m-1); i++) { turn_iteration(i,col_sort_ind,dataset,ax,r,clusts,clust_id); ; } } Log::Info << "Clustering is perfomed. Was found " << clust_id << " clusters." << std::endl; Clusters *cl_full_info = new Clusters(clusts ,m, clust_id); string out = boost::str(boost::format("out_%2.2f.csv") % r ); string out2 = boost::str(boost::format("out_size_%2.2f.csv") % r); data::Save(out.c_str(),cl_full_info->clust_ind,false,false); data::Save(out2.c_str(),cl_full_info->clust_size,false,false); return cl_full_info; }
void TwoPowerTriaxialPotentialxyzforces_xyz(double x,double y, double z, double * Fx, double * Fy, double * Fz,double *args, double a, double alpha, double beta, double b2, double c2, bool aligned, double * rot, int glorder, double * glx, double * glw){ int ii; double t; double td; *args= x; *(args + 1)= y; *(args + 2)= z; if ( !aligned ) rotate(&x,&y,&z,rot); *Fx= 0.; *Fy= 0.; *Fz= 0.; for (ii=0; ii < glorder; ii++) { t= 1. / *(glx+ii) / *(glx+ii) - 1.; td= *(glw+ii) * dens( sqrt ( x * x / ( 1. + t ) + y * y / ( b2 + t ) \ + z * z / ( c2 + t ) ) / a,alpha,beta); *Fx+= td * x / ( 1. + t ); *Fy+= td * y / ( b2 + t ); *Fz+= td * z / ( c2 + t ); } *(args + 3)= *Fx; *(args + 4)= *Fy; *(args + 5)= *Fz; }
void FEMAssemble2DElasticity(MPI_Comm comm, Mesh *mesh, Mat &A, Vec &b, PetscReal E, PetscReal mi, PetscReal(*dens)(Element *), void(*f)(Element*, PetscReal, PetscReal*)) { PetscInt rank; MPI_Comm_rank(comm, &rank); PetscInt size = mesh->vetrices.size(); MatCreateSeqAIJ(PETSC_COMM_SELF, size * 2, size * 2, 14, PETSC_NULL, &A); VecCreateMPI(comm, size * 2, PETSC_DECIDE, &b); for (std::map<PetscInt, Element*>::iterator e = mesh->elements.begin(); e != mesh->elements.end(); e++) { Point* vetrices[3]; PetscInt vIndex[3]; for (int i = 0; i < 3; i++) { vIndex[i] = e->second->vetrices[i]; vetrices[i] = mesh->vetrices[vIndex[i]]; } PetscReal lStiff[36]; PetscReal bLoc[6]; //Local stiffness matrix and force vector assembly PetscReal fs[2]; f(e->second, dens(e->second), fs); elastLoc(vetrices, E, mi, fs, lStiff, bLoc); PetscInt idx[6], idxLoc[6]; for (int i = 0; i < 3; i++) { idx[i * 2] = vIndex[i] * 2; idx[i * 2 + 1] = vIndex[i] * 2 + 1; idxLoc[i * 2] = vIndex[i] * 2 - mesh->startIndexes[rank] * 2; idxLoc[i * 2 + 1] = vIndex[i] * 2 + 1 - mesh->startIndexes[rank] * 2; } MatSetValues(A, 6, idxLoc, 6, idxLoc, lStiff, ADD_VALUES); VecSetValues(b, 6, idx, bLoc, ADD_VALUES); } VecAssemblyBegin(b); VecAssemblyEnd(b); MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); }
std::vector<T> readHDF5_Density(std::string const & ifn) { H5DensityReader reader(ifn); int fN = reader.sizeOfFile(); H5DensityReader::Value_t * readMem = new H5DensityReader::Value_t[fN]; reader.read(readMem); std::vector<T> dens(fN, 0.); for(int i=0; i<fN; i++) { dens[i] = T(readMem[i]); } delete[] readMem; return dens; }
double MoveAgeClonal::logQrat(double tfrom,double tto) { // return(log((exp(logdens(tto,tfrom))+exp(logdens(tto,-tfrom)))/(exp(logdens(tfrom,tto))+exp(logdens(tfrom,-tto))))); // less efficient return(log((dens(tto,tfrom)+dens(tto,-tfrom))/(dens(tfrom,tto)+dens(tfrom,-tto)))); }
void Density::execute(int num, //input //Buffer<float4>& svars, Buffer<float4>& pos_s, Buffer<float>& dens_s, //output Buffer<unsigned int>& ci_start, Buffer<unsigned int>& ci_end, //params Buffer<SPHParams>& sphp, Buffer<GridParams>& gp, //debug params Buffer<float4>& clf_debug, Buffer<int4>& cli_debug) { int iarg = 0; //k_density.setArg(iarg++, svars.getDevicePtr()); k_density.setArg(iarg++, pos_s.getDevicePtr()); k_density.setArg(iarg++, dens_s.getDevicePtr()); k_density.setArg(iarg++, ci_start.getDevicePtr()); k_density.setArg(iarg++, ci_end.getDevicePtr()); k_density.setArg(iarg++, gp.getDevicePtr()); k_density.setArg(iarg++, sphp.getDevicePtr()); // ONLY IF DEBUGGING k_density.setArg(iarg++, clf_debug.getDevicePtr()); k_density.setArg(iarg++, cli_debug.getDevicePtr()); int local = 64; try { float gputime = k_density.execute(num, local); if(gputime > 0) timer->set(gputime); } catch (cl::Error er) { printf("ERROR(density): %s(%s)\n", er.what(), CL::oclErrorString(er.err())); } #if 0 //printouts //DEBUGING if(num > 0)// && choice == 0) { printf("============================================\n"); printf("which == %d *** \n", choice); printf("***** PRINT neighbors diagnostics ******\n"); printf("num %d\n", num); std::vector<int4> cli(num); std::vector<float4> clf(num); cli_debug.copyToHost(cli); clf_debug.copyToHost(clf); std::vector<float4> poss(num); std::vector<float4> dens(num); for (int i=0; i < num; i++) //for (int i=0; i < 10; i++) { //printf("-----\n"); printf("clf_debug: %f, %f, %f, %f\n", clf[i].x, clf[i].y, clf[i].z, clf[i].w); //if(clf[i].w == 0.0) exit(0); //printf("cli_debug: %d, %d, %d, %d\n", cli[i].x, cli[i].y, cli[i].z, cli[i].w); // printf("pos : %f, %f, %f, %f\n", pos[i].x, pos[i].y, pos[i].z, pos[i].w); } } #endif }
REAL pden(REAL s, REAL t, REAL p, REAL pr){ REAL pt; pt = ptmp(s, t, p, pr); return dens(s, pt, pr); }
std::vector<double> Opm::WellDensitySegmented::computeConnectionDensities(const Wells& wells, const WellStateFullyImplicitBlackoil& wstate, const PhaseUsage& phase_usage, const std::vector<double>& b_perf, const std::vector<double>& rsmax_perf, const std::vector<double>& rvmax_perf, const std::vector<double>& surf_dens_perf) { // Verify that we have consistent input. const int np = wells.number_of_phases; const int nw = wells.number_of_wells; const int nperf = wells.well_connpos[nw]; if (wells.number_of_phases != phase_usage.num_phases) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. phase_usage."); } if (nperf*np != int(surf_dens_perf.size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. surf_dens."); } if (nperf*np != int(wstate.perfPhaseRates().size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. wstate."); } if (nperf*np != int(b_perf.size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. b_perf."); } if ((!rsmax_perf.empty()) || (!rvmax_perf.empty())) { // Need both oil and gas phases. if (!phase_usage.phase_used[BlackoilPhases::Liquid]) { OPM_THROW(std::logic_error, "Oil phase inactive, but non-empty rsmax_perf or rvmax_perf."); } if (!phase_usage.phase_used[BlackoilPhases::Vapour]) { OPM_THROW(std::logic_error, "Gas phase inactive, but non-empty rsmax_perf or rvmax_perf."); } } // 1. Compute the flow (in surface volume units for each // component) exiting up the wellbore from each perforation, // taking into account flow from lower in the well, and // in/out-flow at each perforation. std::vector<double> q_out_perf(nperf*np); for (int w = 0; w < nw; ++w) { // Iterate over well perforations from bottom to top. for (int perf = wells.well_connpos[w+1] - 1; perf >= wells.well_connpos[w]; --perf) { for (int phase = 0; phase < np; ++phase) { if (perf == wells.well_connpos[w+1] - 1) { // This is the bottom perforation. No flow from below. q_out_perf[perf*np + phase] = 0.0; } else { // Set equal to flow from below. q_out_perf[perf*np + phase] = q_out_perf[(perf+1)*np + phase]; } // Subtract outflow through perforation. q_out_perf[perf*np + phase] -= wstate.perfPhaseRates()[perf*np + phase]; } } } // 2. Compute the component mix at each perforation as the // absolute values of the surface rates divided by their sum. // Then compute volume ratios (formation factors) for each perforation. // Finally compute densities for the segments associated with each perforation. const int gaspos = phase_usage.phase_pos[BlackoilPhases::Vapour]; const int oilpos = phase_usage.phase_pos[BlackoilPhases::Liquid]; std::vector<double> mix(np); std::vector<double> x(np); std::vector<double> surf_dens(np); std::vector<double> dens(nperf); for (int w = 0; w < nw; ++w) { for (int perf = wells.well_connpos[w]; perf < wells.well_connpos[w+1]; ++perf) { // Find component mix. const double tot_surf_rate = std::accumulate(q_out_perf.begin() + np*perf, q_out_perf.begin() + np*(perf+1), 0.0); if (tot_surf_rate != 0.0) { for (int phase = 0; phase < np; ++phase) { mix[phase] = std::fabs(q_out_perf[perf*np + phase]/tot_surf_rate); } } else { // No flow => use well specified fractions for mix. std::copy(wells.comp_frac + w*np, wells.comp_frac + (w+1)*np, mix.begin()); } // Compute volume ratio. x = mix; double rs = 0.0; double rv = 0.0; if (!rsmax_perf.empty() && mix[oilpos] > 0.0) { rs = std::min(mix[gaspos]/mix[oilpos], rsmax_perf[perf]); } if (!rvmax_perf.empty() && mix[gaspos] > 0.0) { rv = std::min(mix[oilpos]/mix[gaspos], rvmax_perf[perf]); } if (rs != 0.0) { // Subtract gas in oil from gas mixture x[gaspos] = (mix[gaspos] - mix[oilpos]*rs)/(1.0 - rs*rv); } if (rv != 0.0) { // Subtract oil in gas from oil mixture x[oilpos] = (mix[oilpos] - mix[gaspos]*rv)/(1.0 - rs*rv);; } double volrat = 0.0; for (int phase = 0; phase < np; ++phase) { volrat += x[phase] / b_perf[perf*np + phase]; } for (int phase = 0; phase < np; ++phase) { surf_dens[phase] = surf_dens_perf[perf*np + phase]; } // Compute segment density. dens[perf] = std::inner_product(surf_dens.begin(), surf_dens.end(), mix.begin(), 0.0) / volrat; } } return dens; }
std::vector<double> Opm::WellDensitySegmented::computeConnectionPressureDelta(const Wells& wells, const WellStateFullyImplicitBlackoil& wstate, const PhaseUsage& phase_usage, const std::vector<double>& b_perf, const std::vector<double>& rsmax_perf, const std::vector<double>& rvmax_perf, const std::vector<double>& z_perf, const std::vector<double>& surf_dens, const double gravity) { // Verify that we have consistent input. const int np = wells.number_of_phases; const int nw = wells.number_of_wells; const int nperf = wells.well_connpos[nw]; if (wells.number_of_phases != phase_usage.num_phases) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. phase_usage."); } if (surf_dens.size() != size_t(wells.number_of_phases)) { OPM_THROW(std::logic_error, "Inconsistent input: surf_dens vs. phase_usage."); } if (nperf*np != int(wstate.perfPhaseRates().size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. wstate."); } if (nperf*np != int(b_perf.size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. b_perf."); } if (nperf != int(z_perf.size())) { OPM_THROW(std::logic_error, "Inconsistent input: wells vs. z_perf."); } if ((!rsmax_perf.empty()) || (!rvmax_perf.empty())) { // Need both oil and gas phases. if (!phase_usage.phase_used[BlackoilPhases::Liquid]) { OPM_THROW(std::logic_error, "Oil phase inactive, but non-empty rsmax_perf or rvmax_perf."); } if (!phase_usage.phase_used[BlackoilPhases::Vapour]) { OPM_THROW(std::logic_error, "Gas phase inactive, but non-empty rsmax_perf or rvmax_perf."); } } // Algorithm: // We'll assume the perforations are given in order from top to // bottom for each well. By top and bottom we do not necessarily // mean in a geometric sense (depth), but in a topological sense: // the 'top' perforation is nearest to the surface topologically. // Our goal is to compute a pressure delta for each perforation. // 1. Compute the flow (in surface volume units for each // component) exiting up the wellbore from each perforation, // taking into account flow from lower in the well, and // in/out-flow at each perforation. std::vector<double> q_out_perf(nperf*np); for (int w = 0; w < nw; ++w) { // Iterate over well perforations from bottom to top. for (int perf = wells.well_connpos[w+1] - 1; perf >= wells.well_connpos[w]; --perf) { for (int phase = 0; phase < np; ++phase) { if (perf == wells.well_connpos[w+1] - 1) { // This is the bottom perforation. No flow from below. q_out_perf[perf*np + phase] = 0.0; } else { // Set equal to flow from below. q_out_perf[perf*np + phase] = q_out_perf[(perf+1)*np + phase]; } // Subtract outflow through perforation. q_out_perf[perf*np + phase] -= wstate.perfPhaseRates()[perf*np + phase]; } } } // 2. Compute the component mix at each perforation as the // absolute values of the surface rates divided by their sum. // Then compute volume ratios (formation factors) for each perforation. // Finally compute densities for the segments associated with each perforation. const int gaspos = phase_usage.phase_pos[BlackoilPhases::Vapour]; const int oilpos = phase_usage.phase_pos[BlackoilPhases::Liquid]; std::vector<double> mix(np); std::vector<double> x(np); std::vector<double> dens(nperf); for (int w = 0; w < nw; ++w) { for (int perf = wells.well_connpos[w]; perf < wells.well_connpos[w+1]; ++perf) { // Find component mix. const double tot_surf_rate = std::accumulate(q_out_perf.begin() + np*perf, q_out_perf.begin() + np*(perf+1), 0.0); if (tot_surf_rate != 0.0) { for (int phase = 0; phase < np; ++phase) { mix[phase] = std::fabs(q_out_perf[perf*np + phase]/tot_surf_rate); } } else { // No flow => use well specified fractions for mix. std::copy(wells.comp_frac + w*np, wells.comp_frac + (w+1)*np, mix.begin()); } // Compute volume ratio. x = mix; double rs = 0.0; double rv = 0.0; if (!rsmax_perf.empty() && mix[oilpos] > 0.0) { rs = std::min(mix[gaspos]/mix[oilpos], rsmax_perf[perf]); } if (!rvmax_perf.empty() && mix[gaspos] > 0.0) { rv = std::min(mix[oilpos]/mix[gaspos], rvmax_perf[perf]); } if (rs != 0.0) { // Subtract gas in oil from gas mixture x[gaspos] = (mix[gaspos] - mix[oilpos]*rs)/(1.0 - rs*rv); } if (rv != 0.0) { // Subtract oil in gas from oil mixture x[oilpos] = (mix[oilpos] - mix[gaspos]*rv)/(1.0 - rs*rv);; } double volrat = 0.0; for (int phase = 0; phase < np; ++phase) { volrat += x[phase] / b_perf[perf*np + phase]; } // Compute segment density. dens[perf] = std::inner_product(surf_dens.begin(), surf_dens.end(), mix.begin(), 0.0) / volrat; } } // 3. Compute pressure differences between perforations. // dp_perf will contain the pressure difference between a // perforation and the one above it, except for the first // perforation for each well, for which it will be the // difference to the reference (bhp) depth. std::vector<double> dp_perf(nperf); for (int w = 0; w < nw; ++w) { for (int perf = wells.well_connpos[w]; perf < wells.well_connpos[w+1]; ++perf) { const double z_above = perf == wells.well_connpos[w] ? wells.depth_ref[w] : z_perf[perf - 1]; const double dz = z_perf[perf] - z_above; dp_perf[perf] = dz * dens[perf] * gravity; } } // 4. Compute pressure differences to the reference point (bhp) by // accumulating the already computed adjacent pressure // differences, storing the result in dp_perf. // This accumulation must be done per well. for (int w = 0; w < nw; ++w) { const auto beg = dp_perf.begin() + wells.well_connpos[w]; const auto end = dp_perf.begin() + wells.well_connpos[w + 1]; std::partial_sum(beg, end, beg); } return dp_perf; }
void RigidBodyForce::execute(int num, Buffer<float>& density, Buffer<float4>& pos_s, Buffer<float4>& veleval_s, Buffer<float4>& force_s, Buffer<float>& mass_s, Buffer<float4>& rb_pos_s, Buffer<float4>& rb_velocity_s, Buffer<float>& rb_mass_s, Buffer<unsigned int>& ci_start, Buffer<unsigned int>& ci_end, //params Buffer<SPHParams>& sphp, Buffer<GridParams>& gp, float stiffness, float dampening, float friction_dynamic, float friction_static, float friction_static_threshold, //debug params Buffer<float4>& clf_debug, Buffer<int4>& cli_debug) { int iarg = 0; k_rigidbody_force.setArg(iarg++, density.getDevicePtr()); k_rigidbody_force.setArg(iarg++, pos_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, veleval_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, force_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, mass_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, rb_pos_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, rb_velocity_s.getDevicePtr()); k_rigidbody_force.setArg(iarg++, rb_mass_s.getDevicePtr()); float16 rbParams; rbParams.m[0]=stiffness; rbParams.m[1]=dampening; rbParams.m[2]=friction_dynamic; rbParams.m[3]=friction_static; rbParams.m[4]=friction_static_threshold; k_rigidbody_force.setArg(iarg++, rbParams); k_rigidbody_force.setArg(iarg++, ci_start.getDevicePtr()); k_rigidbody_force.setArg(iarg++, ci_end.getDevicePtr()); k_rigidbody_force.setArg(iarg++, gp.getDevicePtr()); k_rigidbody_force.setArg(iarg++, sphp.getDevicePtr()); // ONLY IF DEBUGGING k_rigidbody_force.setArg(iarg++, clf_debug.getDevicePtr()); k_rigidbody_force.setArg(iarg++, cli_debug.getDevicePtr()); int local = 64; try { float gputime = k_rigidbody_force.execute(num, local); if(gputime > 0) timer->set(gputime); } catch (cl::Error er) { printf("ERROR(rigidbody force ): %s(%s)\n", er.what(), CL::oclErrorString(er.err())); } #if 0 //printouts //DEBUGING if(num > 0)// && choice == 0) { printf("============================================\n"); printf("which == %d *** \n", choice); printf("***** PRINT neighbors diagnostics ******\n"); printf("num %d\n", num); std::vector<int4> cli(num); std::vector<float4> clf(num); cli_debug.copyToHost(cli); clf_debug.copyToHost(clf); std::vector<float4> poss(num); std::vector<float4> dens(num); for (int i=0; i < num; i++) //for (int i=0; i < 10; i++) { //printf("-----\n"); printf("clf_debug: %f, %f, %f, %f\n", clf[i].x, clf[i].y, clf[i].z, clf[i].w); //if(clf[i].w == 0.0) exit(0); //printf("cli_debug: %d, %d, %d, %d\n", cli[i].x, cli[i].y, cli[i].z, cli[i].w); //printf("pos : %f, %f, %f, %f\n", pos[i].x, pos[i].y, pos[i].z, pos[i].w); } } #endif }