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 | #ifdef HAVE_MPI
|
---|
48 | #include "comm/comm_mpi.hpp"
|
---|
49 | #else
|
---|
50 | #include "comm/comm_serial.hpp"
|
---|
51 | #endif
|
---|
52 | #include "grid/grid.hpp"
|
---|
53 | #include "grid/multigrid.hpp"
|
---|
54 |
|
---|
55 | #include "InterfaceVMGJob.hpp"
|
---|
56 |
|
---|
57 | using namespace VMG;
|
---|
58 | using VMGInterfaces::InterfaceVMGJob;
|
---|
59 |
|
---|
60 | InterfaceVMGJob::InterfaceVMGJob(const std::vector< double > &_sampled_input,
|
---|
61 | std::vector< double > &_sampled_output,
|
---|
62 | const std::vector< std::vector<double> > &_particle_positions,
|
---|
63 | const std::vector< double > &_particle_charges,
|
---|
64 | VMG::Boundary boundary,
|
---|
65 | int levelMin,
|
---|
66 | int levelMax,
|
---|
67 | const VMG::Vector &box_begin,
|
---|
68 | vmg_float box_end,
|
---|
69 | const int& near_field_cells,
|
---|
70 | int coarseningSteps,
|
---|
71 | double alpha) :
|
---|
72 | VMG::Interface(boundary, levelMin, levelMax,
|
---|
73 | box_begin, box_end, coarseningSteps, alpha),
|
---|
74 | spl(near_field_cells, Extent(MaxLevel()).MeshWidth().Max()),
|
---|
75 | sampled_input(_sampled_input),
|
---|
76 | sampled_output(_sampled_output),
|
---|
77 | level(levelMax)
|
---|
78 | {
|
---|
79 | std::vector< std::vector<double> >::const_iterator positer = _particle_positions.begin();
|
---|
80 | std::vector<double>::const_iterator chargeiter = _particle_charges.begin();
|
---|
81 | double pos[3];
|
---|
82 | for (; positer != _particle_positions.end(); ++positer, ++chargeiter) {
|
---|
83 | ASSERT( (*positer).size() == 3,
|
---|
84 | "InterfaceVMGJob::InterfaceVMGJob() - particle "
|
---|
85 | +toString(distance(_particle_positions.begin(), positer))+" has not exactly 3 coordinates.");
|
---|
86 | for (size_t i=0;i<3;++i)
|
---|
87 | pos[i] = (*positer)[i];
|
---|
88 | particles.push_back(Particle::Particle(pos, *chargeiter));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | InterfaceVMGJob::~InterfaceVMGJob()
|
---|
93 | {}
|
---|
94 |
|
---|
95 | void InterfaceVMGJob::ImportRightHandSide(Multigrid& multigrid)
|
---|
96 | {
|
---|
97 | Index i;
|
---|
98 | Vector pos;
|
---|
99 | // VMG::TempGrid *temp_grid = new VMG::TempGrid(129, 0, 0., 1.);
|
---|
100 |
|
---|
101 | Grid& grid = multigrid(multigrid.MaxLevel());
|
---|
102 | grid.ClearBoundary();
|
---|
103 |
|
---|
104 | /// 1. assign nuclei as smeared-out charges to the grid
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Charge assignment on the grid
|
---|
108 | */
|
---|
109 | #ifdef HAVE_MPI
|
---|
110 | CommMPI& comm = *dynamic_cast<CommMPI*>(VMG::MG::GetComm());
|
---|
111 | #else
|
---|
112 | CommSerial& comm = *dynamic_cast<CommSerial*>(VMG::MG::GetComm());
|
---|
113 | #endif
|
---|
114 | Grid& particle_grid = comm.GetParticleGrid();
|
---|
115 |
|
---|
116 | particle_grid.Clear();
|
---|
117 |
|
---|
118 | assert(particle_grid.Global().LocalSize().IsComponentwiseGreater(
|
---|
119 | VMG::MG::GetFactory().GetObjectStorageVal<int>("PARTICLE_NEAR_FIELD_CELLS")));
|
---|
120 |
|
---|
121 | for (std::list<Particle::Particle>::iterator iter = particles.begin();
|
---|
122 | iter != particles.end(); ++iter)
|
---|
123 | spl.SetSpline(particle_grid, *iter);
|
---|
124 |
|
---|
125 | // Communicate charges over halo
|
---|
126 | comm.CommFromGhosts(particle_grid);
|
---|
127 |
|
---|
128 | // Assign charge values to the right hand side
|
---|
129 | for (int i=0; i<grid.Local().Size().X(); ++i)
|
---|
130 | for (int j=0; j<grid.Local().Size().Y(); ++j)
|
---|
131 | for (int k=0; k<grid.Local().Size().Z(); ++k)
|
---|
132 | grid(grid.Local().Begin().X() + i,
|
---|
133 | grid.Local().Begin().Y() + j,
|
---|
134 | grid.Local().Begin().Z() + k) = 4.0 * VMG::Math::pi *
|
---|
135 | particle_grid.GetVal(particle_grid.Local().Begin().X() + i,
|
---|
136 | particle_grid.Local().Begin().Y() + j,
|
---|
137 | particle_grid.Local().Begin().Z() + k);
|
---|
138 |
|
---|
139 | /// 2. add sampled electron density to the grid
|
---|
140 |
|
---|
141 | const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
|
---|
142 |
|
---|
143 | LOG(1, "INFO: Mesh has extent " << grid.Extent().MeshWidth() << ".");
|
---|
144 | const int gridpoints = pow(2, level);
|
---|
145 | LOG(1, "INFO: gridpoints on finest level are " << gridpoints << ".");
|
---|
146 | assert( (grid.Extent().MeshWidth().X() * gridpoints) == 1 );
|
---|
147 | assert( (grid.Extent().MeshWidth().Y() * gridpoints) == 1 );
|
---|
148 | assert( (grid.Extent().MeshWidth().Z() * gridpoints) == 1 );
|
---|
149 | LOG(1, "INFO: "
|
---|
150 | << "X in [" << grid.Local().Begin().X() << "," << grid.Local().End().X() << "],"
|
---|
151 | << "Y in [" << grid.Local().Begin().Y() << "," << grid.Local().End().Y() << "],"
|
---|
152 | << "Z in [" << grid.Local().Begin().Z() << "," << grid.Local().End().Z() << "].");
|
---|
153 |
|
---|
154 | const double element_volume =
|
---|
155 | grid.Extent().MeshWidth().X() * grid.Extent().MeshWidth().Y() * grid.Extent().MeshWidth().Z();
|
---|
156 | std::vector<double>::const_iterator sample_iter = sampled_input.begin();
|
---|
157 | for (i.X()=grid.Local().Begin().X(); i.X()<grid.Local().End().X(); ++i.X())
|
---|
158 | for (i.Y()=grid.Local().Begin().Y(); i.Y()<grid.Local().End().Y(); ++i.Y())
|
---|
159 | for (i.Z()=grid.Local().Begin().Z(); i.Z()<grid.Local().End().Z(); ++i.Z())
|
---|
160 | grid(i) -= (*sample_iter++) * element_volume; //temp_grid(i);
|
---|
161 | assert( sample_iter == sampled_input.end() );
|
---|
162 |
|
---|
163 | Grid::iterator grid_iter;
|
---|
164 | double charge_sum = 0.0;
|
---|
165 | for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
|
---|
166 | charge_sum += grid.GetVal(*grid_iter);
|
---|
167 | // charge_sum = MG::GetComm()->GlobalSum(charge_sum);
|
---|
168 | // comm.PrintStringOnce("Grid charge sum: %e", charge_sum);
|
---|
169 | std::cout << "Grid charge sum: " << charge_sum << std::endl;
|
---|
170 |
|
---|
171 | // delete temp_grid;
|
---|
172 | }
|
---|
173 |
|
---|
174 | void InterfaceVMGJob::ExportSolution(Grid& grid)
|
---|
175 | {
|
---|
176 | // grid now contains the sough-for potential
|
---|
177 |
|
---|
178 | const Index begin_local = grid.Global().LocalBegin() - grid.Local().HaloSize1();
|
---|
179 | Index i;
|
---|
180 |
|
---|
181 | sampled_output.clear();
|
---|
182 | for (i.X()=grid.Local().Begin().X(); i.X()<grid.Local().End().X(); ++i.X())
|
---|
183 | for (i.Y()=grid.Local().Begin().Y(); i.Y()<grid.Local().End().Y(); ++i.Y())
|
---|
184 | for (i.Z()=grid.Local().Begin().Z(); i.Z()<grid.Local().End().Z(); ++i.Z()) {
|
---|
185 | sampled_output.push_back(grid(i));
|
---|
186 | }
|
---|
187 |
|
---|
188 | Grid::iterator grid_iter;
|
---|
189 | double potential_sum = 0.0;
|
---|
190 | for (grid_iter=grid.Iterators().Local().Begin(); grid_iter!=grid.Iterators().Local().End(); ++grid_iter)
|
---|
191 | potential_sum += grid.GetVal(*grid_iter);
|
---|
192 | // charge_sum = MG::GetComm()->GlobalSum(potential_sum);
|
---|
193 | // comm.PrintStringOnce("Grid potential sum: %e", potential_sum);
|
---|
194 | std::cout << "Grid potential sum: " << potential_sum << std::endl;
|
---|
195 |
|
---|
196 | //Particle::CommMPI& comm = *dynamic_cast<Particle::CommMPI*>(MG::GetComm());
|
---|
197 | //e_long = comm.GlobalSumRoot(e_long);
|
---|
198 |
|
---|
199 | }
|
---|