463 * #include <iostream>
465 * #include <opencv2/core.hpp>
466 * #include <opencv2/videoio.hpp>
468 * #include <ginkgo/ginkgo.hpp>
471 *
void set_val(
unsigned char* data,
double value)
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));
485 * std::pair<cv::VideoWriter, cv::Mat> build_output(
int n,
double fps)
487 * cv::Size videosize{n, n};
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);
496 *
void output_timestep(std::pair<cv::VideoWriter, cv::Mat>& output,
int n,
497 *
const double* data)
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]);
505 * output.first.write(output.second);
509 *
int main(
int argc,
char* argv[])
515 *
auto diffusion = 0.0005;
516 *
auto source_scale = 2.5;
518 *
auto steps_per_sec = 500;
521 *
auto h = 1.0 / (n + 1);
523 *
auto tau = 1.0 / steps_per_sec;
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));
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;
540 * mtx_data.nonzeros.emplace_back(c, c - n, off_val);
543 * mtx_data.nonzeros.emplace_back(c, c - 1, off_val);
545 * mtx_data.nonzeros.emplace_back(c, c, c_val);
547 * mtx_data.nonzeros.emplace_back(c, c + 1, off_val);
550 * mtx_data.nonzeros.emplace_back(c, c + n, off_val);
554 * stencil_matrix->read(mtx_data);
555 *
auto output = build_output(n, fps);
560 * .with_baseline(gko::stop::mode::rhs_norm)
561 * .with_reduction_factor(1e-10))
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) {
568 * std::cout << t << std::endl;
572 * ->get_const_values());
574 * in_vector->add_scaled(tau_source_scalar, source);
575 *
solver->apply(in_vector, out_vector);
576 * std::swap(in_vector, out_vector);
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 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 matrix_data.hpp:126