source: src/Jobs/InterfaceVMGJob.cpp@ 17fcbe7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 17fcbe7 was 17fcbe7, checked in by Frederik Heber <heber@…>, 13 years ago

FIX: Correcting normalization in InterfaceVMGJob among others.

  • with lots of help from Julian Iseringhausen.
  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * InterfaceVMGJob.cpp
25 *
26 * Created on: 10.06.2012
27 * Author: Frederik Heber
28 */
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34#ifdef HAVE_MPI
35#include "mpi.h"
36#endif
37
38#include "CodePatterns/MemDebug.hpp"
39
40#include <cmath>
41#include <iostream>
42
43#include "CodePatterns/Log.hpp"
44
45#include "base/vector.hpp"
46#include "base/math.hpp"
47#include "comm/comm.hpp"
48#include "grid/grid.hpp"
49#include "grid/multigrid.hpp"
50#include "mg.hpp"
51
52#include "InterfaceVMGJob.hpp"
53
54using namespace VMG;
55using VMGInterfaces::InterfaceVMGJob;
56
57InterfaceVMGJob::InterfaceVMGJob(const std::vector< double > &_sampled_input,
58 VMGData &_returndata,
59 const std::vector< std::vector<double> > &_particle_positions,
60 const std::vector< double > &_particle_charges,
61 VMG::Boundary boundary,
62 int levelMin,
63 int levelMax,
64 const VMG::Vector &box_begin,
65 vmg_float box_end,
66 const int& near_field_cells,
67 int coarseningSteps,
68 double alpha) :
69 VMG::Interface(boundary, levelMin, levelMax,
70 box_begin, box_end, coarseningSteps, alpha),
71 spl(near_field_cells, Extent(MaxLevel()).MeshWidth().Max()),
72 sampled_input(_sampled_input),
73 returndata(_returndata),
74 level(levelMax)
75{
76 std::vector< std::vector<double> >::const_iterator positer = _particle_positions.begin();
77 std::vector<double>::const_iterator chargeiter = _particle_charges.begin();
78 double pos[3];
79 for (; positer != _particle_positions.end(); ++positer, ++chargeiter) {
80 ASSERT( (*positer).size() == 3,
81 "InterfaceVMGJob::InterfaceVMGJob() - particle "
82 +toString(distance(_particle_positions.begin(), positer))+" has not exactly 3 coordinates.");
83 for (size_t i=0;i<3;++i)
84 pos[i] = (*positer)[i];
85 particles.push_back(Particle::Particle(pos, *chargeiter));
86 }
87}
88
89InterfaceVMGJob::~InterfaceVMGJob()
90{}
91
92void InterfaceVMGJob::ImportRightHandSide(Multigrid& multigrid)
93{
94 Index i;
95 Vector pos;
96 // VMG::TempGrid *temp_grid = new VMG::TempGrid(129, 0, 0., 1.);
97
98 Grid& grid = multigrid(multigrid.MaxLevel());
99 //grid.ClearBoundary(); // we don't have a boundary under periodic boundary conditions
100
101 /// 1. assign nuclei as smeared-out charges to the grid
102
103 /*
104 * Charge assignment on the grid
105 */
106 Comm& comm = *MG::GetComm();
107 Grid& particle_grid = comm.GetParticleGrid();
108
109 particle_grid.Clear();
110
111 assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(
112 VMG::MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS")));
113
114 // create smeared-out particle charges on particle_grid via splines
115 LOG(1, "INFO: Creating particle grid for " << particles.size() << " particles.");
116 for (std::list<Particle::Particle>::iterator iter = particles.begin();
117 iter != particles.end(); ++iter) {
118 LOG(2, "DEBUG: Current particle is at " << (*iter).Pos()
119 << " with charge " << (*iter).Charge() << ".");
120 spl.SetSpline(particle_grid, *iter);
121 }
122
123 // Communicate charges over halo
124 comm.CommFromGhosts(particle_grid);
125
126// // for normlization sum up charges
127// double particle_charges = 0.0;
128// for (std::list<Particle::Particle>::iterator iter=particles.begin(); iter!=particles.end(); ++iter)
129// particle_charges += iter->Charge();
130// particle_charges = MG::GetComm()->GlobalSumRoot(particle_charges);
131// LOG(1, "INFO: Total charge is " << particle_charges << ".");
132
133 // add sampled electron charge density onto grid
134 std::vector<double>::const_iterator sample_iter = sampled_input.begin();
135 for (Grid::iterator iter = particle_grid.Iterators().Local().Begin();
136 iter != particle_grid.Iterators().Local().End();
137 ++iter)
138 particle_grid(*iter) -= (*sample_iter++);
139/*
140 for (i.X()=grid.Local().Begin().X(); i.X()<grid.Local().End().X(); ++i.X())
141 for (i.Y()=grid.Local().Begin().Y(); i.Y()<grid.Local().End().Y(); ++i.Y())
142 for (i.Z()=grid.Local().Begin().Z(); i.Z()<grid.Local().End().Z(); ++i.Z())
143 particle_grid(i) -= (*sample_iter++); //temp_grid(i);
144*/
145 assert( sample_iter == sampled_input.end() );
146
147 // add particle_grid onto grid
148 for (int i=0; i<grid.Local().Size().X(); ++i)
149 for (int j=0; j<grid.Local().Size().Y(); ++j)
150 for (int k=0; k<grid.Local().Size().Z(); ++k)
151 grid(grid.Local().Begin().X() + i,
152 grid.Local().Begin().Y() + j,
153 grid.Local().Begin().Z() + k) = 4.0 * VMG::Math::pi *
154 particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
155 particle_grid.Local().Begin().Y() + j,
156 particle_grid.Local().Begin().Z() + k);
157
158 /// 2. add sampled electron density to the grid
159
160 const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
161
162 // print debugging info on grid size
163 LOG(1, "INFO: Mesh has extent " << grid.Extent().MeshWidth() << ".");
164 const int gridpoints = pow(2, level);
165 LOG(1, "INFO: gridpoints on finest level are " << gridpoints << ".");
166 LOG(1, "INFO: "
167 << "X in [" << grid.Local().Begin().X() << "," << grid.Local().End().X() << "],"
168 << "Y in [" << grid.Local().Begin().Y() << "," << grid.Local().End().Y() << "],"
169 << "Z in [" << grid.Local().Begin().Z() << "," << grid.Local().End().Z() << "].");
170
171 // calculate sum over grid times h^3 as check, should be roughly zero
172 const double element_volume = grid.Extent().MeshWidth().Product();
173 double charge_sum = 0.0;
174 for (Grid::iterator grid_iter = grid.Iterators().Local().Begin();
175 grid_iter != grid.Iterators().Local().End();
176 ++grid_iter)
177 charge_sum += grid.GetVal(*grid_iter);
178 charge_sum = element_volume * comm.GlobalSum(charge_sum);
179 comm.PrintStringOnce("Grid charge integral: %e", charge_sum/(4.0 * VMG::Math::pi));
180
181 // print input grid to vtk
182 comm.PrintGrid(grid, "grid after ImportRightHandSide");
183
184// delete temp_grid;
185}
186
187void InterfaceVMGJob::ExportSolution(Grid& grid)
188{
189 // grid now contains the sough-for potential
190 VMG::Comm& comm = *VMG::MG::GetComm();
191
192 const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
193 Index i;
194
195 // print output grid to vtk
196 comm.PrintGrid(grid, "grid after ExportSolution");
197
198 // obtain sampled potential from grid
199 returndata.sampled_potential.sampled_grid.clear();
200 for (i.X()=grid.Local().Begin().X(); i.X()<grid.Local().End().X(); ++i.X())
201 for (i.Y()=grid.Local().Begin().Y(); i.Y()<grid.Local().End().Y(); ++i.Y())
202 for (i.Z()=grid.Local().Begin().Z(); i.Z()<grid.Local().End().Z(); ++i.Z()) {
203 returndata.sampled_potential.sampled_grid.push_back(grid(i));
204 }
205
206 // calculate integral over potential as long-range energy contribution
207 const double element_volume =
208 grid.Extent().MeshWidth().X() * grid.Extent().MeshWidth().Y() * grid.Extent().MeshWidth().Z();
209 Grid::iterator grid_iter;
210 double potential_sum = 0.0;
211 for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
212 potential_sum += grid.GetVal(*grid_iter);
213 potential_sum = element_volume * comm.GlobalSum(potential_sum);
214 comm.PrintStringOnce("Grid potential sum: %e", potential_sum);
215
216 //Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
217 returndata.e_long = potential_sum;
218}
Note: See TracBrowser for help on using the repository browser.