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 | * VMGJob.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jul 13, 2012
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #ifdef HAVE_MPI
|
---|
37 | #include "mpi.h"
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | // include headers that implement a archive in simple text format
|
---|
41 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect
|
---|
42 | #include <boost/archive/text_oarchive.hpp>
|
---|
43 | #include <boost/archive/text_iarchive.hpp>
|
---|
44 |
|
---|
45 | #include "mg.hpp"
|
---|
46 | #include "base/object.hpp"
|
---|
47 | #include "base/defs.hpp"
|
---|
48 |
|
---|
49 | #ifdef HAVE_MPI
|
---|
50 | #include "comm/comm_mpi.hpp"
|
---|
51 | #include "comm/domain_decomposition_mpi.hpp"
|
---|
52 | #else
|
---|
53 | #include "comm/comm_serial.hpp"
|
---|
54 | #endif
|
---|
55 | #include "cycles/cycle_cs_periodic.hpp"
|
---|
56 | #include "grid/multigrid.hpp"
|
---|
57 | //#include "grid/tempgrid.hpp"
|
---|
58 | #include "level/level_operator_cs.hpp"
|
---|
59 | #include "level/stencils.hpp"
|
---|
60 | #include "samples/discretization_poisson_fd.hpp"
|
---|
61 | #include "smoother/gsrb_poisson_4.hpp"
|
---|
62 | #include "solver/givens.hpp"
|
---|
63 | //#include "solver/solver_regular.hpp"
|
---|
64 | #include "solver/solver_singular.hpp"
|
---|
65 | #include "units/particle/comm_mpi_particle.hpp"
|
---|
66 |
|
---|
67 | #include "CodePatterns/MemDebug.hpp"
|
---|
68 |
|
---|
69 | #include "Jobs/VMGJob.hpp"
|
---|
70 |
|
---|
71 | #include "LinearAlgebra/defs.hpp"
|
---|
72 | #include "Jobs/InterfaceVMGJob.hpp"
|
---|
73 |
|
---|
74 | #include "CodePatterns/Assert.hpp"
|
---|
75 |
|
---|
76 | using namespace VMG;
|
---|
77 |
|
---|
78 | VMGJob::VMGJob(
|
---|
79 | const JobId_t _JobId,
|
---|
80 | const SamplingGrid &_density_grid,
|
---|
81 | const std::vector< std::vector< double > > &_particle_positions,
|
---|
82 | const std::vector< double > &_particle_charges,
|
---|
83 | const size_t _near_field_cells,
|
---|
84 | const size_t _interpolation_degree,
|
---|
85 | const bool _DoPrintDebug) :
|
---|
86 | FragmentJob(_JobId),
|
---|
87 | density_grid(_density_grid),
|
---|
88 | particle_positions(_particle_positions),
|
---|
89 | particle_charges(_particle_charges),
|
---|
90 | near_field_cells(_near_field_cells),
|
---|
91 | interpolation_degree(_interpolation_degree),
|
---|
92 | returndata(static_cast<const SamplingGridProperties &>(_density_grid)),
|
---|
93 | particles(),
|
---|
94 | DoPrintDebug(_DoPrintDebug)
|
---|
95 | {}
|
---|
96 |
|
---|
97 | VMGJob::VMGJob() :
|
---|
98 | FragmentJob(JobId::IllegalJob),
|
---|
99 | near_field_cells(3),
|
---|
100 | interpolation_degree(3),
|
---|
101 | particles(),
|
---|
102 | DoPrintDebug(false)
|
---|
103 | {}
|
---|
104 |
|
---|
105 | VMGJob::~VMGJob()
|
---|
106 | {
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | VMGJob::particle_arrays::particle_arrays() :
|
---|
111 | num_particles(0),
|
---|
112 | f(NULL),
|
---|
113 | x(NULL),
|
---|
114 | p(NULL),
|
---|
115 | q(NULL)
|
---|
116 | {}
|
---|
117 |
|
---|
118 | VMGJob::particle_arrays::~particle_arrays()
|
---|
119 | {
|
---|
120 | delete[] f;
|
---|
121 | delete[] x;
|
---|
122 | delete[] p;
|
---|
123 | delete[] q;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void VMGJob::particle_arrays::init(const size_t _num_particles)
|
---|
127 | {
|
---|
128 | num_particles = _num_particles;
|
---|
129 | f = new double[num_particles*3];
|
---|
130 | x = new double[num_particles*3];
|
---|
131 | p = new double[num_particles];
|
---|
132 | q = new double[num_particles];
|
---|
133 | }
|
---|
134 |
|
---|
135 | void VMGJob::InitVMGArrays()
|
---|
136 | {
|
---|
137 | particles.init(particle_charges.size());
|
---|
138 |
|
---|
139 | {
|
---|
140 | size_t index=0;
|
---|
141 | for (std::vector< std::vector< double > >::const_iterator iter = particle_positions.begin();
|
---|
142 | iter != particle_positions.end(); ++iter) {
|
---|
143 | for (std::vector< double >::const_iterator positer = (*iter).begin();
|
---|
144 | positer != (*iter).end(); ++positer) {
|
---|
145 | particles.f[index] = 0.;
|
---|
146 | particles.x[index++] = *positer;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | ASSERT( index == particles.num_particles*3,
|
---|
150 | "VMGJob::VMGJob() - too many particles or components in particle_positions.");
|
---|
151 | }
|
---|
152 | {
|
---|
153 | size_t index = 0;
|
---|
154 | for (std::vector< double >::const_iterator iter = particle_charges.begin();
|
---|
155 | iter != particle_charges.end(); ++iter) {
|
---|
156 | particles.p[index] = 0.;
|
---|
157 | particles.q[index++] = *iter;
|
---|
158 | }
|
---|
159 | ASSERT( index == particles.num_particles,
|
---|
160 | "VMGJob::VMGJob() - too many charges in particle_charges.");
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | FragmentResult::ptr VMGJob::Work()
|
---|
166 | {
|
---|
167 | // initialize VMG library solver
|
---|
168 | InitVMG();
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Start the multigrid solver
|
---|
172 | */
|
---|
173 | MG::Solve();
|
---|
174 |
|
---|
175 | /// create and fill result object
|
---|
176 | // place into returnstream
|
---|
177 | std::stringstream returnstream;
|
---|
178 | boost::archive::text_oarchive oa(returnstream);
|
---|
179 | oa << returndata;
|
---|
180 |
|
---|
181 | FragmentResult::ptr ptr( new FragmentResult(getId(), returnstream.str()) );
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Delete all data.
|
---|
185 | */
|
---|
186 | MG::Destroy();
|
---|
187 |
|
---|
188 | return ptr;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Initialization of VMG library.
|
---|
192 | *
|
---|
193 | * The contents is heavily inspired from interface_fcs.cpp: VMG_fcs_init() of
|
---|
194 | * the ScaFaCoS project.
|
---|
195 | *
|
---|
196 | */
|
---|
197 | void VMGJob::InitVMG()
|
---|
198 | {
|
---|
199 | // TODO: As a matter of fact should use open boundary conditions
|
---|
200 | const Boundary boundary(Periodic, Periodic, Periodic);
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Choose multigrid components (self-registering)
|
---|
204 | */
|
---|
205 | #ifdef HAVE_MPI
|
---|
206 | new Particle::CommMPI(boundary, new DomainDecompositionMPI());
|
---|
207 | #else
|
---|
208 | new CommSerial(boundary);
|
---|
209 | #endif
|
---|
210 | new DiscretizationPoissonFD(4);
|
---|
211 | new VMGInterfaces::InterfaceVMGJob(
|
---|
212 | density_grid,
|
---|
213 | returndata,
|
---|
214 | particle_positions,
|
---|
215 | particle_charges,
|
---|
216 | boundary,
|
---|
217 | 2,
|
---|
218 | density_grid.level,
|
---|
219 | Vector(density_grid.begin),
|
---|
220 | density_grid.end[0]-density_grid.begin[0],
|
---|
221 | near_field_cells,
|
---|
222 | DoPrintDebug);
|
---|
223 | new LevelOperatorCS(Stencils::RestrictionFullWeight, Stencils::InterpolationTrilinear);
|
---|
224 | new Givens<SolverSingular>();
|
---|
225 | const int cycle_type = 1; // V-type
|
---|
226 | new CycleCSPeriodic(cycle_type);
|
---|
227 | new GaussSeidelRBPoisson4();
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Register required parameters
|
---|
231 | */
|
---|
232 | new ObjectStorage<int>("PRESMOOTHSTEPS", 3);
|
---|
233 | new ObjectStorage<int>("POSTSMOOTHSTEPS", 3);
|
---|
234 | new ObjectStorage<vmg_float>("PRECISION", 1.0e-10);
|
---|
235 | new ObjectStorage<int>("MAX_ITERATION", 15);
|
---|
236 | new ObjectStorage<int>("PARTICLE_NEAR_FIELD_CELLS", near_field_cells);
|
---|
237 | new ObjectStorage<int>("PARTICLE_INTERPOLATION_DEGREE", interpolation_degree);
|
---|
238 |
|
---|
239 | // first initialize f,x,p,q,... from STL vectors
|
---|
240 | InitVMGArrays();
|
---|
241 | // then initialize as objects with VMG's storage
|
---|
242 | new ObjectStorage<vmg_float*>("PARTICLE_POS_ARRAY", particles.x);
|
---|
243 | new ObjectStorage<vmg_float*>("PARTICLE_CHARGE_ARRAY", particles.q);
|
---|
244 | new ObjectStorage<vmg_float*>("PARTICLE_POTENTIAL_ARRAY", particles.p);
|
---|
245 | new ObjectStorage<vmg_float*>("PARTICLE_FIELD_ARRAY", particles.f);
|
---|
246 | new ObjectStorage<vmg_int>("PARTICLE_NUM_LOCAL", particles.num_particles);
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Post init
|
---|
250 | */
|
---|
251 | MG::PostInit();
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Check whether the library is correctly initialized now.
|
---|
255 | */
|
---|
256 | MG::IsInitialized();
|
---|
257 | }
|
---|
258 |
|
---|
259 | // we need to explicitly instantiate the serialization functions as
|
---|
260 | // its is only serialized through its base class FragmentJob
|
---|
261 | BOOST_CLASS_EXPORT_IMPLEMENT(VMGJob)
|
---|