/home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/heat-equation.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/heat-equation.hpp Source File#

Reference API: /home/docs/checkouts/readthedocs.org/user_builds/ginkgo-test/checkouts/latest/build/doc/doxygen/examples/heat-equation.hpp Source File
Reference API
heat-equation.hpp
1
460 *
461 * #include <chrono>
462 * #include <fstream>
463 * #include <iostream>
464 *
465 * #include <opencv2/core.hpp>
466 * #include <opencv2/videoio.hpp>
467 *
468 * #include <ginkgo/ginkgo.hpp>
469 *
470 *
471 * void set_val(unsigned char* data, double value)
472 * {
473 * double col_r[] = {255, 221, 129, 201, 249, 255};
474 * double col_g[] = {255, 220, 130, 161, 158, 204};
475 * double col_b[] = {255, 220, 133, 93, 24, 8};
476 * value = std::max(0.0, value);
477 * auto i = std::max(0, std::min(4, int(value)));
478 * auto d = std::max(0.0, std::min(1.0, value - i));
479 * data[2] = static_cast<unsigned char>(col_r[i + 1] * d + col_r[i] * (1 - d));
480 * data[1] = static_cast<unsigned char>(col_g[i + 1] * d + col_g[i] * (1 - d));
481 * data[0] = static_cast<unsigned char>(col_b[i + 1] * d + col_b[i] * (1 - d));
482 * }
483 *
484 *
485 * std::pair<cv::VideoWriter, cv::Mat> build_output(int n, double fps)
486 * {
487 * cv::Size videosize{n, n};
488 * auto output =
489 * std::make_pair(cv::VideoWriter{}, cv::Mat{videosize, CV_8UC3});
490 * auto fourcc = cv::VideoWriter::fourcc('a', 'v', 'c', '1');
491 * output.first.open("heat.mp4", fourcc, fps, videosize);
492 * return output;
493 * }
494 *
495 *
496 * void output_timestep(std::pair<cv::VideoWriter, cv::Mat>& output, int n,
497 * const double* data)
498 * {
499 * for (int i = 0; i < n; i++) {
500 * auto row = output.second.ptr(i);
501 * for (int j = 0; j < n; j++) {
502 * set_val(&row[3 * j], data[i * n + j]);
503 * }
504 * }
505 * output.first.write(output.second);
506 * }
507 *
508 *
509 * int main(int argc, char* argv[])
510 * {
511 * using mtx = gko::matrix::Csr<>;
512 * using vec = gko::matrix::Dense<>;
513 *
514 * auto t0 = 5.0;
515 * auto diffusion = 0.0005;
516 * auto source_scale = 2.5;
517 * auto n = 256;
518 * auto steps_per_sec = 500;
519 * auto fps = 25;
520 * auto n2 = n * n;
521 * auto h = 1.0 / (n + 1);
522 * auto h2 = h * h;
523 * auto tau = 1.0 / steps_per_sec;
524 *
526 * std::ifstream initial_stream("data/gko_logo_2d.mtx");
527 * std::ifstream source_stream("data/gko_text_2d.mtx");
528 * auto source = gko::read<vec>(source_stream, exec);
529 * auto in_vector = gko::read<vec>(initial_stream, exec);
530 * auto out_vector = in_vector->clone();
531 * auto tau_source_scalar = gko::initialize<vec>({source_scale * tau}, exec);
532 * auto stencil_matrix = gko::share(mtx::create(exec));
533 * gko::matrix_data<> mtx_data{gko::dim<2>(n2, n2)};
534 * for (int i = 0; i < n; i++) {
535 * for (int j = 0; j < n; j++) {
536 * auto c = i * n + j;
537 * auto c_val = diffusion * tau * 4.0 / h2 + 1.0;
538 * auto off_val = -diffusion * tau / h2;
539 * if (i > 0) {
540 * mtx_data.nonzeros.emplace_back(c, c - n, off_val);
541 * }
542 * if (j > 0) {
543 * mtx_data.nonzeros.emplace_back(c, c - 1, off_val);
544 * }
545 * mtx_data.nonzeros.emplace_back(c, c, c_val);
546 * if (j < n - 1) {
547 * mtx_data.nonzeros.emplace_back(c, c + 1, off_val);
548 * }
549 * if (i < n - 1) {
550 * mtx_data.nonzeros.emplace_back(c, c + n, off_val);
551 * }
552 * }
553 * }
554 * stencil_matrix->read(mtx_data);
555 * auto output = build_output(n, fps);
556 * auto solver =
558 * .with_preconditioner(gko::preconditioner::Ic<>::build())
559 * .with_criteria(gko::stop::ResidualNorm<>::build()
560 * .with_baseline(gko::stop::mode::rhs_norm)
561 * .with_reduction_factor(1e-10))
562 * .on(exec)
563 * ->generate(stencil_matrix);
564 * double last_t = -t0;
565 * for (double t = 0; t < t0; t += tau) {
566 * if (t - last_t > 1.0 / fps) {
567 * last_t = t;
568 * std::cout << t << std::endl;
569 * output_timestep(
570 * output, n,
571 * gko::make_temporary_clone(exec->get_master(), in_vector)
572 * ->get_const_values());
573 * }
574 * in_vector->add_scaled(tau_source_scalar, source);
575 * solver->apply(in_vector, out_vector);
576 * std::swap(in_vector, out_vector);
577 * }
578 * }
579 * @endcode
580*/
static std::shared_ptr< CudaExecutor > create(int device_id, std::shared_ptr< Executor > master, bool device_reset, allocation_mode alloc_mode=default_cuda_alloc_mode, CUstream_st *stream=nullptr)
static std::shared_ptr< OmpExecutor > create(std::shared_ptr< CpuAllocatorBase > alloc=std::make_shared< CpuAllocator >())
Definition executor.hpp:1396
Definition csr.hpp:123
Definition ic.hpp:112
Definition cg.hpp:49
Definition residual_norm.hpp:113
detail::temporary_clone< detail::pointee< Ptr > > make_temporary_clone(std::shared_ptr< const Executor > exec, Ptr &&ptr)
Definition temporary_clone.hpp:208
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Definition utils_helper.hpp:224
Definition dim.hpp:26
Definition matrix_data.hpp:126