///////////////////////////////////////////////////////////// // // // Copyright (c) 2007-2017 by The University of Queensland // // Centre for Geoscience Computing // // http://earth.uq.edu.au/centre-geoscience-computing // // // // Primary Business: Brisbane, Queensland, Australia // // Licensed under the Open Software License version 3.0 // // http://www.apache.org/licenses/LICENSE-2.0 // // // ///////////////////////////////////////////////////////////// #include "MNTable3D.h" // --- STL includes --- #include using namespace std; using std::make_pair; // --- System includes --- #include using std::floor; using std::endl; using std::ceil; // --- IO includes --- #include #include using std::ifstream; int MNTable3D::s_output_style=0; double MNTable3D::s_small_value=1e-7; MNTable3D::MNTable3D():m_write_prec(10) {} /*! construct MNTable3D \param MinPt minimum point (z component ignored) \param MaxPt maximum point (z component ignored) \param cd cell dimension \param ngroups initial number of particle groups */ MNTable3D::MNTable3D(const Vector3& MinPt,const Vector3& MaxPt,double cd,unsigned int ngroups) { m_celldim=cd; m_ngroups=ngroups; m_min_pt=MinPt; // get number of cells Vector3 req_size=MaxPt-MinPt; // requested size m_nx=int(ceil(req_size.X()/m_celldim))+2; m_ny=int(ceil(req_size.Y()/m_celldim))+2; m_nz=int(ceil(req_size.Z()/m_celldim))+2; std::cout << "nx,ny,nz: " << m_nx << " , " << m_ny << " , " << m_nz << std::endl; // add padding m_origin=MinPt-Vector3(m_celldim,m_celldim,m_celldim); m_max_pt=m_min_pt+Vector3(m_celldim*(m_nx-2),m_celldim*(m_ny-2),m_celldim*(m_nz-2)); // allocate cell array m_data=new MNTCell[m_nx*m_ny*m_nz]; for(int i=0;i> x >> y >> z >> r; bool can_insert=insertChecked(Sphere(Vector3(x*scale,y*scale,z*scale),r*scale),0,tol); if(!can_insert){ std::cout << "couldn't insert particle : " << Vector3(x*scale,y*scale,z*scale) << " r= " << r*scale << std::endl; } else { count++; } } std::cout << "inserted particle count:" << count << std::endl; infile.close(); return true; } /*! Increase the number of particle groups. Do nothing if the number is already large enough \param ngroups the new number of particle groups */ void MNTable3D::GrowNGroups(unsigned int ngroups) { if(ngroups>m_ngroups){ m_ngroups=ngroups; for(int i=0;i0) && (ix0) && (iy0) && (izgetIndex(S.Center()); if((id!=-1) && (gidgetIndex(S.Center()); tol+=s_small_value; if((id!=-1) && (gid close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-tol,gid); if(close_spheres.size()==0){ m_data[id].insert(S,gid); res=true; if(m_bbx_tracking){ // if bounding box tracking is on, update bbx Vector3 pmin=S.Center()-Vector3(S.Radius(),S.Radius(),S.Radius()); Vector3 pmax=S.Center()+Vector3(S.Radius(),S.Radius(),S.Radius()); m_min_tbbx.X()=std::min(m_min_tbbx.X(),pmin.X()); m_min_tbbx.Y()=std::min(m_min_tbbx.Y(),pmin.Y()); m_min_tbbx.Z()=std::min(m_min_tbbx.Z(),pmin.Z()); m_max_tbbx.X()=std::max(m_max_tbbx.X(),pmax.X()); m_max_tbbx.Y()=std::max(m_max_tbbx.Y(),pmax.Y()); m_max_tbbx.Z()=std::max(m_max_tbbx.Z(),pmax.Z()); } } else res=false; } else { res=false; } return res; } /*! get the closest spheres to a given point \param p the point \param nmax the maximum number of spheres returned */ const multimap MNTable3D::getSpheresClosestTo(const Vector3& p,unsigned int nmax) const { multimap res; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ for(int k=-1;k<=1;k++){ Vector3 pos=p+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim); int idx=this->getIndex(pos); if(idx!=-1){ multimap smap=m_data[idx].getSpheresClosestTo(p,nmax); res.insert(smap.begin(),smap.end()); } } } } return res; } /*! get all spheres from one group within a given distance to a point \param P the point \param d max. distance between the point and the surface of the sphere \param gid the group id \warning no check for validity of gid */ const multimap MNTable3D::getSpheresFromGroupNear(const Vector3& P,double d,int gid) const { multimap res; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ for(int k=-1;k<=1;k++){ Vector3 np=P+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim); int idx=this->getIndex(np); if(idx!=-1){ multimap v=m_data[idx].getSpheresFromGroupNear(P,d,gid); res.insert(v.begin(),v.end()); } } } } return res; } /*! get the sphere within a given group closest to given sphere \param s the sphere \param gid the group */ const Sphere* MNTable3D::getClosestSphereFromGroup(const Sphere& s,int gid) const { const Sphere* res=NULL; int max_dim=m_nx>m_ny ? m_nx : m_ny; max_dim=m_nz>max_dim ? m_nz : max_dim; int found=max_dim; double dist=max_dim*m_celldim; // search within the same cell int idx=getIndex(s.Center()); const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,2.0*max_dim); if(v!=NULL){ double d=(v->Center()-s.Center()).norm()-s.Radius(); found=1; if(dCenter()-s.Center()).norm()-s.Radius(); found=range+1; if(dTag(); }else{ res=-1; } return res; } /*! Get the sphere within a given group closest to given sphere, using anisotropic weighted distance. \param s the sphere \param gid the group \param wx weighting of the x-component of the distance \param wy weighting of the y-component of the distance \param wz weighting of the z-component of the distance */ const Sphere* MNTable3D::getClosestSphereFromGroup(const Sphere& s,int gid,double wx, double wy, double wz) const { const Sphere* res=NULL; int max_dim=m_nx>m_ny ? m_nx : m_ny; max_dim=m_nz>max_dim ? m_nz : max_dim; int found=max_dim; double dist=max_dim*m_celldim; // search within the same cell int idx=getIndex(s.Center()); const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,2.0*max_dim,wx,wy,wz); if(v!=NULL){ double d=(v->Center()-s.Center()).wnorm2(wx,wy,wz); found=1; if(dCenter()-s.Center()).wnorm2(wx,wy,wz); if(d need to limit search range found=range+1; } } } } } } range++; } return res; } /*! Convenience function which calls getClosestSphereFromGroup, using anisotropic weighted distance and returns the tag of the found sphere or -1 if none is found. \param s the sphere \param gid the group \param wx weighting of the x-component of the distance \param wy weighting of the y-component of the distance \param wz weighting of the z-component of the distance */ int MNTable3D::getTagOfClosestSphereFromGroup(const Sphere& s,int gid, double wx, double wy, double wz) const { int res; const Sphere* sp=getClosestSphereFromGroup(s,gid,wx,wy,wz); if(sp!=NULL){ res=sp->Tag(); }else{ res=-1; } return res; } /*! check if sphere is insertable \param S the Sphere \param gid the group id */ bool MNTable3D::checkInsertable(const Sphere& S,unsigned int gid) { bool res; int id=this->getIndex(S.Center()); if((id!=-1) && (gid close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid); if(close_spheres.size()==0){ res=true; } else { res=false; // for(map::const_iterator iter=close_spheres.begin(); // iter!=close_spheres.end(); // iter++){ // std::cout << iter->first << " | " << *(iter->second) << std::endl; // } } } else { res=false; } return res; } /* get the sum of the area/volume of all particles in a group \param gid the group id */ double MNTable3D::getSumVolume(int gid) { double res=0.0; for(int i=1;i > bonds; if(((ii+jj+kk)==0)){ // intra-cell, not for boundary bonds=m_data[id].getBonds(gid,tol); } else if(id2>id){ // inter-cell bonds=m_data[id].getBonds(gid,tol,m_data[id2]); } for(vector >::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){ m_bonds[btag].insert(*iter); } } } } } } } } /*! generate bonds between particles with given tags of a group \param gid the group ID \param tol max. difference between bond length and equilibrium dist. \param btag bond tag \param tag1 \param tag2 */ void MNTable3D::generateBondsTagged(int gid,double tol,int btag,int tag1,int tag2) { std::cout << "MNTable3D::generateBondsTagged( " << gid << " , " << tol << " , " << btag << " , " << tag1 << " , " << tag2 << " )" << std::endl; // loop over all cells for(int i=1;i > bonds; if(((ii+jj+kk)==0)){ // intra-cell, not for boundary bonds=m_data[id].getBondsTagged(gid,tol,tag1,tag2); } else if(id2>id){ // inter-cell bonds=m_data[id].getBondsTagged(gid,tol,m_data[id2],tag1,tag2); } for(vector >::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){ m_bonds[btag].insert(*iter); } } } } } } } } /*! generate bonds between particles with identical particle tag of a group with a given probablility (0..1) \param gid the group ID \param tol max. difference between bond length and equilibrium dist. \param prob probablility of bond generation \param btag bond tag \param ptag particle tag \param mask */ void MNTable3D::generateRandomBonds(int gid,double tol, double prob, int btag,int ptag,int mask) { std::cout << "MNTable3D::generateRandomBonds( " << gid << " , " << tol << " , " << prob << " , " << btag << " , " << ptag << " , " << mask << " )" << std::endl; // loop over all cells for(int i=1;i > bonds; if((id==id2) && (i!=0) && (j!=0)&& (k!=0) ){ // intra-cell, not for boundary bonds=m_data[id].getBonds(gid,tol,ptag,mask); } else if(id2>id){ // inter-cell bonds=m_data[id].getBonds(gid,tol,m_data[id2],ptag,mask); } for(vector >::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){ double rn=((double)(rand())/(double)(RAND_MAX)); if(rn > same_bonds; vector > diff_bonds; if((id==id2) && (i!=0) && (j!=0) && (k!=0)){ // intra-cell, not for boundary same_bonds=m_data[id].getBondsSame(gid,tol); diff_bonds=m_data[id].getBondsDiff(gid,tol); } else if(id2>id){ // inter-cell same_bonds=m_data[id].getBondsSame(gid,tol,m_data[id2]); diff_bonds=m_data[id].getBondsDiff(gid,tol,m_data[id2]); } // insert intra-cluster bonds for(vector >::iterator iter=same_bonds.begin(); iter!=same_bonds.end(); iter++){ if(m_bonds[btag1].find(*iter)==m_bonds[btag1].end()){ m_bonds[btag1].insert(*iter); } } // insert inter-cluster bonds for(vector >::iterator iter=diff_bonds.begin(); iter!=diff_bonds.end(); iter++){ if(m_bonds[btag2].find(*iter)==m_bonds[btag2].end()){ m_bonds[btag2].insert(*iter); } } } } } } } } } /*! generate bonds taking a list of joint surfaces into account, i.e. bonds which cross one of the surface get tagged differently (according to the tag of the joint) \param Joints the set of joint surfaces \param gid the particle group id \param tol the bond generation tolerance, i.e. bonds up to length (equilibrium length*tol) get generated \param tag the tag for the bonds not crossing any surface */ void MNTable3D::generateBondsWithJointSet(const TriPatchSet& Joints,int gid,double tol,int tag) { std::cout << "MNTable3D::generateBondsWithJointSet( " << tag << " )" << std::endl; // loop over all cells for(int i=1;i bonds; if(((ii+jj+kk)==0)){ // intra-cell, not for boundary bonds=m_data[id].getBondsWithPos(gid,tol); } else if(id2>id){ // inter-cell bonds=m_data[id].getBondsWithPos(gid,tol,m_data[id2]); } for(vector::iterator iter=bonds.begin(); iter!=bonds.end(); iter++){ // check if bond crosses surface int jtag=Joints.isCrossing(iter->firstPos,iter->secondPos) ; if(jtag==-1){ m_bonds[tag].insert(make_pair(iter->first,iter->second)); } else { m_bonds[jtag].insert(make_pair(iter->first,iter->second)); } } } } } } } } } /*! insert bond between particles with given ID \param id1 id of 1st particle \param id2 id of 2nd particle \param btag bond tag */ void MNTable3D::insertBond(int id1,int id2,int btag) { if(id1 > >::const_iterator iter=T.m_bonds.begin(); * iter!=T.m_bonds.end(); * iter++){ * iter->clear(); * } */ m_bonds.clear(); } /*! get vector of pointers to all spheres of a group \param gid goup Id */ const vector MNTable3D::getAllSpheresFromGroup(int gid) const { vector res; for(int i=1;i cr=m_data[idx(i,j,k)].getAllSpheresFromGroup(gid); res.insert(res.end(),cr.begin(),cr.end()); } } } return res; } boost::python::list MNTable3D::getSphereListFromGroup(int gid) const { boost::python::list l; vector sphere_vector; sphere_vector = getAllSpheresFromGroup(gid); for (vector::iterator iter=sphere_vector.begin(); iter!= sphere_vector.end(); iter++) { l.append(*(*(iter))); } return l; } boost::python::list MNTable3D::getSphereListDist(const Vector3& pos, double dist, int gid) const { boost::python::list l; multimap sphere_map = getSpheresFromGroupNear(pos,dist,gid); for (map::iterator iter=sphere_map.begin(); iter!= sphere_map.end(); iter++) { l.append(*(iter->second)); } return l; } /*! Set output style \param style the output style: 0 = debug, 1 = .geo */ void MNTable3D::SetOutputStyle(int style) { MNTable3D::s_output_style=style; } /*! Output the content of a MNTable3D to an ostream. The output format depends on the value of MNTable3D::s_output_style (see MNTable3D::SetOutputStyle). If it is set to 0, output suitable for debugging is generated, if it is set to 1 output in the esys .geo format is generated. If MNTable3D::s_output_style is set to 2, the output format is VTK-XML. \param ost the output stream \param T the table */ ostream& operator << (ostream& ost,const MNTable3D& T) { if(MNTable3D::s_output_style==0){ // debug style // don't print padding MNTCell::SetOutputStyle(0); for(int i=0;i > >::const_iterator iter=T.m_bonds.begin(); iter!=T.m_bonds.end(); iter++){ nbonds+=iter->second.size(); } ost << nbonds << endl; for(map > >::const_iterator iter=T.m_bonds.begin(); iter!=T.m_bonds.end(); iter++){ for(set >::const_iterator v_iter=iter->second.begin(); v_iter!=iter->second.end(); v_iter++){ if(v_iter->first<=v_iter->second){ ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl; } else { ost << v_iter->second << " " << v_iter->first << " " << iter->first << endl; } } } ost << "EndConnect" << endl; } else if (MNTable3D::s_output_style==2){ // vtk-xml T.WriteAsVtkXml(ost); } return ost; } /*! tag all particles close to a plane \param P the plane \param dist max. distance \param tag the tag \param gid group Id */ void MNTable3D::tagParticlesAlongPlane(const Plane& P,double dist,int tag,unsigned int gid) { for(int i=1;i vs=m_data[idx(i,j,k)].getSpheresNearObject(&P,dist,gid); for(vector::iterator iter=vs.begin(); iter!=vs.end(); iter++){ (*iter)->setTag(tag); } } } } } /*! tag all particles inside a specified volume \param V the volume \param tag the tag \param gid group Id */ void MNTable3D::tagParticlesInVolume(const AVolume& V,int tag,unsigned int gid) { for(int i=1;i vs=m_data[idx(i,j,k)].getSpheresInVolume(&V,gid); for(vector::iterator iter=vs.begin(); iter!=vs.end(); iter++){ (*iter)->setTag(tag); } } } } } /*! tag all particles close to a plane \param P the plane \param dist max. distance \param the tag mask \param tag the tag \param gid group Id */ void MNTable3D::tagParticlesAlongPlaneWithMask(const Plane& P,double dist,int tag,int mask,unsigned int gid) { for(int i=1;i vs=m_data[idx(i,j,k)].getSpheresNearObject(&P,dist,gid); for(vector::iterator iter=vs.begin(); iter!=vs.end(); iter++){ int oldtag=(*iter)->Tag(); int newtag=(tag & mask) | (oldtag & ~mask); (*iter)->setTag(newtag); } } } } } /*! tag all particles close to a joint set \param T the joint set \param dist max. distance \param the tag mask \param tag the tag \param gid group Id */ void MNTable3D::tagParticlesAlongJoints(const TriPatchSet& T,double dist,int tag, int mask, unsigned int gid) { for(int i=1;i::const_iterator iter=T.triangles_begin(); iter!=T.triangles_end(); iter++){ vector vs=m_data[idx(i,j,k)].getSpheresNearObject(&(*iter),dist,gid); for(vector::iterator iter=vs.begin(); iter!=vs.end(); iter++){ int oldtag=(*iter)->Tag(); int newtag=(tag & mask) | (oldtag & ~mask); (*iter)->setTag(newtag); } } } } } } /*! tag all particles fully within a sphere \param S the sphere \param tag the tag \param gid group Id */ void MNTable3D::tagParticlesInSphere(const Sphere &S,int tag,unsigned int gid) { int range=int(ceil(S.Radius()/m_celldim)); for(int i=-1*range;i<=range;i++){ for(int j=-1*range;j<=range;j++){ for(int k=-1*range;k<=range;k++){ Vector3 np=S.Center()+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim); int idx=this->getIndex(np); if(idx!=-1){ multimap v=m_data[idx].getSpheresFromGroupNearNC(S.Center(),S.Radius(),gid); for(multimap::iterator iter=v.begin(); iter!=v.end(); iter++){ double outer_dist=(iter->first+(iter->second)->Radius()); if(outer_dist<=S.Radius()) (iter->second)->setTag(tag); } } } } } } /*! tag the spheres in a group A according to the tag of the closest spheres from a another group B \param gid1 the group Id A \param gid1 the group Id B */ void MNTable3D::tagParticlesToClosest(int gid1, int gid2) { for(int i=0;i v=m_data[idx(i,j,k)].getAllSpheresFromGroupNC(gid1); for(vector::iterator iter=v.begin(); iter!=v.end(); iter++){ int t=getTagOfClosestSphereFromGroup(*(*iter),gid2); (*iter)->setTag(t); } } } } } /*! Tag the spheres in a group A according to the tag of the closest spheres from a another group B, using an anisotropic distance measure \param gid1 the group Id A \param gid1 the group Id B \param wx weighting of the x-component of the distance \param wy weighting of the y-component of the distance \param wz weighting of the z-component of the distance */ void MNTable3D::tagParticlesToClosestAnisotropic(int gid1, int gid2, double wx, double wy,double wz) { for(int i=0;i v=m_data[idx(i,j,k)].getAllSpheresFromGroupNC(gid1); for(vector::iterator iter=v.begin(); iter!=v.end(); iter++){ int t=getTagOfClosestSphereFromGroup(*(*iter),gid2,wx,wy,wz); (*iter)->setTag(t); } } } } } /* tag all particles within a group \param gid the group Id \param tag the tag \param mask the tag mask (i.e. new_tag = new_tag=(old_tag & (~mask)) | (tag & mask)) */ void MNTable3D::tagParticlesInGroup(int gid, int tag, int mask) { for(int i=1;iparticle id map inv_map; int pcount=0; for(int i=1;i ids=m_data[idx(i,j,k)].getIdList(); for(vector::iterator iter=ids.begin(); iter!=ids.end(); iter++){ inv_map.insert(make_pair(*iter,pcount)); pcount++; } } } } // get bond count int bcount=0; for(map >::const_iterator iter=m_bonds.begin(); iter!=m_bonds.end(); iter++){ bcount+=iter->second.size(); } // write header vtkfile << "\n"; vtkfile << "\n"; int nparts=0; for(int i=1;i\n"; // write particle pos vtkfile << "\n"; vtkfile << "\n"; for(int i=1;i\n"; vtkfile << "\n"; // --- write particle data --- // radius vtkfile << "\n"; vtkfile << "\n"; for(int i=1;i\n"; // ID vtkfile << "\n"; for(int i=1;i\n"; // Tag vtkfile << "\n"; for(int i=1;i\n"; vtkfile << "\n"; // --- write cells section --- // connections vtkfile << "\n"; vtkfile << "\n"; for(map >::const_iterator iter=m_bonds.begin(); iter!=m_bonds.end(); iter++){ for(set::const_iterator iter_b=iter->second.begin(); iter_b!=iter->second.end(); iter_b++){ vtkfile << inv_map[iter_b->first] << " " << inv_map[iter_b->second] << endl; } } vtkfile << "\n"; // offsets vtkfile << "\n"; for (size_t i = 1; i < bcount*2; i+=2) vtkfile << i+1 << "\n"; vtkfile << "\n"; // element type vtkfile << "\n"; const int CELL_LINE_TYPE = 3; for (size_t i = 0; i < bcount; i++) vtkfile << CELL_LINE_TYPE << endl; vtkfile << "\n"; vtkfile << "\n"; // cell data vtkfile << "\n"; vtkfile << "\n"; for(map >::const_iterator iter=m_bonds.begin(); iter!=m_bonds.end(); iter++){ for(set::const_iterator iter_b=iter->second.begin(); iter_b!=iter->second.end(); iter_b++){ vtkfile << iter->first << endl; } } vtkfile << "\n"; vtkfile << "\n"; // write footer vtkfile << "\n"; vtkfile << "\n"; vtkfile << "\n"; } /*! Set output precision \param prec number of significant digits */ void MNTable3D::SetOutputPrecision(int prec) { m_write_prec=prec; } void MNTable3D::write(char *filename, int outputStyle) { ofstream outFile; outFile.precision(m_write_prec); SetOutputStyle(outputStyle); outFile.open(filename); outFile << *(this); outFile.close(); } // --- block writing --- void MNTable3D::initBlockWriting(const string& filename) { m_outfilename=filename; m_temp_bondfilename=filename+string("_tmp_bond"); m_block_particles_written=0; m_block_bonds_written=0; // write header ofstream ost(filename.c_str()); ost << "LSMGeometry 1.2" << endl; if(m_write_tight_bbx & m_has_tight_bbx){ ost << "BoundingBox " << m_min_tbbx << " " << m_max_tbbx << endl; } else { ost << "BoundingBox " << m_min_pt << " " << m_max_pt << endl; } ost << "PeriodicBoundaries " << m_x_periodic << " " << m_y_periodic << " " << m_z_periodic << endl; ost << "Dimension 3D" << endl; // particles ost << "BeginParticles" << endl; ost << "Simple" << endl; m_np_write_pos=ost.tellp(); ost << " " << endl; // IMPORTANT: 10 spaces as placeholder for the number of particles std::cout << m_np_write_pos << std::endl; m_is_writing_blocks=true; } void MNTable3D::writeBlock(const Vector3& min_pt,const Vector3& max_pt) { ofstream ost(m_outfilename.c_str(),ios::app); // open file in append mode ost.precision(m_write_prec); for(int i=1;i > >::const_iterator iter=m_bonds.begin(); iter!=m_bonds.end(); iter++){ for(set >::const_iterator v_iter=iter->second.begin(); v_iter!=iter->second.end(); v_iter++){ if(v_iter->first<=v_iter->second){ ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl; } else { ost << v_iter->second << " " << v_iter->first << " " << iter->first << endl; } m_block_bonds_written++; } } ost.close(); std::cout << "bonds written: " << m_block_bonds_written << std::endl; } void MNTable3D::finishBlockWriting() { ofstream ost(m_outfilename.c_str(),ios::in | ios::out | ios::ate); // open for writing, initially positioned at the end // -- write end of particles -- ost << "EndParticles" << endl; // -- write bonds -- ost << "BeginConnect" << endl; ost << m_block_bonds_written << endl; ifstream ifs(m_temp_bondfilename.c_str()); ost << ifs.rdbuf(); ifs.close(); ost << "EndConnect" << endl; // -- reposition to the place where nr. of particles should be ost.seekp(m_np_write_pos); // overwrite placeholders ost << m_block_particles_written; // close file ost.close(); }