SPH
ScriptJobs.cpp
Go to the documentation of this file.
1 #include "run/jobs/ScriptJobs.h"
2 #include "quantities/Quantity.h"
3 #include "run/IRun.h"
4 #include "run/ScriptUtils.h"
5 #include "system/Factory.h"
6 #include "thread/Scheduler.h"
7 
9 
10 #ifdef SPH_USE_CHAISCRIPT
11 
12 ChaiScriptJob::ChaiScriptJob(const std::string& name)
13  : IParticleJob(name) {
14  slotNames.resize(8);
15  for (Size i = 0; i < slotNames.size(); ++i) {
16  slotNames[i] = "slot " + std::to_string(i + 1);
17  }
18 
19  paramNames.resize(8);
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;
24  }
25 }
26 
27 UnorderedMap<std::string, ExtJobType> ChaiScriptJob::getSlots() const {
29  for (int i = 0; i < min<int>(inputCnt, slotNames.maxSize()); ++i) {
30  slots.insert(slotNames[i], JobType::PARTICLES);
31  }
32  return slots;
33 }
34 
35 
36 VirtualSettings ChaiScriptJob::getSettings() {
37  VirtualSettings connector;
38  addGenericCategory(connector, instName);
39 
40  VirtualSettings::Category& inputCat = connector.addCategory("Input");
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]);
44  }
45  inputCat.connect("Number of parameters", "paramCnt", paramCnt);
46  for (int i = 0; i < paramCnt; ++i) {
47  inputCat.connect(
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]);
50  }
51 
52  VirtualSettings::Category& scriptCat = connector.addCategory("Script");
53  scriptCat.connect<Path>("Script file", "file", file)
55  .setFileFormats({ { "Chaiscript script", "chai" } });
56 
57  return connector;
58 }
59 
60 void ChaiScriptJob::evaluate(const RunSettings& UNUSED(global), IRunCallbacks& callbacks) {
61  chaiscript::ChaiScript chai;
62  Chai::registerBindings(chai);
63 
64  // node-specific stuff
65  /*for (int i = 0; i < inputCnt; ++i) {
66  SharedPtr<ParticleData> input = this->getInput<ParticleData>(slotNames[i]);
67  chai.add(chaiscript::var(std::ref(input->storage)), slotNames[i]);
68  }*/
69  chai.add(chaiscript::fun<std::function<Chai::Particles(std::string)>>([this](std::string name) {
70  SharedPtr<ParticleData> input = this->getInput<ParticleData>(name);
71  Chai::Particles particles;
72  particles.bindToStorage(input->storage);
73  return particles;
74  }),
75  "getInput");
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];
80  }
81  }
82  throw InvalidSetup("Unknown parameter '" + name + "'");
83  }),
84  "getParam");
85  Statistics stats;
87  chai.add(chaiscript::fun<std::function<void(Float)>>([&callbacks, &stats](const Float progress) {
88  stats.set(StatisticsId::RELATIVE_PROGRESS, progress);
89  callbacks.onTimeStep({}, stats);
90  }),
91  "setProgress");
92  chai.add(chaiscript::fun<std::function<bool()>>([&callbacks]() { return callbacks.shouldAbortRun(); }),
93  "shouldAbort");
94 
95 
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>();
99  result->storage = particles.store().clone(VisitorEnum::ALL_BUFFERS);
100 }
101 
102 static JobRegistrar sRegisterChaiJob(
103  "custom script",
104  "particle operators",
105  [](const std::string& name) { return makeAuto<ChaiScriptJob>(name); },
106  "Custom particle operator, given by a ChaiScript file.");
107 
108 #endif
109 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Basic interface defining a single run.
VirtualSettings::Category & addGenericCategory(VirtualSettings &connector, std::string &instanceName)
Adds a common settings category, used by all jobs.
Definition: Job.cpp:43
@ PARTICLES
Job providing particles.
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
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.
Definition: Job.h:242
Callbacks executed by the simulation to provide feedback to the user.
Definition: IRun.h:27
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.
Definition: Exceptions.h:24
Object representing a path on a filesystem.
Definition: Path.h:17
Object holding various statistics about current run.
Definition: Statistics.h:22
Statistics & set(const StatisticsId idx, TValue &&value)
Sets new values of a statistic.
Definition: Statistics.h:52
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.
Definition: UnorderedMap.h:51
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.
Definition: Job.h:203
Storage storage
Holds all particle positions and other quantities.
Definition: Job.h:35