Skip to content

Commit 4e5c80a

Browse files
robertodrbast
authored andcommitted
Use strings in Meddle object
1 parent 5133636 commit 4e5c80a

File tree

2 files changed

+65
-72
lines changed

2 files changed

+65
-72
lines changed

src/interface/Meddle.cpp

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,25 @@ double pcmsolver_compute_polarization_energy(pcmsolver_context_t * context,
240240
const char * mep_name,
241241
const char * asc_name) {
242242
return (
243-
AS_TYPE(pcm::Meddle, context)->computePolarizationEnergy(mep_name, asc_name));
243+
AS_TYPE(pcm::Meddle, context)
244+
->computePolarizationEnergy(std::string(mep_name), std::string(asc_name)));
244245
}
245-
double pcm::Meddle::computePolarizationEnergy(const char * mep_name,
246-
const char * asc_name) const {
246+
double pcm::Meddle::computePolarizationEnergy(const std::string & mep_name,
247+
const std::string & asc_name) const {
247248
// Dot product of MEP and ASC surface function
248-
double energy =
249-
functions_[std::string(mep_name)].dot(functions_[std::string(asc_name)]);
249+
double energy = functions_[mep_name].dot(functions_[asc_name]);
250250
return (energy / 2.0);
251251
}
252252

253253
double pcmsolver_get_asc_dipole(pcmsolver_context_t * context,
254254
const char * asc_name,
255255
double dipole[]) {
256-
return (AS_TYPE(pcm::Meddle, context)->getASCDipole(asc_name, dipole));
256+
return (
257+
AS_TYPE(pcm::Meddle, context)->getASCDipole(std::string(asc_name), dipole));
257258
}
258-
double pcm::Meddle::getASCDipole(const char * asc_name, double dipole[]) const {
259-
Eigen::Vector3d asc_dipole =
260-
cavity_->elementCenter() * functions_[std::string(asc_name)];
259+
double pcm::Meddle::getASCDipole(const std::string & asc_name,
260+
double dipole[]) const {
261+
Eigen::Vector3d asc_dipole = cavity_->elementCenter() * functions_[asc_name];
261262
// Bind to host-allocated array
262263
Eigen::Map<Eigen::Vector3d>(dipole, 3, 1) = asc_dipole;
263264
return asc_dipole.norm();
@@ -268,25 +269,23 @@ void pcmsolver_compute_asc(pcmsolver_context_t * context,
268269
const char * asc_name,
269270
int irrep) {
270271
TIMER_ON("pcmsolver_compute_asc");
271-
AS_TYPE(pcm::Meddle, context)->computeASC(mep_name, asc_name, irrep);
272+
AS_TYPE(pcm::Meddle, context)
273+
->computeASC(std::string(mep_name), std::string(asc_name), irrep);
272274
TIMER_OFF("pcmsolver_compute_asc");
273275
}
274-
void pcm::Meddle::computeASC(const char * mep_name,
275-
const char * asc_name,
276+
void pcm::Meddle::computeASC(const std::string & mep_name,
277+
const std::string & asc_name,
276278
int irrep) const {
277-
std::string MEP(mep_name);
278-
std::string ASC(asc_name);
279-
280279
// Get the proper iterators
281-
SurfaceFunctionMapConstIter iter_pot = functions_.find(MEP);
280+
SurfaceFunctionMapConstIter iter_pot = functions_.find(mep_name);
282281
Eigen::VectorXd asc = K_0_->computeCharge(iter_pot->second, irrep);
283282
// Renormalize
284283
asc /= double(cavity_->pointGroup().nrIrrep());
285284
// Insert it into the map
286-
if (functions_.count(ASC) == 1) { // Key in map already
287-
functions_[ASC] = asc;
285+
if (functions_.count(asc_name) == 1) { // Key in map already
286+
functions_[asc_name] = asc;
288287
} else { // Create key-value pair
289-
functions_.insert(std::make_pair(ASC, asc));
288+
functions_.insert(std::make_pair(asc_name, asc));
290289
}
291290
}
292291

@@ -295,17 +294,15 @@ void pcmsolver_compute_response_asc(pcmsolver_context_t * context,
295294
const char * asc_name,
296295
int irrep) {
297296
TIMER_ON("pcmsolver_compute_response_asc");
298-
AS_TYPE(pcm::Meddle, context)->computeResponseASC(mep_name, asc_name, irrep);
297+
AS_TYPE(pcm::Meddle, context)
298+
->computeResponseASC(std::string(mep_name), std::string(asc_name), irrep);
299299
TIMER_OFF("pcmsolver_compute_response_asc");
300300
}
301-
void pcm::Meddle::computeResponseASC(const char * mep_name,
302-
const char * asc_name,
301+
void pcm::Meddle::computeResponseASC(const std::string & mep_name,
302+
const std::string & asc_name,
303303
int irrep) const {
304-
std::string MEP(mep_name);
305-
std::string ASC(asc_name);
306-
307304
// Get the proper iterators
308-
SurfaceFunctionMapConstIter iter_pot = functions_.find(MEP);
305+
SurfaceFunctionMapConstIter iter_pot = functions_.find(mep_name);
309306
Eigen::VectorXd asc(cavity_->size());
310307
if (hasDynamic_) {
311308
asc = K_d_->computeCharge(iter_pot->second, irrep);
@@ -314,10 +311,10 @@ void pcm::Meddle::computeResponseASC(const char * mep_name,
314311
}
315312
// Renormalize
316313
asc /= double(cavity_->pointGroup().nrIrrep());
317-
if (functions_.count(ASC) == 1) { // Key in map already
318-
functions_[ASC] = asc;
314+
if (functions_.count(asc_name) == 1) { // Key in map already
315+
functions_[asc_name] = asc;
319316
} else { // Create key-value pair
320-
functions_.insert(std::make_pair(ASC, asc));
317+
functions_.insert(std::make_pair(asc_name, asc));
321318
}
322319
}
323320

@@ -326,20 +323,18 @@ void pcmsolver_get_surface_function(pcmsolver_context_t * context,
326323
double values[],
327324
const char * name) {
328325
TIMER_ON("pcmsolver_get_surface_function");
329-
AS_TYPE(pcm::Meddle, context)->getSurfaceFunction(size, values, name);
326+
AS_TYPE(pcm::Meddle, context)->getSurfaceFunction(size, values, std::string(name));
330327
TIMER_OFF("pcmsolver_get_surface_function");
331328
}
332329
void pcm::Meddle::getSurfaceFunction(PCMSolverIndex size,
333330
double values[],
334-
const char * name) const {
335-
std::string functionName(name);
331+
const std::string & name) const {
336332
if (cavity_->size() != size)
337-
PCMSOLVER_ERROR("The " + functionName +
338-
" SurfaceFunction is bigger than the cavity!");
333+
PCMSOLVER_ERROR("The " + name + " SurfaceFunction is bigger than the cavity!");
339334

340-
SurfaceFunctionMapConstIter iter = functions_.find(functionName);
335+
SurfaceFunctionMapConstIter iter = functions_.find(name);
341336
if (iter == functions_.end())
342-
PCMSOLVER_ERROR("The " + functionName + " SurfaceFunction does not exist.");
337+
PCMSOLVER_ERROR("The " + name + " SurfaceFunction does not exist.");
343338

344339
Eigen::Map<Eigen::VectorXd>(values, size, 1) = iter->second;
345340
}
@@ -349,35 +344,32 @@ void pcmsolver_set_surface_function(pcmsolver_context_t * context,
349344
double values[],
350345
const char * name) {
351346
TIMER_ON("pcmsolver_set_surface_function");
352-
AS_TYPE(pcm::Meddle, context)->setSurfaceFunction(size, values, name);
347+
AS_TYPE(pcm::Meddle, context)->setSurfaceFunction(size, values, std::string(name));
353348
TIMER_OFF("pcmsolver_set_surface_function");
354349
}
355350
void pcm::Meddle::setSurfaceFunction(PCMSolverIndex size,
356351
double values[],
357-
const char * name) const {
358-
std::string functionName(name);
352+
const std::string & name) const {
359353
if (cavity_->size() != size)
360-
PCMSOLVER_ERROR("The " + functionName +
361-
" SurfaceFunction is bigger than the cavity!");
354+
PCMSOLVER_ERROR("The " + name + " SurfaceFunction is bigger than the cavity!");
362355

363356
Eigen::VectorXd func = Eigen::Map<Eigen::VectorXd>(values, size, 1);
364-
if (functions_.count(functionName) == 1) { // Key in map already
365-
functions_[functionName] = func;
357+
if (functions_.count(name) == 1) { // Key in map already
358+
functions_[name] = func;
366359
} else {
367-
functions_.insert(std::make_pair(functionName, func));
360+
functions_.insert(std::make_pair(name, func));
368361
}
369362
}
370363

371364
void pcmsolver_print_surface_function(pcmsolver_context_t * context,
372365
const char * name) {
373-
AS_TYPE(pcm::Meddle, context)->printSurfaceFunction(name);
366+
AS_TYPE(pcm::Meddle, context)->printSurfaceFunction(std::string(name));
374367
}
375-
void pcm::Meddle::printSurfaceFunction(const char * name) const {
376-
std::string functionName(name);
377-
if (functions_.count(functionName) == 1) { // Key in map already
368+
void pcm::Meddle::printSurfaceFunction(const std::string & name) const {
369+
if (functions_.count(name) == 1) { // Key in map already
378370
std::ostringstream print_sf;
379371
Eigen::IOFormat fmt(Eigen::FullPrecision);
380-
print_sf << functions_[functionName].format(fmt) << std::endl;
372+
print_sf << functions_[name].format(fmt) << std::endl;
381373
hostWriter_(print_sf);
382374
} else {
383375
PCMSOLVER_ERROR("You are trying to print a nonexistent SurfaceFunction!");
@@ -400,29 +392,28 @@ void pcm::Meddle::saveSurfaceFunctions() const {
400392

401393
void pcmsolver_save_surface_function(pcmsolver_context_t * context,
402394
const char * name) {
403-
AS_TYPE(pcm::Meddle, context)->saveSurfaceFunction(name);
395+
AS_TYPE(pcm::Meddle, context)->saveSurfaceFunction(std::string(name));
404396
}
405-
void pcm::Meddle::saveSurfaceFunction(const char * name) const {
397+
void pcm::Meddle::saveSurfaceFunction(const std::string & name) const {
406398
SurfaceFunctionMapConstIter it = functions_.find(name);
407-
cnpy::custom::npy_save(std::string(name) + ".npy", it->second);
399+
cnpy::custom::npy_save(name + ".npy", it->second);
408400
}
409401

410402
void pcmsolver_load_surface_function(pcmsolver_context_t * context,
411403
const char * name) {
412-
AS_TYPE(pcm::Meddle, context)->loadSurfaceFunction(name);
404+
AS_TYPE(pcm::Meddle, context)->loadSurfaceFunction(std::string(name));
413405
}
414-
void pcm::Meddle::loadSurfaceFunction(const char * name) const {
415-
std::string functionName(name);
416-
hostWriter_("\nLoading surface function " + functionName + " from .npy file");
417-
Eigen::VectorXd values = cnpy::custom::npy_load<double>(functionName + ".npy");
406+
void pcm::Meddle::loadSurfaceFunction(const std::string & name) const {
407+
hostWriter_("\nLoading surface function " + name + " from .npy file");
408+
Eigen::VectorXd values = cnpy::custom::npy_load<double>(name + ".npy");
418409
if (values.size() != cavity_->size())
419-
PCMSOLVER_ERROR("The loaded " + functionName +
410+
PCMSOLVER_ERROR("The loaded " + name +
420411
" surface function is bigger than the cavity!");
421412
// Append to global map
422-
if (functions_.count(functionName) == 1) { // Key in map already
423-
functions_[functionName] = values;
413+
if (functions_.count(name) == 1) { // Key in map already
414+
functions_[name] = values;
424415
} else {
425-
functions_.insert(std::make_pair(functionName, values));
416+
functions_.insert(std::make_pair(name, values));
426417
}
427418
}
428419

src/interface/Meddle.hpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ class PCMSolver_API Meddle __final {
166166
* and charges. Given labels for each, this function retrieves the MEP
167167
* and computes the corresponding ASC.
168168
*/
169-
void computeASC(const char * mep_name, const char * asc_name, int irrep) const;
169+
void computeASC(const std::string & mep_name,
170+
const std::string & asc_name,
171+
int irrep) const;
170172

171173
/*! \brief Computes response ASC given a MEP and the desired irreducible
172174
* representation
@@ -178,8 +180,8 @@ class PCMSolver_API Meddle __final {
178180
* permittivity
179181
* otherwise.
180182
*/
181-
void computeResponseASC(const char * mep_name,
182-
const char * asc_name,
183+
void computeResponseASC(const std::string & mep_name,
184+
const std::string & asc_name,
183185
int irrep) const;
184186

185187
/*! \brief Computes the polarization energy
@@ -188,14 +190,14 @@ class PCMSolver_API Meddle __final {
188190
* \return the polarization energy
189191
* This function calculates the dot product of the given MEP and ASC vectors.
190192
*/
191-
double computePolarizationEnergy(const char * mep_name,
192-
const char * asc_name) const;
193+
double computePolarizationEnergy(const std::string & mep_name,
194+
const std::string & asc_name) const;
193195
/*! \brief Getter for the ASC dipole
194196
* \param[in] asc_name label of the ASC surface function
195197
* \param[out] dipole the Cartesian components of the ASC dipole moment
196198
* \return the ASC dipole, i.e. \f$\sqrt{\sum_i \mu_i^2}\f$
197199
*/
198-
double getASCDipole(const char * asc_name, double dipole[]) const;
200+
double getASCDipole(const std::string & asc_name, double dipole[]) const;
199201

200202
/*! \brief Retrieves data wrapped in a given surface function
201203
* \param[in] size the size of the surface function
@@ -204,7 +206,7 @@ class PCMSolver_API Meddle __final {
204206
*/
205207
void getSurfaceFunction(PCMSolverIndex size,
206208
double values[],
207-
const char * name) const;
209+
const std::string & name) const;
208210

209211
/*! \brief Sets a surface function given data and label
210212
* \param[in] size the size of the surface function
@@ -213,12 +215,12 @@ class PCMSolver_API Meddle __final {
213215
*/
214216
void setSurfaceFunction(PCMSolverIndex size,
215217
double values[],
216-
const char * name) const;
218+
const std::string & name) const;
217219

218220
/*! \brief Prints surface function contents to host output
219221
* \param[in] name label of the surface function
220222
*/
221-
void printSurfaceFunction(const char * name) const;
223+
void printSurfaceFunction(const std::string & name) const;
222224

223225
/*! \brief Dumps all currently saved surface functions to NumPy arrays in .npy
224226
* files
@@ -231,15 +233,15 @@ class PCMSolver_API Meddle __final {
231233
* \note The name parameter is the name of the NumPy array file
232234
* **without** .npy extension
233235
*/
234-
void saveSurfaceFunction(const char * name) const;
236+
void saveSurfaceFunction(const std::string & name) const;
235237

236238
/*! \brief Loads a surface function from a .npy file
237239
* \param[in] name label of the surface function
238240
*
239241
* \note The name parameter is the name of the NumPy array file
240242
* **without** .npy extension
241243
*/
242-
void loadSurfaceFunction(const char * name) const;
244+
void loadSurfaceFunction(const std::string & name) const;
243245

244246
/*! \brief Prints citation and set up information
245247
*/

0 commit comments

Comments
 (0)