source: molecuilder/src/bondgraph.cpp@ 08b88b

Last change on this file since 08b88b was 08b88b, checked in by Frederik Heber <heber@…>, 16 years ago

Fixing not created adjacency list, partially fixing subgraph dissection in config::Load()

  • CreateAdjacencyList() was called with zero bonddistance.
  • this was due to max_distance not being initialised in the constructor to 0 and subsequently not set if Bond Length Table was not found.
  • new function SetMaxDistanceToMaxOfCovalentRadii() which sets the max_distance to twice the max of covalent radii of all elements.
  • config::Load() - atoms in copied molecule (by DepthFirstSearchAnalysis()) are made their own father (and originals are removed).
  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * bondgraph.cpp
3 *
4 * Created on: Oct 29, 2009
5 * Author: heber
6 */
7
8#include <iostream>
9
10#include "atom.hpp"
11#include "bondgraph.hpp"
12#include "element.hpp"
13#include "molecule.hpp"
14#include "parser.hpp"
15#include "periodentafel.hpp"
16#include "vector.hpp"
17
18/** Constructor of class BondGraph.
19 * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule.
20 */
21BondGraph::BondGraph(bool IsA) : BondLengthMatrix(NULL), max_distance(0), IsAngstroem(IsA)
22{
23};
24
25/** Destructor of class BondGraph.
26 */
27BondGraph::~BondGraph()
28{
29 if (BondLengthMatrix != NULL) {
30 delete(BondLengthMatrix);
31 }
32};
33
34/** Parses the bond lengths in a given file and puts them int a matrix form.
35 * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
36 * but only if parsing is successfull. Otherwise variable is left as NULL.
37 * \param *out output stream for debugging
38 * \param filename file with bond lengths to parse
39 * \return true - success in parsing file, false - failed to parse the file
40 */
41bool BondGraph::LoadBondLengthTable(ofstream * const out, const string &filename)
42{
43 bool status = true;
44 MatrixContainer *TempContainer = NULL;
45
46 // allocate MatrixContainer
47 if (BondLengthMatrix != NULL) {
48 *out << "MatrixContainer for Bond length already present, removing." << endl;
49 delete(BondLengthMatrix);
50 }
51 TempContainer = new MatrixContainer;
52
53 // parse in matrix
54 status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0);
55
56 // find greatest distance
57 max_distance=0;
58 if (status) {
59 for(int i=0;i<TempContainer->RowCounter[0];i++)
60 for(int j=i;j<TempContainer->ColumnCounter[0];j++)
61 if (TempContainer->Matrix[0][i][j] > max_distance)
62 max_distance = TempContainer->Matrix[0][i][j];
63 }
64
65 if (status) // set to not NULL only if matrix was parsed
66 BondLengthMatrix = TempContainer;
67 else {
68 BondLengthMatrix = NULL;
69 delete(TempContainer);
70 }
71 return status;
72};
73
74/** Parses the bond lengths in a given file and puts them int a matrix form.
75 * \param *out output stream for debugging
76 * \param *mol molecule with atoms
77 * \return true - success, false - failed to construct bond structure
78 */
79bool BondGraph::ConstructBondGraph(ofstream * const out, molecule * const mol)
80{
81 bool status = true;
82
83 if (mol->start->next == mol->end) // only construct if molecule is not empty
84 return false;
85
86 if (BondLengthMatrix == NULL) // no bond length matrix parsed?
87 mol->CreateAdjacencyList(out, max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this);
88 else
89 mol->CreateAdjacencyList(out, max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this);
90
91 return status;
92};
93
94/** Returns the entry for a given index pair.
95 * \param firstelement index/atom number of first element (row index)
96 * \param secondelement index/atom number of second element (column index)
97 * \note matrix is of course symmetric.
98 */
99double BondGraph::GetBondLength(int firstZ, int secondZ)
100{
101 if (BondLengthMatrix == NULL)
102 return( -1. );
103 else
104 return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
105};
106
107/** Determines the maximum of all element::CovalentRadius.
108 * \param *out output stream for debugging
109 * \param *periode periodentafel with all elements.
110 */
111double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(ofstream * const out, const periodentafel * const periode)
112{
113 max_distance = 0.;
114
115 element * Runner = periode->start;
116 while (Runner->next != periode->end) {
117 Runner = Runner->next;
118 if (Runner->CovalentRadius > max_distance)
119 max_distance = Runner->CovalentRadius;
120 }
121 max_distance *= 2.;
122
123 return max_distance;
124};
125
126/** Returns bond criterion for given pair based on covalent radius.
127 * \param *Walker first BondedParticle
128 * \param *OtherWalker second BondedParticle
129 * \param &MinDistance lower bond bound on return
130 * \param &MaxDistance upper bond bound on return
131 * \param IsAngstroem whether units are in angstroem or bohr radii
132 */
133void BondGraph::CovalentMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem)
134{
135 MinDistance = OtherWalker->type->CovalentRadius + Walker->type->CovalentRadius;
136 MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
137 MaxDistance = MinDistance + BONDTHRESHOLD;
138 MinDistance -= BONDTHRESHOLD;
139};
140
141/** Returns bond criterion for given pair based on a bond length matrix.
142 * The matrix should be contained in \a this BondGraph and contain an element-
143 * to-element length.
144 * \param *Walker first BondedParticle
145 * \param *OtherWalker second BondedParticle
146 * \param &MinDistance lower bond bound on return
147 * \param &MaxDistance upper bond bound on return
148 * \param IsAngstroem whether units are in angstroem or bohr radii
149 */
150void BondGraph::BondLengthMatrixMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem)
151{
152 if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
153 cerr << Verbose(1) << "WARNING: BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl;
154 CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
155 } else {
156 MinDistance = GetBondLength(Walker->type->Z-1, OtherWalker->type->Z-1);
157 MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
158 MaxDistance = MinDistance + BONDTHRESHOLD;
159 MinDistance -= BONDTHRESHOLD;
160 }
161};
162
Note: See TracBrowser for help on using the repository browser.