//------------------------------------------------------------------------------ bool OBOpenDXCubeFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv) { OBMol* pmol = dynamic_cast<OBMol*>(pOb); if(pmol==NULL) return false; ostream &ofs = *pConv->GetOutStream(); OBMol &mol = *pmol; char buffer[BUFF_SIZE]; string str; stringstream errorMsg; OBGridData *gd = (OBGridData*)mol.GetData(OBGenericDataType::GridData); if (gd == NULL) { errorMsg << "The molecule has no grid."; obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obWarning); return false; } // APBS-style OpenDX Multigrid // First some comments ofs << "# Data from Open Babel " << BABEL_VERSION << "\n"; str = mol.GetTitle(); if (str.empty()) ofs << "# Molecule Title: *****" << "\n"; else ofs << "# Molecule Title: " << str << "\n"; int nx, ny, nz; double origin[3], xAxis[3], yAxis[3], zAxis[3]; gd->GetAxes(xAxis, yAxis, zAxis); gd->GetNumberOfPoints(nx, ny, nz); gd->GetOriginVector(origin); // data line 1: # of points in x, y, z (nx, ny, nz) snprintf(buffer, BUFF_SIZE, "object 1 class gridpositions counts %5d %5d %5d", nx, ny, nz); ofs << buffer << "\n"; // data line 2: origin (x, y, z) snprintf(buffer, BUFF_SIZE,"origin %12.6f %12.6f %12.6f", origin[0], origin[1], origin[2]); ofs << buffer << "\n"; // data line 3: x-displacement snprintf(buffer, BUFF_SIZE,"delta %12.6f %12.6f %12.6f", xAxis[0], xAxis[1], xAxis[2]); ofs << buffer << "\n"; // data line 4: y-displacement snprintf(buffer, BUFF_SIZE,"delta %12.6f %12.6f %12.6f", yAxis[0], yAxis[1], yAxis[2]); ofs << buffer << "\n"; // data line 5: z-displacement snprintf(buffer, BUFF_SIZE,"delta %12.6f %12.6f %12.6f", zAxis[0], zAxis[1], zAxis[2]); ofs << buffer << "\n"; // data line 6: # of points in x, y, z (nx, ny, nz) snprintf(buffer, BUFF_SIZE, "object 2 class gridconnections counts %5d %5d %5d", nx, ny, nz); ofs << buffer << "\n"; // data line 7: total # of points snprintf(buffer, BUFF_SIZE, "object 3 class array type double rank 0 items %5d data follows", nx*ny*nz); ofs << buffer << "\n"; // The cube(s) double value; unsigned int count = 1; for (int i = 0; i < nx; ++i) { for (int j = 0; j < ny; ++j) { for (int k = 0; k < nz; ++k) { value = gd->GetValue(i, j, k); snprintf(buffer, BUFF_SIZE," %12.5E", value); if (count % 3 == 0) ofs << buffer << "\n"; else ofs << buffer; count++; } // z-axis } // y-axis } // x-axis if (count % 3 != 0) ofs << "\n"; ofs << "attribute \"dep\" string \"positions\"\n"; ofs << "object \"regular positions regular connections\" class field\n"; ofs << "component \"positions\" value 1\n"; ofs << "component \"connections\" value 2\n"; ofs << "component \"data\" value 3\n"; return true; }
extern "C" int readgrid_(float *vdata, int *nx, int *ny, int *nz, float *x0, float *y0, float *z0, float *xx, float *yy, float *zz, char *file, int fsize) { float dx,dy,dz; int gsize; float v, vmin, vmax, vavg; int navg = 0; OBMol mol; OBConversion conv; conv.SetInFormat("cube"); std::string fname = file; int blank = fname.find(" "); //printf("%d,'%s'\n", blank, fname.substr(0,blank).c_str()); conv.ReadFile(&mol, fname.substr(0,blank).c_str()); //cout << mol.NumAtoms() << " atoms." << endl; if (mol.HasData(OBGenericDataType::GridData)) { std::vector<OBGenericData*> grids = mol.GetAllData(OBGenericDataType::GridData); OBGridData *grid = dynamic_cast<OBGridData *> (grids[0]); gsize = grid->GetNumberOfPoints(); grid->GetNumberOfPoints(*nx, *ny, *nz); vector3 origin = grid->GetOriginVector(); *x0 = origin[0]; *y0 = origin[1]; *z0 = origin[2]; vector3 maxv = grid->GetMaxVector(); *xx = maxv[0]; *yy = maxv[1]; *zz = maxv[2]; dx=(*xx-*x0)/(*nx-1); dy=(*yy-*y0)/(*ny-1); dz=(*zz-*z0)/(*nz-1); printf("%s %d=%d*%d*%d\n", grid->GetAttribute().c_str(), gsize, *nx, *ny, *nz); printf("%f %f\n", grid->GetMinValue(), grid->GetMaxValue()); printf("%f,%f,%f\n", *x0,*y0,*z0); printf("%f,%f,%f\n", *xx,*yy,*zz); printf("%f,%f,%f\n", dx,dy,dz); //vdata = (float *)calloc(gsize, sizeof(float)); /* this is for fortran, so reverse sense of slowest/fastest moving dimensions */ //for (int i=0; i<*nx; ++i) { vmin = grid->GetValue(0,0,0); vmax = vmin; for (int i=0; i<*nz; ++i) { for (int j=0; j<*ny; ++j) { //for (int k=0; k<*nz; ++k) { for (int k=0; k<*nx; ++k) { //*vdata++ = grid->GetValue(i,j,k); v = grid->GetValue(k,j,i); if (v < 1e30 && v > -1e30) { if (v < vmin) vmin = v; if (v > vmax) vmax = v; ++navg; vavg += v; } else { v = vmax * 1000; // just a guess } *vdata++ = v; } } } } vavg = vavg/navg; printf("min/avg/max = %f/%f/%f\n", vmin, vavg, vmax); return 0; }