Graph Framework
Loading...
Searching...
No Matches
absorption.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
6//------------------------------------------------------------------------------
7//------------------------------------------------------------------------------
90//------------------------------------------------------------------------------
91#ifndef absorption_h
92#define absorption_h
93
94#include <thread>
95
96#include "newton.hpp"
97#include "output.hpp"
98
100namespace absorption {
101//******************************************************************************
102// Base class
103//******************************************************************************
104//------------------------------------------------------------------------------
109//------------------------------------------------------------------------------
110 template<jit::complex_scalar T, bool SAFE_MATH=true>
111 class method {
112 public:
114 typedef T base;
116 static constexpr bool safe_math = SAFE_MATH;
117
118//------------------------------------------------------------------------------
120//------------------------------------------------------------------------------
121 virtual void compile()=0;
122
123//------------------------------------------------------------------------------
127//------------------------------------------------------------------------------
128 virtual void run(const size_t time_index)=0;
129 };
130
132 template<class A>
133 concept model = std::is_base_of<method<typename A::base, A::safe_math>, A>::value;
134
135//******************************************************************************
136// Root finder.
137//******************************************************************************
138//------------------------------------------------------------------------------
143//------------------------------------------------------------------------------
144 template<jit::complex_scalar T, bool SAFE_MATH=true>
145 class root_finder : public method<T, SAFE_MATH> {
146 private:
149
166
169
173 const size_t index;
174
178 output::data_set<T> dataset;
179
181 std::thread sync;
182
183 public:
184//------------------------------------------------------------------------------
199//------------------------------------------------------------------------------
210 const std::string &filename="",
211 const size_t index=0) :
212 kamp(kamp), w(w), kx(kx), ky(ky), kz(kz), x(x), y(y), z(z), t(t),
213 work(index), index(index), file(filename), dataset(file), sync([]{}) {
214 auto kvec = kx*eq->get_esup1(x, y, z)
215 + ky*eq->get_esup2(x, y, z)
216 + kz*eq->get_esup3(x, y, z);
217 auto klen = kvec->length();
218
219 auto kx_amp = kamp*kx/klen;
220 auto ky_amp = kamp*ky/klen;
221 auto kz_amp = kamp*kz/klen;
222
224 graph::variable_cast(this->kamp),
225 graph::variable_cast(this->kx),
226 graph::variable_cast(this->ky),
227 graph::variable_cast(this->kz),
228 graph::variable_cast(this->x),
229 graph::variable_cast(this->y),
230 graph::variable_cast(this->z)
231 };
232
235 };
236
237 work.add_item(inputs, {}, setters,
239 "root_find_init_kernel", inputs.back()->size());
240
241 inputs.push_back(graph::variable_cast(this->t));
242 inputs.push_back(graph::variable_cast(this->w));
243
244 auto D = dispersion::hot_plasma<T,
246 SAFE_MATH>().D(w,
247 kx + kx_amp,
248 ky + ky_amp,
249 kz + kz_amp,
250 x, y, z, t, eq);
251
252 solver::newton(work, {kamp}, inputs, {D},
254
255 inputs = {
256 graph::variable_cast(this->kamp),
257 graph::variable_cast(this->kx),
258 graph::variable_cast(this->ky),
259 graph::variable_cast(this->kz),
260 graph::variable_cast(this->x),
261 graph::variable_cast(this->y),
262 graph::variable_cast(this->z)
263 };
264 setters = {
265 {klen + kamp, graph::variable_cast(this->kamp)}
266 };
267 work.add_item(inputs, {}, setters,
269 "final_kamp", inputs.back()->size());
270 }
271
272//------------------------------------------------------------------------------
274//------------------------------------------------------------------------------
276 sync.join();
277 }
278
279//------------------------------------------------------------------------------
281//------------------------------------------------------------------------------
282 void compile() {
283 work.compile();
284
285 dataset.create_variable(file, "kamp", this->kamp, work.get_context());
286
287 dataset.reference_variable(file, "w", graph::variable_cast(this->w));
288 dataset.reference_variable(file, "kx", graph::variable_cast(this->kx));
289 dataset.reference_variable(file, "ky", graph::variable_cast(this->ky));
290 dataset.reference_variable(file, "kz", graph::variable_cast(this->kz));
291 dataset.reference_variable(file, "x", graph::variable_cast(this->x));
292 dataset.reference_variable(file, "y", graph::variable_cast(this->y));
293 dataset.reference_variable(file, "z", graph::variable_cast(this->z));
294 dataset.reference_variable(file, "time", graph::variable_cast(this->t));
295 file.end_define_mode();
296 }
297
298//------------------------------------------------------------------------------
302//------------------------------------------------------------------------------
303 void run(const size_t time_index) {
304 dataset.read(file, time_index);
305 work.copy_to_device(w, graph::variable_cast(this->w)->data());
306 work.copy_to_device(kx, graph::variable_cast(this->kx)->data());
307 work.copy_to_device(ky, graph::variable_cast(this->ky)->data());
308 work.copy_to_device(kz, graph::variable_cast(this->kz)->data());
309 work.copy_to_device(x, graph::variable_cast(this->x)->data());
310 work.copy_to_device(y, graph::variable_cast(this->y)->data());
311 work.copy_to_device(z, graph::variable_cast(this->z)->data());
312 work.copy_to_device(t, graph::variable_cast(this->t)->data());
313
314 work.run();
315
316 sync.join();
317 work.wait();
318 sync = std::thread([this] (const size_t i) -> void {
319 dataset.write(file, i);
320 }, time_index);
321 }
322 };
323
324//******************************************************************************
325// Weak Damping.
326//******************************************************************************
327//------------------------------------------------------------------------------
332//------------------------------------------------------------------------------
333 template<jit::complex_scalar T, bool SAFE_MATH=true>
334 class weak_damping : public method<T, SAFE_MATH> {
335 private:
338
355
358
360 const size_t index;
361
365 output::data_set<T> dataset;
366
368 std::thread sync;
369
370 public:
371//------------------------------------------------------------------------------
386//------------------------------------------------------------------------------
397 const std::string &filename="",
398 const size_t index=0) :
399 kamp(kamp), w(w), kx(kx), ky(ky), kz(kz), x(x), y(y), z(z), t(t),
400 work(index), index(index), file(filename), dataset(file), sync([]{}) {
401 auto k_vec = kx*eq->get_esup1(x, y, z)
402 + ky*eq->get_esup2(x, y, z)
403 + kz*eq->get_esup3(x, y, z);
404 auto k_unit = k_vec->unit();
405
406 auto Dc = dispersion::cold_plasma_expansion<T, SAFE_MATH> ().D(w, kx, ky, kz,
407 x, y, z, t, eq);
410 SAFE_MATH> ().D(w, kx, ky, kz,
411 x, y, z, t, eq);
412
413 auto kamp1 = k_vec->length()
414 - Dw/k_unit->dot(Dc->df(kx)*eq->get_esup1(x, y, z) +
415 Dc->df(ky)*eq->get_esup2(x, y, z) +
416 Dc->df(kz)*eq->get_esup3(x, y, z));
417
419 graph::variable_cast(this->kamp),
420 graph::variable_cast(this->kx),
421 graph::variable_cast(this->ky),
422 graph::variable_cast(this->kz),
423 graph::variable_cast(this->x),
424 graph::variable_cast(this->y),
425 graph::variable_cast(this->z),
426 graph::variable_cast(this->t),
427 graph::variable_cast(this->w)
428 };
429
431 {kamp1, graph::variable_cast(this->kamp)}
432 };
433
434 work.add_item(inputs, {}, setters,
436 "weak_damping_kimg_kernel", inputs.back()->size());
437 }
438
439//------------------------------------------------------------------------------
441//------------------------------------------------------------------------------
443 sync.join();
444 }
445
446//------------------------------------------------------------------------------
448//------------------------------------------------------------------------------
449 void compile() {
450 work.compile();
451
452 dataset.create_variable(file, "kamp", this->kamp, work.get_context());
453
454 dataset.reference_variable(file, "w", graph::variable_cast(this->w));
455 dataset.reference_variable(file, "kx", graph::variable_cast(this->kx));
456 dataset.reference_variable(file, "ky", graph::variable_cast(this->ky));
457 dataset.reference_variable(file, "kz", graph::variable_cast(this->kz));
458 dataset.reference_variable(file, "x", graph::variable_cast(this->x));
459 dataset.reference_variable(file, "y", graph::variable_cast(this->y));
460 dataset.reference_variable(file, "z", graph::variable_cast(this->z));
461 dataset.reference_variable(file, "time", graph::variable_cast(this->t));
462 file.end_define_mode();
463 }
464
465//------------------------------------------------------------------------------
469//------------------------------------------------------------------------------
470 void run(const size_t time_index) {
471 dataset.read(file, time_index);
472 work.copy_to_device(w, graph::variable_cast(this->w)->data());
473 work.copy_to_device(kx, graph::variable_cast(this->kx)->data());
474 work.copy_to_device(ky, graph::variable_cast(this->ky)->data());
475 work.copy_to_device(kz, graph::variable_cast(this->kz)->data());
476 work.copy_to_device(x, graph::variable_cast(this->x)->data());
477 work.copy_to_device(y, graph::variable_cast(this->y)->data());
478 work.copy_to_device(z, graph::variable_cast(this->z)->data());
479 work.copy_to_device(t, graph::variable_cast(this->t)->data());
480
481 work.run();
482
483 sync.join();
484 work.wait();
485 sync = std::thread([this] (const size_t i) -> void {
486 dataset.write(file, i);
487 }, time_index);
488 }
489 };
490}
491
492#endif /* absorption_h */
Base class for absoption models.
Definition absorption.hpp:111
static constexpr bool safe_math
Retrieve template parameter of safe math.
Definition absorption.hpp:116
T base
Type def to retrieve the backend base type.
Definition absorption.hpp:114
virtual void run(const size_t time_index)=0
Run the workflow.
virtual void compile()=0
Compile the workitems.
Class interface for the root finder.
Definition absorption.hpp:145
root_finder(graph::shared_leaf< T, SAFE_MATH > kamp, graph::shared_leaf< T, SAFE_MATH > w, graph::shared_leaf< T, SAFE_MATH > kx, graph::shared_leaf< T, SAFE_MATH > ky, graph::shared_leaf< T, SAFE_MATH > kz, graph::shared_leaf< T, SAFE_MATH > x, graph::shared_leaf< T, SAFE_MATH > y, graph::shared_leaf< T, SAFE_MATH > z, graph::shared_leaf< T, SAFE_MATH > t, equilibrium::shared< T, SAFE_MATH > &eq, const std::string &filename="", const size_t index=0)
Constructor for root finding.
Definition absorption.hpp:200
void run(const size_t time_index)
Run the workflow.
Definition absorption.hpp:303
void compile()
Compile the workitems.
Definition absorption.hpp:282
~root_finder()
Destructor.
Definition absorption.hpp:275
Class interface weak damping approximation.
Definition absorption.hpp:334
void run(const size_t time_index)
Run the workflow.
Definition absorption.hpp:470
~weak_damping()
Destructor.
Definition absorption.hpp:442
void compile()
Compile the workitems.
Definition absorption.hpp:449
weak_damping(graph::shared_leaf< T, SAFE_MATH > kamp, graph::shared_leaf< T, SAFE_MATH > w, graph::shared_leaf< T, SAFE_MATH > kx, graph::shared_leaf< T, SAFE_MATH > ky, graph::shared_leaf< T, SAFE_MATH > kz, graph::shared_leaf< T, SAFE_MATH > x, graph::shared_leaf< T, SAFE_MATH > y, graph::shared_leaf< T, SAFE_MATH > z, graph::shared_leaf< T, SAFE_MATH > t, equilibrium::shared< T, SAFE_MATH > &eq, const std::string &filename="", const size_t index=0)
Constructor for weak damping.
Definition absorption.hpp:387
Cold Plasma expansion disperison function.
Definition dispersion.hpp:1007
virtual graph::shared_leaf< T, SAFE_MATH > D(graph::shared_leaf< T, SAFE_MATH > w, graph::shared_leaf< T, SAFE_MATH > kx, graph::shared_leaf< T, SAFE_MATH > ky, graph::shared_leaf< T, SAFE_MATH > kz, graph::shared_leaf< T, SAFE_MATH > x, graph::shared_leaf< T, SAFE_MATH > y, graph::shared_leaf< T, SAFE_MATH > z, graph::shared_leaf< T, SAFE_MATH > t, equilibrium::shared< T, SAFE_MATH > &eq)
Cold Plasma expansion Disperison function.
Definition dispersion.hpp:1034
Hot Plasma Expansion Disperison function.
Definition dispersion.hpp:1210
Hot Plasma Disperison function.
Definition dispersion.hpp:1095
Class interface to build dispersion relation functions.
Definition dispersion.hpp:218
Class representing a netcdf dataset.
Definition output.hpp:166
void read(const result_file &result, const size_t index)
Read step.
Definition output.hpp:412
void reference_variable(const result_file &result, const std::string &name, graph::shared_variable< T, SAFE_MATH > &&node)
Load reference.
Definition output.hpp:285
void create_variable(const result_file &result, const std::string &name, graph::shared_leaf< T, SAFE_MATH > &node, jit::context< T, SAFE_MATH > &context)
Create a variable.
Definition output.hpp:260
void write(const result_file &result)
Write step.
Definition output.hpp:353
Class representing a netcdf based output file.
Definition output.hpp:32
void end_define_mode() const
End define mode.
Definition output.hpp:94
Class representing a workflow manager.
Definition workflow.hpp:171
void run()
Run work items.
Definition workflow.hpp:285
void wait()
Wait for GPU queue to finish.
Definition workflow.hpp:294
void add_item(graph::input_nodes< T, SAFE_MATH > in, graph::output_nodes< T, SAFE_MATH > out, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size)
Add a workflow item.
Definition workflow.hpp:221
void copy_to_device(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy buffer contents to the device.
Definition workflow.hpp:304
jit::context< T, SAFE_MATH > & get_context()
Get the jit context.
Definition workflow.hpp:348
void compile()
Compile the workflow items.
Definition workflow.hpp:262
Solver method concept.
Definition absorption.hpp:133
Namespace for power absorption models.
Definition absorption.hpp:100
std::shared_ptr< generic< T, SAFE_MATH > > shared
Convenience type alias for shared equilibria.
Definition equilibrium.hpp:472
constexpr shared_leaf< T, SAFE_MATH > zero()
Forward declare for zero.
Definition node.hpp:994
std::vector< shared_variable< T, SAFE_MATH > > input_nodes
Convenience type alias for a vector of inputs.
Definition node.hpp:1730
shared_variable< T, SAFE_MATH > variable_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a variable node.
Definition node.hpp:1746
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:673
std::vector< std::pair< shared_leaf< T, SAFE_MATH >, shared_variable< T, SAFE_MATH > > > map_nodes
Convenience type alias for maping end codes back to inputs.
Definition node.hpp:1734
void newton(workflow::manager< T, SAFE_MATH > &work, graph::output_nodes< T, SAFE_MATH > vars, graph::input_nodes< T, SAFE_MATH > inputs, graph::shared_leaf< T, SAFE_MATH > func, graph::shared_random_state< T, SAFE_MATH > state, const T tolarance=1.0E-30, const size_t max_iterations=1000, const T step=1.0)
Determine the value of vars to minimze the loss function.
Definition newton.hpp:34
Sets up the kernel for a newtons method.
Impliments output files in a netcdf format.