10 #ifdef SPH_USE_CHAISCRIPT
12 ChaiScriptJob::ChaiScriptJob(
const std::string& name)
15 for (
Size i = 0; i < slotNames.size(); ++i) {
16 slotNames[i] =
"slot " + std::to_string(i + 1);
20 paramValues.resize(8);
21 for (
Size i = 0; i < paramNames.size(); ++i) {
22 paramNames[i] =
"parameter " + std::to_string(i + 1);
23 paramValues[i] = 0._f;
29 for (
int i = 0; i < min<int>(inputCnt, slotNames.maxSize()); ++i) {
41 inputCat.
connect(
"Number of inputs",
"inputCnt", inputCnt);
42 for (
int i = 0; i < inputCnt; ++i) {
43 inputCat.
connect(
"Slot " + std::to_string(i + 1),
"slot" + std::to_string(i + 1), slotNames[i]);
45 inputCat.
connect(
"Number of parameters",
"paramCnt", paramCnt);
46 for (
int i = 0; i < paramCnt; ++i) {
48 "Parameter " + std::to_string(i + 1),
"param" + std::to_string(i + 1), paramNames[i]);
49 inputCat.
connect(
"Value '" + paramNames[i] +
"'",
"value" + std::to_string(i + 1), paramValues[i]);
53 scriptCat.
connect<
Path>(
"Script file",
"file", file)
55 .setFileFormats({ {
"Chaiscript script",
"chai" } });
61 chaiscript::ChaiScript chai;
62 Chai::registerBindings(chai);
69 chai.add(chaiscript::fun<std::function<Chai::Particles(std::string)>>([
this](std::string name) {
71 Chai::Particles particles;
72 particles.bindToStorage(input->
storage);
76 chai.add(chaiscript::fun<std::function<
Float(std::string)>>([
this](std::string name) {
77 for (
Size i = 0; i < paramNames.size(); ++i) {
78 if (name == paramNames[i]) {
79 return paramValues[i];
87 chai.add(chaiscript::fun<std::function<
void(
Float)>>([&callbacks, &stats](
const Float progress) {
92 chai.add(chaiscript::fun<std::function<
bool()>>([&callbacks]() {
return callbacks.
shouldAbortRun(); }),
96 chaiscript::Boxed_Value scriptValue = chai.eval_file(file.native());
97 const Chai::Particles& particles = chaiscript::boxed_cast<const Chai::Particles&>(scriptValue);
98 result = makeShared<ParticleData>();
104 "particle operators",
105 [](
const std::string& name) {
return makeAuto<ChaiScriptJob>(name); },
106 "Custom particle operator, given by a ChaiScript file.");
uint32_t Size
Integral type used to index arrays (by default).
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Basic interface defining a single run.
VirtualSettings::Category & addGenericCategory(VirtualSettings &connector, std::string &instanceName)
Adds a common settings category, used by all jobs.
@ PARTICLES
Job providing particles.
#define NAMESPACE_SPH_END
Holder of quantity values and their temporal derivatives.
Interface for executing tasks (potentially) asynchronously.
Utility functions and classes exposed to the embedded scripting language.
Base class for all jobs providing particle data.
Callbacks executed by the simulation to provide feedback to the user.
virtual bool shouldAbortRun() const =0
Returns whether current run should be aborted or not.
virtual void onTimeStep(const Storage &storage, Statistics &stats)=0
Called every timestep.
Thrown when components of the run are mutually incompatible.
Object representing a path on a filesystem.
Object holding various statistics about current run.
Statistics & set(const StatisticsId idx, TValue &&value)
Sets new values of a statistic.
INLINE TValue & insert(const TKey &key, const TValue &value)
Adds a new element into the map or sets new value of element with the same key.
EntryControl & connect(const std::string &name, const std::string &key, TValue &value)
Connects to given reference.
Holds a map of virtual entries, associated with a unique name.
Category & addCategory(const std::string &name)
Creates a new category of entries.
Creating code components based on values from settings.
Helper class, allowing to register job into the global list of jobs.
Storage storage
Holds all particle positions and other quantities.