// // bem.cc // // Copyright (C) 1996 Limit Point Systems, Inc. // // Author: Curtis Janssen // Maintainer: LPS // // This file is part of the SC Toolkit. // // The SC Toolkit is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // // The SC Toolkit is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with the SC Toolkit; see the file COPYING.LIB. If not, write to // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. // // The U.S. Government is granted a limited license as per AL 91-7. // #include #include #include #include #include #include #include #include using namespace std; using namespace sc; static ClassDesc BEMSolvent_cd( typeid(BEMSolvent),"BEMSolvent",1,"public DescribedClass", 0, create, 0); BEMSolvent::BEMSolvent(const Ref& keyval) { vertex_area_ = 0; matrixkit_ = new LocalSCMatrixKit; debug_ = keyval->intvalue("debug"); solute_ << keyval->describedclassvalue("solute"); solvent_ << keyval->describedclassvalue("solvent"); // Use the aug-cc-pVQZ MP2 optimum geometry for H2O as default if (solvent_.null()) { solvent_ = new Molecule; solvent_->add_atom(8, 0.0000000000, 0.0000000000, -0.1265941233); solvent_->add_atom(1, 0.0000000000, 1.4304840085, 0.9856159541); solvent_->add_atom(1, 0.0000000000, -1.4304840085, 0.9856159541); } solvent_density_ = keyval->doublevalue("solvent_density"); // use as default the number density of water in au^-3, T=25 C, P=101325 Pa if (keyval->error() != KeyVal::OK) solvent_density_ = 0.004938887; surf_ << keyval->describedclassvalue("surface"); dielectric_constant_ = keyval->doublevalue("dielectric_constant"); if (keyval->error() != KeyVal::OK) dielectric_constant_ = 78.0; grp_ = MessageGrp::get_default_messagegrp(); } BEMSolvent::~BEMSolvent() { } double** BEMSolvent::alloc_array(int n, int m) { double ** result = new double*[n]; result[0] = new double[n*m]; for (int i=1; ivertex(i)->point(); for (j=0; j<3; j++) { pos[i][j] = p[j]; } } } void BEMSolvent::normals(double**norms) { int i,j; int n = ncharge(); for (i=0; ivertex(i)->normal(); for (j=0; j<3; j++) { norms[i][j] = p[j]; } } } void BEMSolvent::init() { surf_->clear(); surf_->init(); system_matrix_i_ = 0; f_ = (1.0-dielectric_constant_)/(2.0*M_PI*(1.0+dielectric_constant_)); if (vertex_area_) delete[] vertex_area_; vertex_area_ = new double[ncharge()]; for (int i=0; iclear(); system_matrix_i_ = 0; if (vertex_area_) delete[] vertex_area_; vertex_area_ = 0; } void BEMSolvent::charges_to_surface_charge_density(double *charges) { for (int i=0; i 0.0) charge_pos += charges[i]; else charge_neg += charges[i]; } double scale_pos = 1.0; double scale_neg = 1.0; if (charge_pos > 1.0e-4 && charge_neg < -1.0e-4) { scale_pos += (expected_charge-charge)/(2.0*charge_pos); scale_neg += (expected_charge-charge)/(2.0*charge_neg); } else if (charge_pos > 1.0e-4) { scale_pos += (expected_charge-charge)/charge_pos; } else if (charge_neg < -1.0e-4) { scale_neg += (expected_charge-charge)/charge_neg; } double new_charge = 0.0; for (i=0; i 0.0) charges[i] *= scale_pos; else charges[i] *= scale_neg; new_charge += charges[i]; } if (fabs(new_charge - expected_charge) > 1.0e-3) { ExEnv::outn() << "BEMSolvent:normalize_charge: failed:" << endl << "new_charge = " << new_charge << endl << "expected_charge = " << expected_charge << endl; abort(); } if (debug_) { ExEnv::out0() << indent << "BEMSolvent:normalize_charge:" << endl << indent << scprintf(" integrated surface charge = %20.15f", charge) << endl << indent << scprintf(" expected surface charge = %20.15f", expected_charge) << endl; } } void BEMSolvent::init_system_matrix() { int i, j; int n = ncharge(); RefSCDimension d = new SCDimension(n); RefSCMatrix system_matrix(d,d,matrixkit()); system_matrix.assign(0.0); tim_enter("precomp"); // precompute some arrays TriangulatedSurfaceIntegrator triint(surf_.pointer()); int n_integration_points = triint.n(); SCVector3 *surfpv = new SCVector3[n_integration_points]; double *rfdA = new double[n_integration_points]; double *sfdA = new double[n_integration_points]; double *rsfdA = new double[n_integration_points]; int *j0 = new int[n_integration_points]; int *j1 = new int[n_integration_points]; int *j2 = new int[n_integration_points]; for (triint=0, i=0; ipoint(); j0[i] = triint.vertex_number(0); j1[i] = triint.vertex_number(1); j2[i] = triint.vertex_number(2); double r = triint.r(); double s = triint.s(); double rs = 1 - r - s; double dA = triint.w(); double fdA = - f_ * dA; rfdA[i] = r * fdA; sfdA[i] = s * fdA; rsfdA[i] = rs * fdA; } tim_exit("precomp"); tim_enter("sysmat"); double *sysmati = new double[n]; RefSCVector vsysmati(system_matrix->rowdim(),system_matrix->kit()); // loop thru all the vertices for (i = 0; i v = surf_->vertex(i); const SCVector3& pv = v->point(); const SCVector3& nv = v->normal(); // integrate over the surface for (j = 0; j < n_integration_points; j++) { SCVector3 diff(pv - surfpv[j]); double normal_component = diff.dot(nv); double diff2 = diff.dot(diff); if (diff2 <= 1.0e-8) { // The self term must not be included here. This // case shouldn't occur for the usual integrators // so abort. ExEnv::errn() << "BEMSolvent: integrator gave the self term" << endl; abort(); } double denom = diff2*sqrt(diff2); double common_factor = normal_component/denom; sysmati[j0[j]] += common_factor * rsfdA[j]; sysmati[j1[j]] += common_factor * rfdA[j]; sysmati[j2[j]] += common_factor * sfdA[j]; } vsysmati->assign(sysmati); system_matrix->assign_row(vsysmati,i); } tim_exit("sysmat"); delete[] surfpv; delete[] rfdA; delete[] sfdA; delete[] rsfdA; delete[] j0; delete[] j1; delete[] j2; delete[] sysmati; tim_enter("AV"); double A = 0.0; double V = 0.0; for (triint = 0; triint.update(); triint++) { V += triint.weight()*triint.dA()[2]*triint.current()->point()[2]; A += triint.w(); } area_ = A; volume_ = V; tim_exit("AV"); ExEnv::out0() << indent << scprintf("Solvent Accessible Surface:") << endl << indent << scprintf(" Area = %15.10f ", A) << scprintf("Volume = %15.10f ", V) << scprintf("Nvertex = %3d", n) << endl; // Add I to the system matrix. system_matrix->shift_diagonal(1.0); //system_matrix->print("System Matrix"); tim_enter("inv"); system_matrix->invert_this(); system_matrix_i_ = system_matrix; tim_exit("inv"); //system_matrix_i_->print("System Matrix Inverse"); } void BEMSolvent::compute_charges(double* efield_dot_normals, double* charges) { if (system_matrix_i_.null()) { tim_enter("sysmat"); init_system_matrix(); tim_exit("sysmat"); } tim_enter("qenq"); double efield_dot_normal = 0.0; int n = ncharge(); for (int i=0; inatom(); for (int i=0; ir(i,k); r2 += r*r; } energy += nuclear_charge[i] * charge[j] / sqrt(r2); } } return energy; } double BEMSolvent::nuclear_interaction_energy(double** charge_positions, double* charge) { double energy = 0.0; int natom = solute_->natom(); for (int i=0; ir(i,k); r2 += r*r; } energy += double(solute_->Z(i)) * charge[j] / sqrt(r2); } } return energy; } double BEMSolvent::self_interaction_energy(double** charge_positions, double* charge) { int i,j; charges_to_surface_charge_density(charge); TriangulatedSurfaceIntegrator triint(surf_.pointer()); int n_integration_points = triint.n(); SCVector3 *points = new SCVector3[n_integration_points]; double *charges = new double[n_integration_points]; double energy = 0.0; for (triint=0, i=0; ipoint(); int v0 = triint.vertex_number(0); int v1 = triint.vertex_number(1); int v2 = triint.vertex_number(2); double r = triint.r(); double s = triint.s(); double rs = 1.0 - r - s; double dA = triint.w(); charges[i] = (charge[v0]*rs + charge[v1]*r + charge[v2]*s)*dA; energy += 0.0; // is this good enough for the self term? } for (i=0; i