File: generalizedBornCase.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (966 lines) | stat: -rw-r--r-- 23,067 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: generalizedBornCase.C,v 1.3.10.1 2007/03/25 22:00:32 oliver Exp $
//

#include <BALL/SOLVATION/generalizedBornCase.h>
// #include "generalizedBornCase.h"

#include <BALL/SYSTEM/path.h>
#include <BALL/SYSTEM/timer.h>
#include <BALL/KERNEL/forEach.h>
#include <BALL/FORMAT/parameters.h>
#include <BALL/COMMON/exception.h>
#include <BALL/KERNEL/residue.h>
#include <BALL/KERNEL/bond.h>

using namespace std;

#define UNITED_ATOMS
// #define TSUI true

namespace BALL
{

	const String GeneralizedBornModel::Option::VERBOSITY = "verbosity";
	const String GeneralizedBornModel::Option::LAMBDA = "lambda";
	const String GeneralizedBornModel::Option::DELTA = "delta";
	const String GeneralizedBornModel::Option::KAPPA = "kappa";
	const String GeneralizedBornModel::Option::RHO_0 = "rho_0";
	const String GeneralizedBornModel::Option::DC_SOLVENT = "dc_solvent";
	const String GeneralizedBornModel::Option::DC_SOLUTE = "dc_solute";
	const String GeneralizedBornModel::Option::FILENAME = "filename";

	const Size GeneralizedBornModel::Default::VERBOSITY = 10;
	const float GeneralizedBornModel::Default::LAMBDA = 1.4f;
	const float GeneralizedBornModel::Default::DELTA = 0.15f;
	const float GeneralizedBornModel::Default::KAPPA = 0.1f;
	const float GeneralizedBornModel::Default::RHO_0 = 0.09f;
	const float GeneralizedBornModel::Default::DC_SOLVENT = 80.0f;
	const float GeneralizedBornModel::Default::DC_SOLUTE = 2.0f;
	const String GeneralizedBornModel::Default::FILENAME = "solvation/gb_scaling.ini";

	/*
	const Size GeneralizedBornModel::Default::VERBOSITY = 10;
	const float GeneralizedBornModel::Default::LAMBDA = 1.0f;
	const float GeneralizedBornModel::Default::DELTA = 0.00f;
	const float GeneralizedBornModel::Default::KAPPA = 0.1f;
	const float GeneralizedBornModel::Default::RHO_0 = 0.00f;
	const float GeneralizedBornModel::Default::DC_SOLVENT = 80.0f;
	const float GeneralizedBornModel::Default::DC_SOLUTE = 2.0f;
	const String GeneralizedBornModel::Default::FILENAME = "solvation/GB.ini";
	*/

	GeneralizedBornModel::GeneralizedBornModel()
		: ac_(0),
			born_radii_(),
			pair_list_(),
			pair_distances_(),
			pair_charges_(),
			dc_solute_(0.0f),
			dc_solvent_(0.0f),
			scaling_factors_(),
			scaling_factor_filename_(""),
			lambda_(0.0f),
			delta_(0.0f),
			kappa_(0.0f),
			rho_0_(0.0f)
	{
		// set the default values for the parameters
		lambda_ = Default::LAMBDA;
		delta_ = Default::DELTA;
		kappa_ = Default::KAPPA;
		rho_0_ = Default::RHO_0;
		dc_solvent_ = Default::DC_SOLVENT;
		dc_solute_ = Default::DC_SOLUTE;

		// Compute the factor for converting values to kJ/mol

		// Multiply charge unit
		to_kJ_mol_ = Constants::e0 * Constants::e0;

		// Divide by vacuum permittivity
		to_kJ_mol_ /= Constants::VACUUM_PERMITTIVITY;

		// Divide by length unit (Angstrom)
		to_kJ_mol_ /= 1e-10;

		// Divide by 1000 in order to resturn kJ/mol
		to_kJ_mol_ /= 1e3;

		// Now multiply with N_A for obtaining molar values
		to_kJ_mol_ *= Constants::AVOGADRO;

		// Divide by 4\pi
		to_kJ_mol_ /= (4.0f * Constants::PI);

	}


	GeneralizedBornModel::~GeneralizedBornModel()
	{
	}


	void GeneralizedBornModel::clear()
		
	{
		// Clear the AtomContainer pointer
		ac_ = 0;

		// Don't clear the scaling factors, although this is a clear() function.
		// This is obviously inconsistent, but should make sense in this case.
		// Maybe we should rename this method�

		// Clear all the lists we built for fast calculations
		atoms_.clear();
		scaling_factor_.clear();
		pair_list_.clear();
		pair_distances_.clear();
		pair_charges_.clear();
		squared_distances_.clear();
	}


	bool GeneralizedBornModel::setup(const AtomContainer& ac)
	{
		
		Timer timer;
		timer.start();
		// save the AtomContainer
		clear();
		ac_ = const_cast<AtomContainer*>(&ac);

		// Set the filename for the scaling factors
		if (scaling_factor_filename_ == String(""))
		{
			scaling_factor_filename_ = Default::FILENAME;
		}

		// Go through the atoms of the molecule and find those charged
		// hydrogens which have to be condensed on heavy atoms, because they
		// bear partial charges without havin a radius.

		// Note: Condensing hydrogens may lead to single atoms (NHx)!

		// Store the atoms that have to be removed after condensation in a hash
		// set.
		HashSet<Atom*> remove_me;

		// Iterate over the system
		AtomIterator it = ac_->beginAtom();
		for (; +it; ++it)
		{
			String resid;
			if (it->getResidue() != 0)
			{
				resid = it->getResidue()->getID() + ":";
			}
			else
			{
				resid = "";
			}

			// DEBUG
			/*
			Log.info() << resid << it->getFullName() << "@"
				<< it->getPosition() << " (r=" << it->getRadius() << ", c="
				<< it->getCharge() << ", e=" << it->getElement().getSymbol() << ")" 
				<< endl;
			*/
			// DEBUG

			if (it->getRadius() == 0.0 && it->getCharge() != 0.0)
			{
				if (it->getElement() != PTE[Element::H])
				{
					Log.error() << "Found non-hydrogen atom with non-zero charge and zero radius, aborting." 
						<< endl;
					Log.error() << it->getResidue()->getID() << ":" << it->getFullName() 
						<< "@" << it->getPosition() << " (r=" << it->getRadius() << ", c="
						<< it->getCharge() << ", e=" << it->getElement().getSymbol() << ")" 
						<< endl;
					return(1);
				}
				else
				{
					Log.info() << "Found charged hydrogen with zero radius: "
						<< it->getFullName() << endl;
#ifdef UNITED_ATOMS
					Atom* hydrogen_partner = it->getBond(0)->getPartner(*it);
					Log.info() << "Condensing it onto the bound heavy atom "
						<< hydrogen_partner->getFullName() << endl;
					hydrogen_partner->setCharge(hydrogen_partner->getCharge()
							+ it->getCharge());
					it->setCharge(0.0);
					remove_me.insert(&*it);
#else
					cout << "Setting radius to 1.30 A" << endl;
					it->setRadius(1.30f);
#endif
				}
			}
		}

		// Now remove the atoms from the system
		HashSet<Atom*>::Iterator remove_it = remove_me.begin();
		for (; +remove_it; ++remove_it)
		{
			Atom* ptr = *remove_it;
			delete(ptr);
		}
		Log.info() << "United atoms: Removed " << remove_me.size() 
			<< " atoms from the system" << endl;

		if (ac_->countAtoms() < 2)
		{
			Log.error() << "Need at least two atoms for this method!"
				<< endl;
		}

		// if there are scaling factors stored here, we're fine. If not, try to
		// load them from the default file
		if (scaling_factors_.size() == 0)
		{
			// Read scaling factors from standard INIfile
			if (!readScalingFactors(scaling_factor_filename_))
			{
				return(false);
			}
		}

		// Set scaling factor for each atom
		AtomConstIterator it1;
		for (it1 = ac_->beginAtom(); +it1; ++it1)
		{
			atoms_.push_back(&*it1);

			float S_j = 0.0f;

			if (scaling_factors_.has(it1->getElement().getSymbol()))
			{
				S_j = scaling_factors_[it1->getElement().getSymbol()];
			}
			else
			{
				Log.warn() << "Unknown element " << it1->getElement().getSymbol()
					<< ". Using unscaled radius for calculating eff. Born radius." 
					<< std::endl;
				S_j = 1.0f;
			}
			scaling_factor_.push_back(S_j);
		}

		Size N = atoms_.size();

		// Allocate memory
		pair_list_.resize(N*N / 2 + 1);
		pair_distances_.resize(N*N / 2 + 1);
		pair_charges_.resize(N*N / 2 + 1);

		squared_distances_.resize(N);

		// Build pair list and calculate distances
		Size k = 0;
		for (Size i = 0; i < N; ++i)
		{
			// Allocate memory
			squared_distances_[i].resize(N);

			for (Size j = 0; j < i; ++j)
			{
				float distance = atoms_[i]->getPosition().getDistance(atoms_[j]->getPosition());
				if (distance < 0.1f)
				{
					Log.warn() << "Atoms too close: " 
						<< atoms_[i]->getResidue()->getFullName()
						<< ":" << atoms_[i]->getResidue()->getID()
						<< ":" << atoms_[i]->getName()
						<< " - " 
						<< atoms_[j]->getResidue()->getFullName()
						<< ":" << atoms_[j]->getResidue()->getID()
						<< ":" << atoms_[j]->getName()
						<< std::endl;
				}

				pair_list_[k] = std::pair<Size, Size>(i, j);
				pair_distances_[k] = distance;
				// DEBUG
				// cout << "pair_distances_[" << k << "]: " << pair_distances_[k] << endl;
				// /DEBUG
				pair_charges_[k] = atoms_[i]->getCharge() * atoms_[j]->getCharge();
				squared_distances_[i][j] = squared_distances_[j][i] 
					= distance * distance;

				// DEBUG
				/*
				cout << i << " " << j << " " << k << " " 
					<< (Size) (i/2.0f * (i-1) + j) << endl;
				*/
				// /DEBUG

				// increase index counter
				++k;
			}
		}


		timer.stop();
		Log.info() << "setup(): " << timer.getCPUTime() << "s" << std::endl;
		return(true);
	}


	void GeneralizedBornModel::calculateBornRadii_()
		
	{
		born_radii_.clear();
		born_radii_.resize(atoms_.size());
		for (Size i = 0; i < atoms_.size(); ++i)
		{
			// This is what we want to know for atom i
			float effective_born_radius = 0.0f;
			// This variable is used for summing up the actual contributions to the
			// coulomb integral approximation
			float tmp = 0.0f;
			// The actual "radius" of atom i
			float radius = atoms_[i]->getRadius();

			Size n = atoms_.size();
			for (Size j = 0; j < n; ++j)
			{
				if (j != i)
				{
					Size k;
					if (i > j)
					{
						k = (Size) (i/2.0f * (i-1) + j);
					}
					else
					{
						k = (Size) (j/2.0f * (j-1) + i);
					}
					// DEBUG
					// cout << i << " " << j << " --> " << k << endl;
					// /DEBUG

					// sum up the contribution of every atom of the system
					tmp += coulombIntegralApproximation_(pair_distances_[k],
							scaling_factor_[j] * (atoms_[j]->getRadius() - rho_0_), 
							radius - rho_0_);
				}
			}

			// DEBUG
			// float one_over_B = 1.0f / (radius - rho_0_) - 0.5f * tmp;
			// std::cout << "HCT radius = " << (1.0f / one_over_B) << std::endl;
			// /DEBUG

#ifdef TSUI

			// These are the original calculations 
			// See Tsui et al., 

			float S = 1.0f / ( radius - rho_0_ ) - 0.5f * lambda_ * tmp;
			effective_born_radius = 1.0f / S - delta_;

			// DEBUG
			// std::cout << "reduced HCT radius = " << effective_born_radius << std::endl;
			// /DEBUG
#else

			// In Proteins 55:383-394, 2004 Onufriev et al. propose another term

			// OBC I
			float alpha = 0.8f;
			float beta = 0.0f;
			float gamma = 2.91f;

			// OBC II
			/*
				 float alpha = 1.0f;
				 float beta = 0.8f;
				 float gamma = 4.85f;
			 */

			float psi = 0.5f * (radius - rho_0_) * tmp;
			float psi2 = psi * psi;
			float psi3 = psi2 * psi;

			float S = 1.0f / ( radius - rho_0_ ) 
				- 1.0f / radius * tanhf( alpha * psi - beta * psi2 + gamma * psi3 );
			effective_born_radius = 1.0f / S;

			// DEBUG
			// std::cout << "OCB I radius = " << effective_born_radius << std::endl;
			// /DEBUG
#endif
			// DEBUG
			// cout << "born_radii_[" << i << "]: " << born_radii_[i] << endl;
			// /DEBUG

			born_radii_[i] = effective_born_radius;
		}
	}


	float GeneralizedBornModel::calculateEnergy()
		
	{
		// We have to calculate Born radii every time we do the calculation of
		// the energy because the distances between atoms does influence the
		// effedctive radii. This could be done more sophisticated in order to
		// speed up the calculation, no doubt.
		Timer timer;
		timer.start();
		calculateBornRadii_();
		timer.stop();
		Log.info() << "calculateBornRadii_(): " << timer.getCPUTime() 
			<< "s" << std::endl;

		return(calculateTransferEnergy_() + calculateCreationEnergy_());
	}


	float GeneralizedBornModel::calculateSolvationEnergy()
		
	{
		// We have to calculate Born radii every time we do the calculation of
		// the energy because the distances between atoms does influence the
		// effective radii. This could be done more sophisticated in order to
		// speed up the calculation, no doubt.
		Timer timer;
		timer.start();
		calculateBornRadii_();
		timer.stop();
		Log.info() << "calculateBornRadii_(): " << timer.getCPUTime() 
			<< "s" << std::endl;

		// Solvation energy is just the transfer energy with an internal
		// dieelectric constant of 1. The Coulomb part is constant and vanishes
		// in the difference.
		setSoluteDC(1.0f);
		return(calculateTransferEnergy_());
	}


	bool GeneralizedBornModel::readScalingFactors(const String& filename)
	{

		Path path;
		String tmp = path.find(filename);
		if (tmp == "") tmp = filename;

		Parameters gb_parameter_file(tmp);
		GeneralizedBornModel::GBParameters gb_parameters;
		if (!gb_parameters.extractSection(gb_parameter_file, "ScalingFactors"))
		{
			Log.error() << "Error reading paramters" << std::endl;
			return(false);
		}
		scaling_factors_ = gb_parameters.getScalingFactors();

		dc_solute_ = gb_parameters.getSoluteDC();
		dc_solvent_ = gb_parameters.getSolventDC();
		kappa_ = gb_parameters.getKappa();

		return(true);
	}


	float GeneralizedBornModel::calculateCreationEnergy_() 
		const
		
	{
		Timer timer;
		timer.start();

		// This is just a simple Coulomb-Calculation. 
		
		float energy = 0.0f;

		// iterate over all atom pairs
		for (Size i = 0; i < pair_list_.size(); i++)
		{
			float contribution = 0.0f;
			float c = pair_charges_[i];
			if (c != 0.0f) contribution = c / pair_distances_[i];
			energy += contribution;
		}

		// The pair list delivers every pair only once! So we don't
		// have to divide by two like given in the formula 
		/*
		  $$
				W = \frac{1}{2} \frac{1}{4 \pi \varepsilon_0 \varepsilon_r} 
					\sum_{i \neq j} \frac{q_i q_j}{r_{ij}}
			$$
		*/

		// Divide relative permittivity
		energy /= dc_solute_;

		// Convert to the correct dimension kJ/mol
		energy *= to_kJ_mol_;

		// DEBUG
		cout << "calculateCreationEnergy_(): " << energy << endl;
		// /DEBUG

		timer.stop();
		Log.info() << "calculateCreationEnergy_(): " << timer.getCPUTime() 
			<< "s" << std::endl;

		return(energy);

	}


	float GeneralizedBornModel::calculateTransferEnergy_() 
		const
		
	{
		Timer timer;
		timer.start();

		// This is a standard Generalized Born (see Still et al.) plus the salt
		// effect calculations developed by Srinivasan et al. (Theor. Chem.
		// Acc. 101:426-434, 1999)

		float energy = 0.0f;

		// inverse dcs
		float inv_dc_solute = 1.0f/dc_solute_;
		float inv_dc_solvent = 1.0f/dc_solvent_;

		// First calculate those terms considering different atoms
		for (Size k = 0; k < pair_list_.size(); ++k)
		{
			Size i = pair_list_[k].first;
			Size j = pair_list_[k].second;
			float c = pair_charges_[k];
			if (c != 0.0)
			{
				// Calculate the evil f term and the resulting energy
				// contribution
				float f_GB = f_GB_(i, j);
				float e = c / f_GB;

				// DEBUG
				// cout << "e: " << e << endl;
				// /DEBUG

				// Include salt effects if necessary
				// Maybe this could be done more efficiently
				if (kappa_ != 0.0f)
				{
					e *= (inv_dc_solute - inv_dc_solvent * exp(-kappa_ * f_GB));
				}

				energy += e;
			}
		}

		// Now calculate self energies
		for (Size i = 0; i < atoms_.size(); ++i)
		{
			float c = atoms_[i]->getCharge();
			if (c != 0.0f)
			{
				float e = c * c / born_radii_[i];
				
				// DEBUG
				// cout << "e self: " << 0.5f * e << endl;
				// /DEBUG

				energy += 0.5f * e;
			}
		}

		// This is the simple term without salt effects
		if (kappa_ == 0.0f)
		{
			energy *= (inv_dc_solute - inv_dc_solvent);
		}

		// Change sign (this contribution is negative) and Convert to the unit
		// of kJ/mol
		energy *= - to_kJ_mol_;

		// DEBUG
		cout << "calculateTransferEnergy_(): " << energy << endl;
		// /DEBUG

		timer.stop();
		Log.info() << "calculateTransferEnergy_(): " << timer.getCPUTime() 
			<< "s" << std::endl;

		return(energy);

	}


	float GeneralizedBornModel::f_GB_(Size i, Size j) const
		
	{
		// we need the effective Born radii for computing this smooth volume
		// function
		float R_iR_j = born_radii_[i] * born_radii_[j];
		
		// we need the squared distance
		float squared_distance = squared_distances_[i][j];

		// This is the term derived by Still et al. (JACS 112:6127-6129, 1990)
		float result = sqrt(squared_distance 
				+ R_iR_j * exp(-squared_distance / (4.0f * R_iR_j)));

		// There are other terms possible. Onufriew et al. propose 
		// (J Comput Chem 23:1297-1304, 2002) several other terms:

		/*
		float beta = 0.0f;
		float S;

		S = 1.0f / (1 + squared_distance/(4.0f * R_iR_j));
		S = 1.0f / sqrt(1 + squared_distance/(4.0f * R_iR_j));
		S = 1.0f / (1 + log(1.0f + squared_distance/(4.0f * R_iR_j)));

		result = sqrt(squared_distance 
				+ beta * sqrt(R_iR_j)
				+ R_iR_j * S);
		*/

		// DEBUG
		// cout << "f_GB_(" << i << ", " << j << "):" << result << endl;
		// /DEBUG

		return(result);
	}


	float GeneralizedBornModel::coulombIntegralApproximation_(float r_ij,
			float scaled_radius_j, float radius_i) const
		
	{
		// This is the approximation by Hawkins, Cramer, Truhlar (based on the
		// calculations of Schaefer and Froemmel
		//
		// Hawkins et al,, Chem Phys Lett 246:122-129, 1995
		// Hawkins et al., J Phys Chem 100:19824-19839, 1996
		// Schaefer and Froemmel, J Mol Biol 216:1045-1066, 1990

		// helper variables
		float L_ij;
		float U_ij;

		float r_plus_Salpha = r_ij + scaled_radius_j;
		float r_minus_Salpha = r_ij - scaled_radius_j;
		
		if (r_plus_Salpha <= radius_i)
		{
			L_ij = 1.0f;
			U_ij = 1.0f;
		}
		else
		{
			if (r_minus_Salpha <= radius_i)
			{
				L_ij = radius_i;
			}
			else
			{
				L_ij = r_minus_Salpha;
			}

			U_ij = r_plus_Salpha;
		}

		// Difference of the squared limits
		float D = 1.0f / ( L_ij * L_ij ) - 1.0f / ( U_ij * U_ij );

		float tmp 
			= 1.0f / L_ij 
			- 1.0f / U_ij 
			+ r_ij / 4.0f * -D
			+ 1.0f / ( 2.0f * r_ij ) * log( L_ij / U_ij )
			+ ( scaled_radius_j * scaled_radius_j )
			/ ( 4.0f * r_ij ) * D;

		return(tmp);
	}

	
	void GeneralizedBornModel::setScalingFactorFile(const String& filename)
		
	{
		scaling_factor_filename_ = filename;
	}


	void GeneralizedBornModel::setSolventDC(float solvent_dc)
		
	{
		dc_solvent_ = solvent_dc;
	}


	void GeneralizedBornModel::setSoluteDC(float solute_dc)
		
	{
		dc_solute_ = solute_dc;
	}


	void GeneralizedBornModel::calculatePotential(HashMap<const Atom*,
			float>& p_hash) const
		
	{
		p_hash.clear();

		AtomConstIterator it = ac_->beginAtom();
		for (; +it; ++it)
		{
			p_hash[&*it] = calculatePotential(*it);
		}
	}


	float GeneralizedBornModel::calculatePotential(const Atom& atom_i) const
		
	{

		Timer timer;
		timer.start();

		// We have to find the index of the atom in question
		Index index = -1;
		for (Size i = 0; i < atoms_.size(); ++i)
		{
			if (atoms_[i] == &atom_i) index = i;
		}
		if (index < 0)
		{
			Log.error() << "calculatePotential(): Could not find atom index!"
				<< std::endl;
			return(0.0f);
		}

		// the potential
		float potential = 0.0f;

		// inverse dcs
		float inv_dc_solute = 1.0f/dc_solute_;
		float inv_dc_solvent = 1.0f/dc_solvent_;

		// Now iterate over all atoms and calculate the potential at the
		// position of atom_i
		for (Index j = 0; (Size) j < atoms_.size(); ++j)
		{
			// Get the charge of the atom
			float c = atoms_[j]->getCharge();

			// compute potential contribution only if charge is not zero
			if (c != 0.0)
			{
				// Calculate the evil f term and the resulting energy
				// contribution
				float f_GB;

				// If this is the self-energy part, take the effective Born radius
				// which is equal to f_GB_(i, i). Otherwise compute f_GB_().
				if (index == j)
				{
					f_GB = born_radii_[index];
				}
				else
				{
					f_GB = f_GB_(index, j);
				}

				// Add up this contribution to the potential.
				float p = - c / f_GB;

				// DEBUG
				// cout << "p: " << p << " c: " << c << " f_GB: " << f_GB << endl;
				// /DEBUG

				// Include salt effects if necessary
				// Maybe this could be done more efficiently
				if (kappa_ != 0.0f)
				{
					p *= (inv_dc_solute - inv_dc_solvent * exp(-kappa_ * f_GB));
				} 
				potential += p;

				// pc is the coulomb part of the potential
				float pc = 0.0f;

				// Compute the Coulomb part of the potential
				if (index != j)
				{
					pc = inv_dc_solute * c / sqrt(squared_distances_[index][j]);
					potential += pc;
					// DEBUG
					// cout << "pc: " << pc << endl;
					// /DEBUG
				}
			}
		}

		// Multiply with DC effect term. This is the simple term without salt
		// effects.
		if (kappa_ == 0.0f)
		{
			potential *= (inv_dc_solute - inv_dc_solvent);
		}

		// Multiply charge unit
		potential *= to_kJ_mol_ / Constants::e0;

		// Stop timer
		timer.stop();

		/*
		Log.info() << "calculatePotential(): " << timer.getCPUTime() 
			<< "s" << std::endl;
		*/

		return(potential);


	}


	GeneralizedBornModel::GBParameters::~GBParameters()
		
	{
	}


	GeneralizedBornModel::GBParameters::GBParameters()
		
		: ParameterSection(),
			scaling_factors_()
	{
	}


	const StringHashMap<float>& GeneralizedBornModel::GBParameters::getScalingFactors() const
		
	{
		return(scaling_factors_);
	}


	float GeneralizedBornModel::GBParameters::getSoluteDC() const
		
	{
		return(dc_solute_);
	}


	float GeneralizedBornModel::GBParameters::getSolventDC() const
		
	{
		return(dc_solvent_);
	}


	float GeneralizedBornModel::GBParameters::getKappa() const
		
	{
		return(kappa_);
	}


	bool GeneralizedBornModel::GBParameters::extractSection(Parameters&
			parameters, const String& section_name)
		
	{
		if (!parameters.isValid())
		{
			Log.error() << "Parameters are invalid." << std::endl;
			return(false);
		}

		ParameterSection::extractSection(parameters, section_name);

		if (!hasVariable("factor"))
		{
			Log.error() << "Missing variables" << std::endl;
			return(false);
		}

		Size number_of_keys = getNumberOfKeys();
		Index factor_index = getColumnIndex("factor");

		// clear the HashMap and fill it with the new values
		scaling_factors_.clear();

		for (Size i = 0; i < number_of_keys; ++i)
		{
			String element_symbol = getKey(i);
			
			if (scaling_factors_.has(element_symbol))
			{
				Log.error() << "Duplicate scaling factor for element " 
					<< element_symbol << std::endl;
				return(false);
			}

			// DEBUG
			std::cout << "inserting " << element_symbol << " with factor "
				<< getValue(i, factor_index).toFloat() << std::endl;
			// /DEBUG
			scaling_factors_[element_symbol] = getValue(i, factor_index).toFloat();

		}

		if (options.has("dc_solute"))
		{
			dc_solute_ = options.getReal("dc_solute");
			Log.info() << "Setting solute dc to " << dc_solute_ << std::endl;
		}

		if (options.has("dc_solvent"))
		{
			dc_solvent_ = options.getReal("dc_solvent");
			Log.info() << "Setting solvent dc to " << dc_solvent_ << std::endl;
		}

		if (options.has("lambda"))
		{
			lambda_ = options.getReal("lambda");
			Log.info() << "(still disabled) Setting lambda to " 
				<< lambda_ << std::endl;
		}

		if (options.has("delta"))
		{
			delta_ = options.getReal("delta");
			Log.info() << "(still disabled) Setting delta to " 
				<< delta_ << std::endl;
		}

		if (options.has("kappa"))
		{
			kappa_ = options.getReal("kappa");
			Log.info() << "Setting kappa to " 
				<< kappa_ << std::endl;
		}

		if (options.has("rho_0"))
		{
			rho_0_ = options.getReal("rho_0");
			Log.info() << "(still disabled) Setting rho_0 to " 
				<< rho_0_ << std::endl;
		}

		valid_ = true;
		return(true);

	}

}