-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from CarrieFilion/diskbulge
- Loading branch information
Showing
13 changed files
with
332 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <DiskDensityFunc.H> | ||
|
||
namespace py = pybind11; | ||
|
||
DiskDensityFunc::DiskDensityFunc(const std::string& modulename, | ||
const std::string& funcname) | ||
: funcname(funcname) | ||
{ | ||
// Check if a Python interpreter exists | ||
if (Py_IsInitialized() == 0) { | ||
py::initialize_interpreter(); | ||
// Mark the interpreter as started by this instance | ||
started = true; | ||
} | ||
|
||
// Bind the disk_density function from Python | ||
disk_density = | ||
py::reinterpret_borrow<py::function> | ||
( py::module::import(modulename.c_str()).attr(funcname.c_str()) ); | ||
} | ||
|
||
DiskDensityFunc::~DiskDensityFunc() | ||
{ | ||
// Only end the interpreter if it was started by this instance | ||
if (started) py::finalize_interpreter(); | ||
} | ||
|
||
double DiskDensityFunc::operator() (double R, double z, double phi) | ||
{ | ||
return disk_density(R, z, phi).cast<double>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef _DiskDensityFunc_H_ | ||
#define _DiskDensityFunc_H_ | ||
|
||
#include <functional> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/embed.h> | ||
|
||
/** Python disk-density function wrapper | ||
The wrapper class will take a user supplied Python module that | ||
must supply the 'disk-density' function | ||
An example disk-density Python module: | ||
--------------------------cut here-------------------------------- | ||
import math | ||
def disk_density(R, z, phi): | ||
"""Computes the disk density at a given radius, height, and | ||
azimuth. This would be the user's target density for basis | ||
creation. | ||
""" | ||
a = 1.0 # Scale radius | ||
h = 0.2 # Scale height | ||
f = math.exp(-math.fabs(z)/h) # Prevent overflows | ||
sech = 2.0*f / (1.0 + f*f) # | ||
return math.exp(-R/a)*sech*sech/(4*math.pi*h*a*a) | ||
--------------------------cut here-------------------------------- | ||
*/ | ||
class __attribute__((visibility("default"))) | ||
DiskDensityFunc | ||
{ | ||
private: | ||
|
||
//! The disk-density function | ||
pybind11::function disk_density; | ||
|
||
//! Interpreter started? | ||
bool started = false; | ||
|
||
//! Name of density function | ||
std::string funcname; | ||
|
||
public: | ||
|
||
//! Constructor | ||
DiskDensityFunc(const std::string& modulename, | ||
const std::string& funcname="disk_density"); | ||
|
||
//! Destructor | ||
~DiskDensityFunc(); | ||
|
||
//! Evaluate the density | ||
double operator() (double R, double z, double phi); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.