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#include "dispersion.hpp"
99
101namespace absorption {
102//******************************************************************************
103// Base class
104//******************************************************************************
105//------------------------------------------------------------------------------
110//------------------------------------------------------------------------------
111 template<jit::complex_scalar T, bool SAFE_MATH=true>
112 class method {
113 public:
115 typedef T base;
117 static constexpr bool safe_math = SAFE_MATH;
118
119//------------------------------------------------------------------------------
121//------------------------------------------------------------------------------
122 virtual void compile()=0;
123
124//------------------------------------------------------------------------------
128//------------------------------------------------------------------------------
129 virtual void run(const size_t time_index)=0;
130 };
131
133 template<class A>
134 concept model = std::is_base_of<method<typename A::base, A::safe_math>, A>::value;
135
136//******************************************************************************
137// Root finder.
138//******************************************************************************
139//------------------------------------------------------------------------------
144//------------------------------------------------------------------------------
145 template<jit::complex_scalar T, bool SAFE_MATH=true>
146 class root_finder : public method<T, SAFE_MATH> {
147 private:
150
167
170
174 const size_t index;
175
179 output::data_set<T> dataset;
180
182 std::thread sync;
183
184 public:
185//------------------------------------------------------------------------------
200//------------------------------------------------------------------------------
211 const std::string &filename="",
212 const size_t index=0) :
213 kamp(kamp), w(w), kx(kx), ky(ky), kz(kz), x(x), y(y), z(z), t(t),
214 work(index), index(index), file(filename), dataset(file), sync([]{}) {
215 auto kvec = kx*eq->get_esup1(x, y, z)
216 + ky*eq->get_esup2(x, y, z)
217 + kz*eq->get_esup3(x, y, z);
218 auto klen = kvec->length();
219
220 auto kx_amp = kamp*kx/klen;
221 auto ky_amp = kamp*ky/klen;
222 auto kz_amp = kamp*kz/klen;
223
225 graph::variable_cast(this->kamp),
226 graph::variable_cast(this->kx),
227 graph::variable_cast(this->ky),
228 graph::variable_cast(this->kz),
229 graph::variable_cast(this->x),
230 graph::variable_cast(this->y),
231 graph::variable_cast(this->z)
232 };
233
236 };
237
238 work.add_item(inputs, {}, setters,
240 "root_find_init_kernel", inputs.back()->size());
241
242 inputs.push_back(graph::variable_cast(this->t));
243 inputs.push_back(graph::variable_cast(this->w));
244
245 auto D = dispersion::hot_plasma<T,
247 SAFE_MATH>().D(w,
248 kx + kx_amp,
249 ky + ky_amp,
250 kz + kz_amp,
251 x, y, z, t, eq);
252
253 solver::newton(work, {kamp}, inputs, {D},
255
256 inputs = {
257 graph::variable_cast(this->kamp),
258 graph::variable_cast(this->kx),
259 graph::variable_cast(this->ky),
260 graph::variable_cast(this->kz),
261 graph::variable_cast(this->x),
262 graph::variable_cast(this->y),
263 graph::variable_cast(this->z)
264 };
265 setters = {
266 {klen + kamp, graph::variable_cast(this->kamp)}
267 };
268 work.add_item(inputs, {}, setters,
270 "final_kamp", inputs.back()->size());
271 }
272
273//------------------------------------------------------------------------------
275//------------------------------------------------------------------------------
277 sync.join();
278 }
279
280//------------------------------------------------------------------------------
282//------------------------------------------------------------------------------
283 void compile() {
284 work.compile();
285
286 dataset.create_variable(file, "kamp", this->kamp, work.get_context());
287
288 dataset.reference_variable(file, "w", graph::variable_cast(this->w));
289 dataset.reference_variable(file, "kx", graph::variable_cast(this->kx));
290 dataset.reference_variable(file, "ky", graph::variable_cast(this->ky));
291 dataset.reference_variable(file, "kz", graph::variable_cast(this->kz));
292 dataset.reference_variable(file, "x", graph::variable_cast(this->x));
293 dataset.reference_variable(file, "y", graph::variable_cast(this->y));
294 dataset.reference_variable(file, "z", graph::variable_cast(this->z));
295 dataset.reference_variable(file, "time", graph::variable_cast(this->t));
296 file.end_define_mode();
297 }
298
299//------------------------------------------------------------------------------
303//------------------------------------------------------------------------------
304 void run(const size_t time_index) {
305 dataset.read(file, time_index);
306 work.copy_to_device(w, graph::variable_cast(this->w)->data());
307 work.copy_to_device(kx, graph::variable_cast(this->kx)->data());
308 work.copy_to_device(ky, graph::variable_cast(this->ky)->data());
309 work.copy_to_device(kz, graph::variable_cast(this->kz)->data());
310 work.copy_to_device(x, graph::variable_cast(this->x)->data());
311 work.copy_to_device(y, graph::variable_cast(this->y)->data());
312 work.copy_to_device(z, graph::variable_cast(this->z)->data());
313 work.copy_to_device(t, graph::variable_cast(this->t)->data());
314
315 work.run();
316
317 sync.join();
318 work.wait();
319 sync = std::thread([this] (const size_t i) -> void {
320 dataset.write(file, i);
321 }, time_index);
322 }
323 };
324
325//******************************************************************************
326// Weak Damping.
327//******************************************************************************
328//------------------------------------------------------------------------------
333//------------------------------------------------------------------------------
334 template<jit::complex_scalar T, bool SAFE_MATH=true>
335 class weak_damping : public method<T, SAFE_MATH> {
336 private:
339
356
359
361 const size_t index;
362
366 output::data_set<T> dataset;
367
369 std::thread sync;
370
371 public:
372//------------------------------------------------------------------------------
387//------------------------------------------------------------------------------
398 const std::string &filename="",
399 const size_t index=0) :
400 kamp(kamp), w(w), kx(kx), ky(ky), kz(kz), x(x), y(y), z(z), t(t),
401 work(index), index(index), file(filename), dataset(file), sync([]{}) {
402 auto k_vec = kx*eq->get_esup1(x, y, z)
403 + ky*eq->get_esup2(x, y, z)
404 + kz*eq->get_esup3(x, y, z);
405 auto k_unit = k_vec->unit();
406
407 auto Dc = dispersion::cold_plasma_expansion<T, SAFE_MATH> ().D(w, kx, ky, kz,
408 x, y, z, t, eq);
411 SAFE_MATH> ().D(w, kx, ky, kz,
412 x, y, z, t, eq);
413
414 auto kamp1 = k_vec->length()
415 - Dw/k_unit->dot(Dc->df(kx)*eq->get_esup1(x, y, z) +
416 Dc->df(ky)*eq->get_esup2(x, y, z) +
417 Dc->df(kz)*eq->get_esup3(x, y, z));
418
420 graph::variable_cast(this->kamp),
421 graph::variable_cast(this->kx),
422 graph::variable_cast(this->ky),
423 graph::variable_cast(this->kz),
424 graph::variable_cast(this->x),
425 graph::variable_cast(this->y),
426 graph::variable_cast(this->z),
427 graph::variable_cast(this->t),
428 graph::variable_cast(this->w)
429 };
430
432 {kamp1, graph::variable_cast(this->kamp)}
433 };
434
435 work.add_item(inputs, {}, setters,
437 "weak_damping_kimg_kernel", inputs.back()->size());
438 }
439
440//------------------------------------------------------------------------------
442//------------------------------------------------------------------------------
444 sync.join();
445 }
446
447//------------------------------------------------------------------------------
449//------------------------------------------------------------------------------
450 void compile() {
451 work.compile();
452
453 dataset.create_variable(file, "kamp", this->kamp, work.get_context());
454
455 dataset.reference_variable(file, "w", graph::variable_cast(this->w));
456 dataset.reference_variable(file, "kx", graph::variable_cast(this->kx));
457 dataset.reference_variable(file, "ky", graph::variable_cast(this->ky));
458 dataset.reference_variable(file, "kz", graph::variable_cast(this->kz));
459 dataset.reference_variable(file, "x", graph::variable_cast(this->x));
460 dataset.reference_variable(file, "y", graph::variable_cast(this->y));
461 dataset.reference_variable(file, "z", graph::variable_cast(this->z));
462 dataset.reference_variable(file, "time", graph::variable_cast(this->t));
463 file.end_define_mode();
464 }
465
466//------------------------------------------------------------------------------
470//------------------------------------------------------------------------------
471 void run(const size_t time_index) {
472 dataset.read(file, time_index);
473 work.copy_to_device(w, graph::variable_cast(this->w)->data());
474 work.copy_to_device(kx, graph::variable_cast(this->kx)->data());
475 work.copy_to_device(ky, graph::variable_cast(this->ky)->data());
476 work.copy_to_device(kz, graph::variable_cast(this->kz)->data());
477 work.copy_to_device(x, graph::variable_cast(this->x)->data());
478 work.copy_to_device(y, graph::variable_cast(this->y)->data());
479 work.copy_to_device(z, graph::variable_cast(this->z)->data());
480 work.copy_to_device(t, graph::variable_cast(this->t)->data());
481
482 work.run();
483
484 sync.join();
485 work.wait();
486 sync = std::thread([this] (const size_t i) -> void {
487 dataset.write(file, i);
488 }, time_index);
489 }
490 };
491}
492
493#endif /* absorption_h */
Base class for absorption models.
Definition absorption.hpp:112
static constexpr bool safe_math
Retrieve template parameter of safe math.
Definition absorption.hpp:117
T base
Type def to retrieve the backend base type.
Definition absorption.hpp:115
virtual void run(const size_t time_index)=0
Run the workflow.
virtual void compile()=0
Compile the work items.
Class interface for the root finder.
Definition absorption.hpp:146
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:201
void run(const size_t time_index)
Run the workflow.
Definition absorption.hpp:304
void compile()
Compile the work items.
Definition absorption.hpp:283
~root_finder()
Destructor.
Definition absorption.hpp:276
Class interface weak damping approximation.
Definition absorption.hpp:335
void run(const size_t time_index)
Run the workflow.
Definition absorption.hpp:471
~weak_damping()
Destructor.
Definition absorption.hpp:443
void compile()
Compile the work items.
Definition absorption.hpp:450
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:388
Cold Plasma expansion dispersion function.
Definition dispersion.hpp:1008
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 Dispersion function.
Definition dispersion.hpp:1035
Hot Plasma Expansion Dispersion function.
Definition dispersion.hpp:1211
Hot Plasma Dispersion function.
Definition dispersion.hpp:1096
Class interface to build dispersion relation functions.
Definition dispersion.hpp:219
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:134
Base class for a dispersion relation.
Namespace for power absorption models.
Definition absorption.hpp:101
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:995
std::vector< shared_variable< T, SAFE_MATH > > input_nodes
Convenience type alias for a vector of inputs.
Definition node.hpp:1731
shared_variable< T, SAFE_MATH > variable_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a variable node.
Definition node.hpp:1747
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:674
std::vector< std::pair< shared_leaf< T, SAFE_MATH >, shared_variable< T, SAFE_MATH > > > map_nodes
Convenience type alias for mapping end codes back to inputs.
Definition node.hpp:1735
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 tolerance=1.0E-30, const size_t max_iterations=1000, const T step=1.0)
Determine the value of vars to minimize the loss function.
Definition newton.hpp:34
Sets up the kernel for a newtons method.
Implements output files in a netcdf format.