Skip to content

Commit b596e86

Browse files
author
Roberto Di Remigio
committed
Use std::tuple to return multiple values from function
1 parent d9ebf82 commit b596e86

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

src/bin/plot_green_spherical.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
int main()
1212
{
1313
int nPoints = 10000;
14-
double epsInside = 2.0;
14+
double epsInside = 80.0;
1515
double epsOutside = 2.0;
1616
Eigen::Vector3d sphereCenter;
1717
sphereCenter << 0.0, 0.0, 0.0;
1818
double sphereRadius = 100.0;
1919
double width = 10.0;
2020

2121
Eigen::Vector3d source;
22-
source << 4.0, 0.0, 100.0;
22+
source << 0.0, 0.0, 1.0;
2323
Eigen::Vector3d probe;
2424
probe << 0.0, 0.0, 0.0;
2525

src/green/InterfacesDef.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <array>
3030
#include <cmath>
3131
#include <functional>
32+
#include <tuple>
3233
#include <vector>
3334

3435
#include "Config.hpp"
@@ -50,7 +51,7 @@ typedef std::array<StateType, 3> RadialFunction;
5051
/*! \typedef ProfileEvaluator
5152
* \brief sort of a function pointer to the dielectric profile evaluation function
5253
*/
53-
typedef std::function<void(double &, double &, const double)> ProfileEvaluator;
54+
typedef std::function< std::tuple<double, double>(const double) > ProfileEvaluator;
5455

5556
/*! \struct IntegratorParameters
5657
* \brief holds parameters for the integrator
@@ -97,7 +98,7 @@ class LnTransformedRadial
9798
{
9899
// Evaluate the dielectric profile
99100
double eps = 0.0, epsPrime = 0.0;
100-
eval_(eps, epsPrime, r);
101+
std::tie(eps, epsPrime) = eval_(r);
101102
if (numericalZero(eps)) throw std::domain_error("Division by zero!");
102103
double gamma_epsilon = epsPrime / eps;
103104
// System of equations is defined here

src/green/SphericalDiffuse.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <functional>
3232
#include <iosfwd>
3333
#include <string>
34+
#include <tuple>
3435
#include <vector>
3536

3637
#include "Config.hpp"
@@ -120,8 +121,8 @@ class SphericalDiffuse : public GreensFunction<Numerical, ProfilePolicy>
120121
const Eigen::Vector3d & p1, const Eigen::Vector3d & p2) const
121122
{
122123
/*
123-
double eps_r2 = 0.0, epsPrime_r2 = 0.0;
124-
this->profile_(eps_r2, epsPrime_r2, p2.norm());
124+
double eps_r2 = 0.0;
125+
std::tie(eps_r2, std::ignore) = this->profile_(p2.norm());
125126
126127
return (eps_r2 * this->derivativeProbe(direction, p1, p2));
127128
*/
@@ -131,8 +132,8 @@ class SphericalDiffuse : public GreensFunction<Numerical, ProfilePolicy>
131132
double imageDeriv = threePointStencil(std::bind(&SphericalDiffuse::imagePotential, this, _1, _2),
132133
p2, p1, direction, this->delta_);
133134

134-
double eps_r2 = 0.0, epsPrime_r2 = 0.0;
135-
this->profile_(eps_r2, epsPrime_r2, p2.norm());
135+
double eps_r2 = 0.0;
136+
std::tie(eps_r2, std::ignore) = this->profile_(p2.norm());
136137

137138
return (eps_r2 * CoulombDeriv + imageDeriv);
138139
}
@@ -208,7 +209,7 @@ class SphericalDiffuse : public GreensFunction<Numerical, ProfilePolicy>
208209
double imagePotentialDerivative(const Eigen::Vector3d & direction, const Eigen::Vector3d & p1, const Eigen::Vector3d & p2) const;
209210
/*! Handle to the dielectric profile evaluation
210211
*/
211-
void epsilon(double & v, double & d, double point) const { this->profile_(v, d, point); }
212+
void epsilon(double & v, double & d, double point) const { std::tie(v, d) = this->profile_(point); }
212213
private:
213214
/*! Evaluates the Green's function given a pair of points
214215
*

src/green/TanhSphericalDiffuse.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <functional>
3131
#include <iosfwd>
3232
#include <string>
33+
#include <tuple>
3334

3435
#include "Config.hpp"
3536

@@ -113,7 +114,7 @@ inline void TanhSphericalDiffuse::initSphericalDiffuse()
113114
double r_infinity_ = profile_.center() + 200.0; /*! Upper bound of the integration interval */
114115
double observer_step_ = 1.0e-03; /*! Time step between observer calls */
115116
IntegratorParameters params_(r_0_, r_infinity_, observer_step_);
116-
ProfileEvaluator eval_ = std::bind(&TanhDiffuse::operator(), this->profile_, _1, _2, _3);
117+
ProfileEvaluator eval_ = std::bind(&TanhDiffuse::operator(), this->profile_, _1);
117118

118119
LOG("Computing coefficient for the separation of the Coulomb singularity");
119120
LOG("Computing first radial solution L = " + std::to_string(maxLC_));
@@ -175,8 +176,8 @@ inline double TanhSphericalDiffuse::coefficient(double r1, double r2) const
175176
double d_omega2 = linearInterpolation(r2, omegaC_[0], omegaC_[2]);
176177

177178
double tmp = 0.0, coeff = 0.0;
178-
double eps_r2 = 0.0, epsPrime_r2 = 0.0;
179-
this->profile_(eps_r2, epsPrime_r2, r2);
179+
double eps_r2 = 0.0;
180+
std::tie(eps_r2, std::ignore) = this->profile_(r2);
180181

181182
double denominator = (d_zeta2 - d_omega2) * std::pow(r2, 2) * eps_r2;
182183

@@ -217,8 +218,8 @@ inline double TanhSphericalDiffuse::functionSummation(int L, double r1, double r
217218
/* Value of derivative of omega_[L] at point with index 2 */
218219
double d_omega2 = linearInterpolation(r2, omega_[L][0], omega_[L][2]);
219220

220-
double eps_r2 = 0.0, epsPrime_r2 = 0.0;
221-
this->profile_(eps_r2, epsPrime_r2, r2);
221+
double eps_r2 = 0.0;
222+
std::tie(eps_r2, std::ignore) = this->profile_(r2);
222223

223224
double denominator = (d_zeta2 - d_omega2) * std::pow(r2, 2) * eps_r2;
224225

src/green/dielectric_profile/TanhDiffuse.hpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define TANHDIFFUSE_HPP
2828

2929
#include <iosfwd>
30+
#include <tuple>
3031

3132
#include "Config.hpp"
3233

@@ -40,16 +41,26 @@
4041
class TanhDiffuse
4142
{
4243
private:
44+
/// Dielectric constant on the left of the interface
4345
double epsilon1_;
46+
/// Dielectric constant one the right of the interface
4447
double epsilon2_;
48+
/// Width of the transition layer
4549
double width_;
50+
/// Center of the transition layer
4651
double center_;
52+
/*! Returns value of dielectric profile at given point
53+
* \param[in] point where to evaluate the profile
54+
*/
4755
double value(double point) const {
4856
double epsPlus = (epsilon1_ + epsilon2_) / 2.0;
4957
double epsMinus = (epsilon2_ - epsilon1_) / 2.0;
5058
double tanh_r = std::tanh((point - center_) / width_);
5159
return (epsPlus + epsMinus * tanh_r); //epsilon(r)
5260
}
61+
/*! Returns value of derivative of dielectric profile at given point
62+
* \param[in] point where to evaluate the derivative
63+
*/
5364
double derivative(double point) const {
5465
double factor = (epsilon1_ - epsilon2_) / (2.0 * width_);
5566
double tanh_r = std::tanh((point - center_) / width_);
@@ -59,24 +70,20 @@ class TanhDiffuse
5970
{
6071
os << "Permittivity inside = " << epsilon1_ << std::endl;
6172
os << "Permittivity outside = " << epsilon2_ << std::endl;
62-
os << "Profile width = " << width_ << std::endl;
63-
os << "Profile center = " << center_;
73+
os << "Profile width = " << width_ << " AU" << std::endl;
74+
os << "Profile center = " << center_ << " AU";
6475
return os;
6576
}
6677
public:
6778
TanhDiffuse() {}
6879
TanhDiffuse(double e1, double e2, double w, double c) :
6980
epsilon1_(e1), epsilon2_(e2), width_(w), center_(c) {}
70-
/*! The permittivity profile of the transition layer
71-
* \param[out] e the value of the dielectric constant at point r
72-
* \param[out] de the value of the derivative of the dielectric constant
73-
* at point r
81+
/*! Returns a tuple holding the permittivity and its derivative
7482
* \param[in] r evaluation point
7583
*/
76-
void operator()(double & e, double & de, const double r) const
84+
std::tuple<double, double> operator()(const double r) const
7785
{
78-
e = value(r);
79-
de = derivative(r);
86+
return std::make_tuple(value(r), derivative(r));
8087
}
8188
double epsilon1() const { return epsilon1_; }
8289
double epsilon2() const { return epsilon2_; }

0 commit comments

Comments
 (0)