/** * Get the Wilson's B-matrix */ void getBMatrix(Real** cartCoords, int numCartesians, bondCoord** bonds, int numBonds, angleCoord** angles, int numAngles, dihedralCoord** dihedrals, int numDihedrals, improperCoord** impropers, int numImpropers, Matrix& B) { #ifdef DEBUG cout << "\n\ngetBMatrix - Constructing B Matrix\n"; #endif // Constructing B Matrix // follows method in chapter 4 of Molecular Vibrations by Wilson, Decius, and Cross #ifdef DEBUG int numInternals = numBonds + numAngles + numDihedrals + numImpropers; cout << "numBonds: " << numBonds << "\n"; cout << "numAngles: " << numAngles << "\n"; cout << "numDihedrals: " << numDihedrals << "\n"; cout << "numImpropers: " << numImpropers << "\n"; cout << "numInternals: " << numInternals << "\n"; #endif // Load Data B = 0.0; int i = 0; int j = 0; int index1 = 0; int index2 = 0; RowVector tempCoord1(3); RowVector tempCoord2(3); Real norm = 0.0; // Bonds for (i=0; i<numBonds; i++) { index1 = bonds[i]->x1; index2 = bonds[i]->x2; //norm = bonds[i].val; // Could calculate this, like below. #ifdef DEBUG cout << "index1=" << index1 << "index2=" << index2 << "\n"; #endif for (j=0; j<3; j++) { tempCoord1(j+1) = cartCoords[index1][j]; tempCoord2(j+1) = cartCoords[index2][j]; } tempCoord1 << tempCoord1 - tempCoord2; norm = tempCoord1.NormFrobenius(); // XXX - don't delete if (norm > 0.0) { tempCoord1 << tempCoord1 / norm; } for (j=1; j<=3; j++) { B(i+1,((index1)*3)+j) = tempCoord1(j); B(i+1,((index2)*3)+j) = -tempCoord1(j); } } #ifdef DEBUG cout << "after bonds\n"; cout << "B:\n"; cout << setw(9) << setprecision(3) << (B); cout << "\n\n"; #endif // Angles int index3 = 0; RowVector tempCoord3(3); RowVector tempCoord4(3); RowVector tempCoord5(3); RowVector r21(3); // Vector from 2nd to 1st point RowVector r23(3); // Vector from 2nd to 3rd point RowVector e21(3); // Unit vector from 2nd to 1st point RowVector e23(3); // Unit vector from 2nd to 3rd point Real norm21; // Norm of r21 Real norm23; // Norm of r23 Real angle = 0.0; // Angle in radians Real cosAngle123 = 0.0; Real sinAngle123 = 0.0; //Real pi = 3.14159265; Real scaleFactor = 0.529178; // Scaling factor (0.529178) for (i=0; i<numAngles; i++) { index1 = angles[i]->x1; index2 = angles[i]->x2; index3 = angles[i]->x3; //angle = angles[i].val * (pi/180.0); // Convert to radians. for (j=0; j<3; j++) { tempCoord1(j+1) = cartCoords[index1][j]; tempCoord2(j+1) = cartCoords[index2][j]; tempCoord3(j+1) = cartCoords[index3][j]; } r21 << tempCoord1 - tempCoord2; r23 << tempCoord3 - tempCoord2; norm21 = r21.NormFrobenius(); norm23 = r23.NormFrobenius(); e21 << r21; if (norm21 > 0.0) { e21 << e21 / norm21; } e23 << r23; if (norm23 > 0.0) { e23 << e23 / norm23; } angle = acos(DotProduct(r21,r23) / (norm21 * norm23)); cosAngle123 = DotProduct(r21,r23) / (norm21 * norm23); sinAngle123 = sqrt(1 - (cosAngle123 * cosAngle123)); #ifdef DEBUG cout << "r21: " << (r21) << "\n"; cout << "r23: " << (r23) << "\n"; cout << "norm21: " << norm21 << ", norm23: " << norm23 << "\n\n"; cout << "e21: " << (e21) << "\n"; cout << "e23: " << (e23) << "\n"; cout << "cos(" << angle << "): " << cos(angle) << "\n"; cout << "sin(" << angle << "): " << sin(angle) << "\n"; cout << "angle: " << acos(DotProduct(r21,r23) / (norm21 * norm23)) << "\n"; cout << "cosAngle123: " << cosAngle123 << "\n"; cout << "sinAngle123: " << sinAngle123 << "\n"; #endif // First elements of coordinate triples tempCoord4 << ((cosAngle123 * e21) - e23); tempCoord4 << (tempCoord4 * scaleFactor) / (norm21 * sinAngle123); for (j=1; j<=3; j++) { B(i+numBonds+1,((index1)*3)+j) = tempCoord4(j); } // Third elements of coordinate triples tempCoord5 << ((cosAngle123 * e23) - e21); tempCoord5 << (tempCoord5 * scaleFactor) / (norm23 * sinAngle123); for (j=1; j<=3; j++) { B(i+numBonds+1,((index3)*3)+j) = tempCoord5(j); } // Second (middle) elements of coordinate triples (depends on 1st and 3rd) tempCoord4 << -tempCoord4 - tempCoord5; for (j=1; j<=3; j++) { B(i+numBonds+1,((index2)*3)+j) = tempCoord4(j); } } #ifdef DEBUG cout << "after angles\n"; cout << "B:\n"; cout << setw(9) << setprecision(3) << (B); cout << "\n\n"; #endif // Dihedrals RowVector r12(3); // Vector from 1st to 2nd point RowVector r32(3); // Vector from 3rd to 2nd point RowVector r34(3); // Vector from 3rd to 2nd point RowVector r43(3); // Vector from 4th to 3rd point RowVector e12(3); // Unit vector from 1st to 2nd point RowVector e32(3); // Unit vector from 3rd to 2nd point RowVector e34(3); // Unit vector from 3rd to 2nd point RowVector e43(3); // Unit vector from 4th to 3rd point Real norm12; // Norm of r12 Real norm32; // Norm of r32 Real norm34; // Norm of r34 Real norm43; // Norm of r43 RowVector cross1223(3); // Cross product of e12 and e23 RowVector cross4332(3); // Cross product of e43 and e32 Real angle123 = 0.0; // Angle in radians Real angle234 = 0.0; // Angle in radians Real cosAngle234 = 0.0; Real sinAngle234 = 0.0; scaleFactor = 0.529178; // Scaling factor (0.529178) int index4 = 0; RowVector tempCoord6(3); for (i=0; i<numDihedrals; i++) { index1 = dihedrals[i]->x1; index2 = dihedrals[i]->x2; index3 = dihedrals[i]->x3; index4 = dihedrals[i]->x4; for (j=0; j<3; j++) { tempCoord1(j+1) = cartCoords[index1][j]; tempCoord2(j+1) = cartCoords[index2][j]; tempCoord3(j+1) = cartCoords[index3][j]; tempCoord4(j+1) = cartCoords[index4][j]; } r12 << tempCoord2 - tempCoord1; r21 << tempCoord1 - tempCoord2; r23 << tempCoord3 - tempCoord2; r32 << tempCoord2 - tempCoord3; r34 << tempCoord4 - tempCoord3; r43 << tempCoord3 - tempCoord4; norm12 = r12.NormFrobenius(); norm21 = r21.NormFrobenius(); norm23 = r23.NormFrobenius(); norm32 = r32.NormFrobenius(); norm34 = r34.NormFrobenius(); norm43 = r43.NormFrobenius(); #ifdef DEBUG cout << "norm12: " << norm12 << "\n"; cout << "norm21: " << norm21 << "\n"; cout << "norm23: " << norm23 << "\n"; cout << "norm32: " << norm32 << "\n"; cout << "norm34: " << norm34 << "\n"; cout << "norm43: " << norm43 << "\n"; #endif e12 << r12 / norm12; e21 << r21 / norm21; e23 << r23 / norm23; e32 << r32 / norm32; e34 << r34 / norm34; e43 << r43 / norm43; angle123 = acos(DotProduct(r21,r23) / (norm21 * norm23)); // Wilson's angle 2 angle234 = acos(DotProduct(r32,r34) / (norm32 * norm34)); // Wilson's angle 3 cosAngle123 = DotProduct(r21,r23) / (norm21 * norm23); cosAngle234 = DotProduct(r32,r34) / (norm32 * norm34); sinAngle123 = sqrt(1 - (cosAngle123 * cosAngle123)); sinAngle234 = sqrt(1 - (cosAngle234 * cosAngle234)); #ifdef DEBUG cout << "angle123: " << angle123 << ", cos(angle123): " << cos(angle123) << ", sin(angle123): " << sin(angle123) << "\n"; cout << "angle234: " << angle234 << ", cos(angle234): " << cos(angle234) << ", sin(angle234): " << sin(angle234) << "\n"; cout << "cosAngle123: " << cosAngle123 << ", sinAngle123: " << sinAngle123 << "\n"; cout << "cosAngle234: " << cosAngle234 << ", sinAngle234: " << sinAngle234 << "\n"; #endif cross1223(1) = (e12(2)*e23(3)) - (e12(3)*e23(2)); cross1223(2) = (e12(3)*e23(1)) - (e12(1)*e23(3)); cross1223(3) = (e12(1)*e23(2)) - (e12(2)*e23(1)); cross4332(1) = (e43(2)*e32(3)) - (e43(3)*e32(2)); cross4332(2) = (e43(3)*e32(1)) - (e43(1)*e32(3)); cross4332(3) = (e43(1)*e32(2)) - (e43(2)*e32(1)); #ifdef DEBUG cout << "cross1223 (norm " << cross1223.NormFrobenius() << "):\n"; cout << setw(9) << setprecision(6) << (cross1223); cout << "\n\n"; cout << "cross4332 (norm " << cross4332.NormFrobenius() << "):\n"; cout << setw(9) << setprecision(6) << (cross4332); cout << "\n\n"; #endif // First elements of coordinate triples tempCoord5 << -((cross1223 * scaleFactor) / (norm12 * sinAngle123 * sinAngle123)); for (j=1; j<=3; j++) { B(i+numBonds+numAngles+1,((index1)*3)+j) = tempCoord5(j); } // Second elements of coordinate triples tempCoord5 << ((norm23 - (norm12 * cosAngle123)) / (norm23 * norm12 * sinAngle123 * sinAngle123)) * (cross1223); tempCoord6 << (cosAngle234 / (norm23 * sinAngle234 * sinAngle234)) * (cross4332); #ifdef DEBUG cout << "tempCoord5:\n"; cout << setw(9) << setprecision(6) << (tempCoord5); cout << "tempCoord6:\n"; cout << setw(9) << setprecision(6) << (tempCoord6); #endif tempCoord5 << (tempCoord5 + tempCoord6) * scaleFactor; #ifdef DEBUG cout << "tempCoord5:\n"; cout << setw(9) << setprecision(6) << (tempCoord5); #endif for (j=1; j<=3; j++) { B(i+numBonds+numAngles+1,((index2)*3)+j) = tempCoord5(j); } // Third elements of coordinate triples tempCoord5 << ((norm32 - (norm43 * cosAngle234)) / (norm32 * norm43 * sinAngle234 * sinAngle234)) * (cross4332); tempCoord6 << (cosAngle123 / (norm32 * sinAngle123 * sinAngle123)) * (cross1223); tempCoord5 << (tempCoord5 + tempCoord6) * scaleFactor; for (j=1; j<=3; j++) { B(i+numBonds+numAngles+1,((index3)*3)+j) = tempCoord5(j); } // Fourth elements of coordinate triples tempCoord5 << -((cross4332 * scaleFactor) / (norm43 * sinAngle234 * sinAngle234)); for (j=1; j<=3; j++) { B(i+numBonds+numAngles+1,((index4)*3)+j) = tempCoord5(j); } } #ifdef DEBUG cout << "B:\n"; cout << setw(9) << setprecision(3) << (B); cout << "\n\n"; #endif // Impropers RowVector r41(3); // Vector from 4th to 1st point RowVector r42(3); // Vector from 4th to 2nd point RowVector e41(3); // Unit vector from 4th to 1st point RowVector e42(3); // Unit vector from 4th to 2nd point RowVector normVector(3); // Normal to the plane Real norm41; // Norm of r41 Real norm42; // Norm of r42 Real angle142 = 0.0; // Angle in radians Real angle143 = 0.0; // Angle in radians Real angle243 = 0.0; // Angle in radians Real cosAngle142 = 0.0; Real cosAngle143 = 0.0; Real cosAngle243 = 0.0; Real sinAngle142 = 0.0; Real sinAngle143 = 0.0; Real sinAngle243 = 0.0; Real apexCoeff = 0.0; // Magnitude of central atom displacement scaleFactor = -0.352313; // Scale factor (-0.352313) for (i=0; i<numImpropers; i++) { index1 = impropers[i]->x1; index2 = impropers[i]->x2; index3 = impropers[i]->x3; index4 = impropers[i]->x4; for (j=0; j<3; j++) { tempCoord1(j+1) = cartCoords[index1][j]; tempCoord2(j+1) = cartCoords[index2][j]; tempCoord3(j+1) = cartCoords[index3][j]; tempCoord4(j+1) = cartCoords[index4][j]; } r41 << tempCoord1 - tempCoord4; r42 << tempCoord2 - tempCoord4; r43 << tempCoord3 - tempCoord4; norm41 = r41.NormFrobenius(); norm42 = r42.NormFrobenius(); norm43 = r43.NormFrobenius(); e41 << r41 / norm41; e42 << r42 / norm42; e43 << r43 / norm43; angle142 = acos(DotProduct(r41,r42) / (norm41 * norm42)); angle143 = acos(DotProduct(r41,r43) / (norm41 * norm43)); angle243 = acos(DotProduct(r42,r43) / (norm42 * norm43)); cosAngle142 = DotProduct(r41,r42) / (norm41 * norm42); cosAngle143 = DotProduct(r41,r43) / (norm41 * norm43); cosAngle243 = DotProduct(r42,r43) / (norm42 * norm43); sinAngle142 = sqrt(1 - (cosAngle142 * cosAngle142)); sinAngle143 = sqrt(1 - (cosAngle143 * cosAngle143)); sinAngle243 = sqrt(1 - (cosAngle243 * cosAngle243)); normVector(1) = (r41(2)*r42(3)) - (r41(3)*r42(2)); normVector(2) = (r41(3)*r42(1)) - (r41(1)*r42(3)); normVector(3) = (r41(1)*r42(2)) - (r41(2)*r42(1)); normVector << normVector / normVector.NormFrobenius(); // First elements of coordinate triples tempCoord5 << normVector * (scaleFactor / norm41); for (j=1; j<=3; j++) { B(i+numBonds+numAngles+numDihedrals+1,((index1)*3)+j) = tempCoord5(j); } // Second elements of coordinate triples tempCoord5 << normVector * sinAngle143 * scaleFactor; tempCoord5 << tempCoord5 / (norm42 * sinAngle243); for (j=1; j<=3; j++) { B(i+numBonds+numAngles+numDihedrals+1,((index2)*3)+j) = tempCoord5(j); } // Third elements of coordinate triples tempCoord5 << normVector * sinAngle142 * scaleFactor; tempCoord5 << tempCoord5 / (norm43 * sinAngle243); for (j=1; j<=3; j++) { B(i+numBonds+numAngles+numDihedrals+1,((index3)*3)+j) = tempCoord5(j); } // Fourth elements of coordinate triples apexCoeff = -1.0 / norm42; apexCoeff -= sinAngle143 / (norm42 * sinAngle243); apexCoeff -= sinAngle142 / (norm43 * sinAngle243); tempCoord5 << normVector * apexCoeff * scaleFactor; for (j=1; j<=3; j++) { B(i+numBonds+numAngles+numDihedrals+1,((index4)*3)+j) = tempCoord5(j); } } return; }
int json_cpp_tests() { json::Value e1(json::load_file("test.json")); json::Value e2(e1); json::Value e3; json::Value e4(json::load_string("{\"foo\": true, \"bar\": \"test\"}")); ASSERT_TRUE(e1.is_object(), "e1 is not an object"); ASSERT_TRUE(e2.is_object(), "e2 is not an object"); ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value"); ASSERT_TRUE(e4.is_object(), "e4 is not an object"); ASSERT_EQ(e1.size(), 1, "e1 has too many properties"); ASSERT_EQ(e2.size(), 1, "e2 has too many properties"); ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements"); ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object"); ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value"); // verify iterator results (note that they can be returned in any order) json::Iterator i(e1.get("web-app")); std::set<std::string> iteratorResults; for ( int ii = 0; ii < 3; ++ii ) { ASSERT_FALSE(i.key().empty(), "iterator returned a null value"); iteratorResults.insert(i.key()); i.next(); } ASSERT_FALSE(i.valid(), "iterator has more values than expected"); ASSERT_EQ(iteratorResults.size(), 3, "iterator did not return enough values"); json::Value e5(json::Value(12.34)); ASSERT_TRUE(e5.is_number(), "e5 is not a number after assignment"); ASSERT_EQ(e5.as_real(), 12.34, "e5 has incorrect value after assignment"); json::Value e6(json::Value(true)); ASSERT_TRUE(e6.is_boolean(), "e6 is not a boolean after assignment"); ASSERT_EQ(e6.as_boolean(), true, "e6 has incorrect value after assignment"); json::Value e7(json::Value("foobar")); ASSERT_TRUE(e7.is_string(), "e7 is not a string after assignment"); ASSERT_EQ(e7.as_string(), "foobar", "e7 has incorrect value after assignment"); json::Value e8(json::object()); ASSERT_TRUE(e8.is_object(), "e8 is not an object after assignment"); json::Value e9(json::null()); ASSERT_TRUE(e9.is_null(), "e9 is not null after assignment"); json::Value e10(json::array()); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); e10.set_at(0, json::Value("foobar")); ASSERT_EQ(e10.size(), 1, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[0].as_string(), "foobar", "e10[0] has incorrect value after assignment"); e10.set_at(1, json::Value("foobar")); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[1].as_string(), "foobar", "e10[0] has incorrect value after assignment"); e10.set_at(0, json::Value("barfoo")); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); ASSERT_EQ(e10[0].as_string(), "barfoo", "e10[0] has incorrect value after assignment"); e10.set_at(100, json::null()); ASSERT_TRUE(e10.is_array(), "e10 is not an array after index assignment"); ASSERT_EQ(e10.size(), 2, "e10 has incorrect number of elements after assignment"); e10.insert_at(1, json::Value("new")); ASSERT_EQ(e10.size(), 3, "e10 has incorrect size after insert"); ASSERT_EQ(e10[1].as_string(), "new", "e10[1] has incorrect value after insert"); ASSERT_EQ(e10[2].as_string(), "foobar", "e10[2] has incorrect value after insert"); e10.del_at(0); ASSERT_EQ(e10.size(), 2, "e10 has incorrect size after delete"); ASSERT_EQ(e10[1].as_string(), "foobar", "e10[1] has incorrect value after delete"); e10.clear(); ASSERT_EQ(e10.size(), 0, "e10 has incorrect number of elements after clear"); json::Value e11(json::object()); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); e11.set_key("foo", json::Value("test")); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["foo"].as_string(), "test", "e11.foo has incorrect value after assignment"); e11.set_key("foo", json::Value("again")); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_EQ(e11.size(), 1, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["foo"].as_string(), "again", "e11.foo has incorrect value after assignment"); e11.set_key("bar", json::Value("test")); ASSERT_TRUE(e11.is_object(), "e11 is not an object after property assignment"); ASSERT_EQ(e11.size(), 2, "e11 has incorrect number of properties after assignment"); ASSERT_EQ(e11["bar"].as_string(), "test", "e11.foo has incorrect value after assignment"); e11.clear(); ASSERT_EQ(e11.size(), 0, "e11 has incorrect number of properties after clear"); json::Value e12(json::object()); e12.set_key("foo", json::Value("test")); e12.set_key("bar", json::Value(3)); char* out_cstr = e12.save_string(JSON_COMPACT); std::string out(out_cstr); free(out_cstr); ASSERT_EQ(out, "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); std::istringstream instr(out); instr >> e12; ASSERT_TRUE(e12.is_object(), "e12 is not an object after stream read"); ASSERT_EQ(e12.size(), 2, "e12 has wrong size after stream read"); ASSERT_EQ(e12.get("bar").as_integer(), 3, "e12.bar has incorrect value after stream read"); ASSERT_EQ(e12.get("foo").as_string(), "test", "ee12.test has incorrect value after stream read"); std::ostringstream outstr; outstr << e12; ASSERT_EQ(instr.str(), "{\"bar\":3,\"foo\":\"test\"}", "object did not serialize as expected"); const json::Value e13(e12); ASSERT_EQ(e13["bar"].as_integer(), 3, "e13.bar has incorrect value after copy"); json::Value e14(json::object()); ASSERT_TRUE(e14.is_object(), "e14 is not an object after construction"); e14.set_key("foo", json::object()); ASSERT_TRUE(e14["foo"].is_object(), "e14.foo is not an object after assignment"); e14["foo"]["bar"] = json::Value(42); ASSERT_EQ(e14["foo"]["bar"].as_integer(), 42, "e14.foo.bar has incorrect value after assignment"); json::Value e15(json::array()); ASSERT_TRUE(e15.is_array(), "e15 is not an array after construction"); e15.set_at(0, json::Value(42)); ASSERT_EQ(e15[0].as_integer(), 42, "e15[0] has incorrect value after assignment"); e15[0] = json::Value("foo"); ASSERT_EQ(e15[0].as_string(), "foo", "e15[0] has incorrecy value after assignment"); return 0; }