Skip to content

Commit 0795540

Browse files
robertodrbast
authored andcommitted
Enforce const-correctness in Meddle
- Remove the `mutable` qualifier from `infoStream_` and `functions_` data members. - Remove the `const` qualifier from methods that modify either or both of the two. In C++03 this requires quite some verbose `std::map` manipulations. I've put the C++11 version in preprocessor blocks as a reminder to do it properly when we move to C++11
1 parent ac789b2 commit 0795540

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/interface/Meddle.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ Meddle::Meddle(int nr_nuclei,
176176
cavity_(__nullptr),
177177
K_0_(__nullptr),
178178
K_d_(__nullptr),
179-
infoStream_("\n~~~~~~~~~~ PCMSolver ~~~~~~~~~~\n"),
180179
hasDynamic_(false) {
181180
TIMER_ON("Meddle::initInput");
182181
initInput(nr_nuclei, charges, coordinates, symmetry_info);
@@ -199,20 +198,20 @@ pcm::Meddle::~Meddle() {
199198
}
200199

201200
PCMSolverIndex pcmsolver_get_cavity_size(pcmsolver_context_t * context) {
202-
return (AS_TYPE(pcm::Meddle, context)->getCavitySize());
201+
return (AS_CTYPE(pcm::Meddle, context)->getCavitySize());
203202
}
204203
PCMSolverIndex pcm::Meddle::getCavitySize() const { return cavity_->size(); }
205204

206205
PCMSolverIndex pcmsolver_get_irreducible_cavity_size(pcmsolver_context_t * context) {
207-
return (AS_TYPE(pcm::Meddle, context)->getIrreducibleCavitySize());
206+
return (AS_CTYPE(pcm::Meddle, context)->getIrreducibleCavitySize());
208207
}
209208
PCMSolverIndex pcm::Meddle::getIrreducibleCavitySize() const {
210209
return cavity_->irreducible_size();
211210
}
212211

213212
void pcmsolver_get_centers(pcmsolver_context_t * context, double centers[]) {
214213
TIMER_ON("pcmsolver_get_centers");
215-
AS_TYPE(pcm::Meddle, context)->getCenters(centers);
214+
AS_CTYPE(pcm::Meddle, context)->getCenters(centers);
216215
TIMER_OFF("pcmsolver_get_centers");
217216
}
218217
void pcm::Meddle::getCenters(double centers[]) const {
@@ -223,14 +222,14 @@ void pcm::Meddle::getCenters(double centers[]) const {
223222
}
224223

225224
void pcmsolver_get_center(pcmsolver_context_t * context, int its, double center[]) {
226-
AS_TYPE(pcm::Meddle, context)->getCenter(its, center);
225+
AS_CTYPE(pcm::Meddle, context)->getCenter(its, center);
227226
}
228227
void pcm::Meddle::getCenter(int its, double center[]) const {
229228
Eigen::Map<Eigen::Vector3d>(center, 3, 1) = cavity_->elementCenter(its - 1);
230229
}
231230

232231
void pcmsolver_get_areas(pcmsolver_context_t * context, double areas[]) {
233-
AS_TYPE(pcm::Meddle, context)->getAreas(areas);
232+
AS_CTYPE(pcm::Meddle, context)->getAreas(areas);
234233
}
235234
void pcm::Meddle::getAreas(double areas[]) const {
236235
Eigen::Map<Eigen::VectorXd>(areas, cavity_->size(), 1) = cavity_->elementArea();
@@ -240,25 +239,34 @@ double pcmsolver_compute_polarization_energy(pcmsolver_context_t * context,
240239
const char * mep_name,
241240
const char * asc_name) {
242241
return (
243-
AS_TYPE(pcm::Meddle, context)
242+
AS_CTYPE(pcm::Meddle, context)
244243
->computePolarizationEnergy(std::string(mep_name), std::string(asc_name)));
245244
}
246245
double pcm::Meddle::computePolarizationEnergy(const std::string & mep_name,
247246
const std::string & asc_name) const {
248-
// Dot product of MEP and ASC surface function
249-
double energy = functions_[mep_name].dot(functions_[asc_name]);
247+
#ifdef HAS_CXX11
248+
double energy = functions_.at(mep_name).dot(functions_.at(asc_name));
249+
#else /* HAS_CXX11 */
250+
double energy =
251+
(functions_.find(mep_name)->second).dot(functions_.find(asc_name)->second);
252+
#endif /* HAS_CXX11 */
250253
return (energy / 2.0);
251254
}
252255

253256
double pcmsolver_get_asc_dipole(pcmsolver_context_t * context,
254257
const char * asc_name,
255258
double dipole[]) {
256259
return (
257-
AS_TYPE(pcm::Meddle, context)->getASCDipole(std::string(asc_name), dipole));
260+
AS_CTYPE(pcm::Meddle, context)->getASCDipole(std::string(asc_name), dipole));
258261
}
259262
double pcm::Meddle::getASCDipole(const std::string & asc_name,
260263
double dipole[]) const {
261-
Eigen::Vector3d asc_dipole = cavity_->elementCenter() * functions_[asc_name];
264+
#ifdef HAS_CXX11
265+
Eigen::Vector3d asc_dipole = cavity_->elementCenter() * functions_.at(asc_name);
266+
#else /* HAS_CXX11 */
267+
Eigen::Vector3d asc_dipole =
268+
cavity_->elementCenter() * functions_.find(asc_name)->second;
269+
#endif /* HAS_CXX11 */
262270
// Bind to host-allocated array
263271
Eigen::Map<Eigen::Vector3d>(dipole, 3, 1) = asc_dipole;
264272
return asc_dipole.norm();
@@ -275,7 +283,7 @@ void pcmsolver_compute_asc(pcmsolver_context_t * context,
275283
}
276284
void pcm::Meddle::computeASC(const std::string & mep_name,
277285
const std::string & asc_name,
278-
int irrep) const {
286+
int irrep) {
279287
// Get the proper iterators
280288
SurfaceFunctionMapConstIter iter_pot = functions_.find(mep_name);
281289
Eigen::VectorXd asc = K_0_->computeCharge(iter_pot->second, irrep);
@@ -300,7 +308,7 @@ void pcmsolver_compute_response_asc(pcmsolver_context_t * context,
300308
}
301309
void pcm::Meddle::computeResponseASC(const std::string & mep_name,
302310
const std::string & asc_name,
303-
int irrep) const {
311+
int irrep) {
304312
// Get the proper iterators
305313
SurfaceFunctionMapConstIter iter_pot = functions_.find(mep_name);
306314
Eigen::VectorXd asc(cavity_->size());
@@ -323,7 +331,8 @@ void pcmsolver_get_surface_function(pcmsolver_context_t * context,
323331
double values[],
324332
const char * name) {
325333
TIMER_ON("pcmsolver_get_surface_function");
326-
AS_TYPE(pcm::Meddle, context)->getSurfaceFunction(size, values, std::string(name));
334+
AS_CTYPE(pcm::Meddle, context)
335+
->getSurfaceFunction(size, values, std::string(name));
327336
TIMER_OFF("pcmsolver_get_surface_function");
328337
}
329338
void pcm::Meddle::getSurfaceFunction(PCMSolverIndex size,
@@ -349,7 +358,7 @@ void pcmsolver_set_surface_function(pcmsolver_context_t * context,
349358
}
350359
void pcm::Meddle::setSurfaceFunction(PCMSolverIndex size,
351360
double values[],
352-
const std::string & name) const {
361+
const std::string & name) {
353362
if (cavity_->size() != size)
354363
PCMSOLVER_ERROR("The " + name + " SurfaceFunction is bigger than the cavity!");
355364

@@ -363,21 +372,25 @@ void pcm::Meddle::setSurfaceFunction(PCMSolverIndex size,
363372

364373
void pcmsolver_print_surface_function(pcmsolver_context_t * context,
365374
const char * name) {
366-
AS_TYPE(pcm::Meddle, context)->printSurfaceFunction(std::string(name));
375+
AS_CTYPE(pcm::Meddle, context)->printSurfaceFunction(std::string(name));
367376
}
368377
void pcm::Meddle::printSurfaceFunction(const std::string & name) const {
369378
if (functions_.count(name) == 1) { // Key in map already
370379
std::ostringstream print_sf;
371380
Eigen::IOFormat fmt(Eigen::FullPrecision);
372-
print_sf << functions_[name].format(fmt) << std::endl;
381+
#ifdef HAS_CXX11
382+
print_sf << functions_.at(name).format(fmt) << std::endl;
383+
#else /* HAS_CXX11 */
384+
print_sf << (functions_.find(name)->second).format(fmt) << std::endl;
385+
#endif /* HAS_CXX11 */
373386
hostWriter_(print_sf);
374387
} else {
375388
PCMSOLVER_ERROR("You are trying to print a nonexistent SurfaceFunction!");
376389
}
377390
}
378391

379392
void pcmsolver_save_surface_functions(pcmsolver_context_t * context) {
380-
AS_TYPE(pcm::Meddle, context)->saveSurfaceFunctions();
393+
AS_CTYPE(pcm::Meddle, context)->saveSurfaceFunctions();
381394
}
382395
void pcm::Meddle::saveSurfaceFunctions() const {
383396
hostWriter_("\nDumping surface functions to .npy files");
@@ -392,7 +405,7 @@ void pcm::Meddle::saveSurfaceFunctions() const {
392405

393406
void pcmsolver_save_surface_function(pcmsolver_context_t * context,
394407
const char * name) {
395-
AS_TYPE(pcm::Meddle, context)->saveSurfaceFunction(std::string(name));
408+
AS_CTYPE(pcm::Meddle, context)->saveSurfaceFunction(std::string(name));
396409
}
397410
void pcm::Meddle::saveSurfaceFunction(const std::string & name) const {
398411
SurfaceFunctionMapConstIter it = functions_.find(name);
@@ -403,7 +416,7 @@ void pcmsolver_load_surface_function(pcmsolver_context_t * context,
403416
const char * name) {
404417
AS_TYPE(pcm::Meddle, context)->loadSurfaceFunction(std::string(name));
405418
}
406-
void pcm::Meddle::loadSurfaceFunction(const std::string & name) const {
419+
void pcm::Meddle::loadSurfaceFunction(const std::string & name) {
407420
hostWriter_("\nLoading surface function " + name + " from .npy file");
408421
Eigen::VectorXd values = cnpy::custom::npy_load<double>(name + ".npy");
409422
if (values.size() != cavity_->size())
@@ -418,12 +431,12 @@ void pcm::Meddle::loadSurfaceFunction(const std::string & name) const {
418431
}
419432

420433
void pcmsolver_write_timings(pcmsolver_context_t * context) {
421-
AS_TYPE(pcm::Meddle, context)->writeTimings();
434+
AS_CTYPE(pcm::Meddle, context)->writeTimings();
422435
}
423436
void pcm::Meddle::writeTimings() const { TIMER_DONE("pcmsolver.timer.dat"); }
424437

425438
void pcmsolver_print(pcmsolver_context_t * context) {
426-
AS_TYPE(pcm::Meddle, context)->printInfo();
439+
AS_CTYPE(pcm::Meddle, context)->printInfo();
427440
}
428441
void pcm::Meddle::printInfo() const {
429442
hostWriter_(citation_message());
@@ -511,7 +524,7 @@ void Meddle::initDynamicSolver() {
511524
delete gf_i;
512525
}
513526

514-
void Meddle::mediumInfo(IGreensFunction * gf_i, IGreensFunction * gf_o) const {
527+
void Meddle::mediumInfo(IGreensFunction * gf_i, IGreensFunction * gf_o) {
515528
using utils::Solvent;
516529
infoStream_ << "============ Medium " << std::endl;
517530
if (input_.fromSolvent()) {

src/interface/Meddle.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class PCMSolver_API Meddle __final {
168168
*/
169169
void computeASC(const std::string & mep_name,
170170
const std::string & asc_name,
171-
int irrep) const;
171+
int irrep);
172172

173173
/*! \brief Computes response ASC given a MEP and the desired irreducible
174174
* representation
@@ -182,7 +182,7 @@ class PCMSolver_API Meddle __final {
182182
*/
183183
void computeResponseASC(const std::string & mep_name,
184184
const std::string & asc_name,
185-
int irrep) const;
185+
int irrep);
186186

187187
/*! \brief Computes the polarization energy
188188
* \param[in] mep_name label of the MEP surface function
@@ -215,7 +215,7 @@ class PCMSolver_API Meddle __final {
215215
*/
216216
void setSurfaceFunction(PCMSolverIndex size,
217217
double values[],
218-
const std::string & name) const;
218+
const std::string & name);
219219

220220
/*! \brief Prints surface function contents to host output
221221
* \param[in] name label of the surface function
@@ -241,7 +241,7 @@ class PCMSolver_API Meddle __final {
241241
* \note The name parameter is the name of the NumPy array file
242242
* **without** .npy extension
243243
*/
244-
void loadSurfaceFunction(const std::string & name) const;
244+
void loadSurfaceFunction(const std::string & name);
245245

246246
/*! \brief Prints citation and set up information
247247
*/
@@ -278,9 +278,9 @@ class PCMSolver_API Meddle __final {
278278
/*! Whether K_d_ was initialized */
279279
bool hasDynamic_;
280280
/*! PCMSolver set up information */
281-
mutable std::ostringstream infoStream_;
281+
std::ostringstream infoStream_;
282282
/*! SurfaceFunction map */
283-
mutable SurfaceFunctionMap functions_;
283+
SurfaceFunctionMap functions_;
284284
/*! Common implemenation for the CTOR-s */
285285
void CTORBody();
286286
/*! \brief Initialize input_
@@ -300,6 +300,6 @@ class PCMSolver_API Meddle __final {
300300
/*! Initialize dynamic solver K_d_ */
301301
void initDynamicSolver();
302302
/*! Collect info on medium */
303-
void mediumInfo(IGreensFunction * gf_i, IGreensFunction * gf_o) const;
303+
void mediumInfo(IGreensFunction * gf_i, IGreensFunction * gf_o);
304304
};
305305
} // namespace pcm

0 commit comments

Comments
 (0)