Skip to content

Commit 739121f

Browse files
author
Roberto Di Remigio
committed
save_surface_functions and load_surface_function are now defined in Interface.cpp
1 parent 4ece64f commit 739121f

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/interface/Interface.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ extern "C" void compute_polarization_energy(double * energy)
175175
}
176176
}
177177

178+
extern "C" void save_surface_functions()
179+
{
180+
typedef SharedSurfaceFunctionMap::const_iterator surfMap_iter;
181+
for ( surfMap_iter it = functions.begin(); it != functions.end(); ++it ) {
182+
std::string fname = it->second->name();
183+
unsigned int dim = static_cast<unsigned int>(it->second->nPoints());
184+
const unsigned int shape[] = {dim};
185+
cnpy::npy_save(fname, it->second->vector().data(), shape, 1, "w", true);
186+
}
187+
}
188+
189+
extern "C" void load_surface_function(const char * name)
190+
{
191+
std::string functionName(name);
192+
cnpy::NpyArray raw_surfFunc = cnpy::npy_load(functionName);
193+
int dim = raw_surfFunc.shape[0];
194+
if (dim != _cavity->size()) {
195+
throw std::runtime_error("Inconsistent dimension of loaded surface function!");
196+
} else {
197+
Eigen::VectorXd values = getFromRawBuffer<double>(dim, 1, raw_surfFunc.data);
198+
SharedSurfaceFunction func( new SurfaceFunction(functionName, dim, values) );
199+
// Append to global map
200+
SharedSurfaceFunctionMap::iterator iter = functions.lower_bound(functionName);
201+
if ( iter == functions.end() || iter->first != functionName ) {
202+
if ( iter != functions.begin() ) --iter;
203+
SharedSurfaceFunctionPair insertion = SharedSurfaceFunctionMap::value_type(functionName, func);
204+
iter = functions.insert(iter, insertion);
205+
}
206+
}
207+
}
208+
178209
extern "C" void dot_surface_functions(double * result, const char * potString,
179210
const char * chgString)
180211
{

src/interface/Interface.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ extern "C" void compute_polarization_energy(double * energy);
9292
extern "C" void save_surface_functions();
9393

9494
#define load_surface_functions \
95-
FortranCInterface_GLOBAL_(load_surface_functions, load_SURFACE_FUNCTIONS)
96-
/*! \fn extern "C" void load_surface_functions(const char * name)
95+
FortranCInterface_GLOBAL_(load_surface_function, load_SURFACE_FUNCTION)
96+
/*! \fn extern "C" void load_surface_function(const char * name)
9797
* \brief Loads the surface function from a .npy file
9898
* \param[in] name the name of the surface function to be loaded from file
9999
*
100100
* The name has to be consistent with that of the .npy file saved using save_surface_functions()
101101
*/
102-
extern "C" void load_surface_functions(const char * name);
102+
extern "C" void load_surface_function(const char * name);
103103

104104
#define dot_surface_functions \
105105
FortranCInterface_GLOBAL_(dot_surface_functions, DOT_SURFACE_FUNCTIONS)

src/utils/SurfaceFunction.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ class SurfaceFunction
9191
/// Division-assignment operator. Defined only for the uniform scaling case.
9292
SurfaceFunction & operator/=(double scaling);
9393

94-
std::string & name() { return name_; }
95-
int nPoints() { return nPoints_; }
94+
const std::string & name() const { return name_; }
95+
int nPoints() const { return nPoints_; }
9696
void value(int index, double value) { values_(index) = value; }
97-
double value(int index) { return values_(index); }
97+
double value(int index) const { return values_(index); }
9898
Eigen::VectorXd & vector() { return values_; }
9999
void allocate(int np) { values_.resize(np); }
100-
bool allocated() { return allocated_; }
100+
bool allocated() const { return allocated_; }
101101
void clear();
102102

103103
void setValues(double * v);

0 commit comments

Comments
 (0)