Logistic map as an example of how to use the ImageWriter.

This commit is contained in:
Nick Thompson 2021-01-16 15:13:33 -05:00
parent 4fac642273
commit 1bad28a3ec
3 changed files with 79 additions and 0 deletions

@ -24,6 +24,7 @@ if(VTKm_ENABLE_EXAMPLES)
add_subdirectory(histogram)
add_subdirectory(ising)
add_subdirectory(lagrangian)
add_subdirectory(logistic_map)
add_subdirectory(mesh_quality)
add_subdirectory(multi_backend)
add_subdirectory(oscillator)

@ -0,0 +1,16 @@
##============================================================================
## Copyright (c) Kitware, Inc.
## All rights reserved.
## See LICENSE.txt for details.
##
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notice for more information.
##============================================================================
cmake_minimum_required(VERSION 3.12...3.19 FATAL_ERROR)
project(LogisticMap CXX)
find_package(VTKm REQUIRED QUIET)
add_executable(LogisticMap LogisticMap.cxx)
target_link_libraries(LogisticMap PRIVATE vtkm_io)

@ -0,0 +1,62 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#include <iostream>
#include <vtkm/cont/DataSetBuilderUniform.h>
#include <vtkm/io/ImageWriterPNG.h>
int main()
{
size_t height = 1800;
size_t width = height * 1.618;
vtkm::cont::DataSetBuilderUniform dsb;
vtkm::cont::DataSet ds = dsb.Create(vtkm::Id2(width, height));
std::vector<double> x(width, 0.5);
double rmin = 2.9;
for (size_t i = 0; i < width; ++i)
{
double r = rmin + (4.0 - rmin) * i / (width - 1);
int n = 0;
// 2048 should be enough iterations to be "converged";
// though of course the iterations actually don't all converge but cycle or are chaotic.
while (n++ < 2048)
{
x[i] = r * x[i] * (1 - x[i]);
}
}
vtkm::Vec4f v(1.0, 0.5, 0.0, 0.0);
std::vector<vtkm::Vec4f> pixelValues(width * height, vtkm::Vec4f(0, 0, 0, 0));
int iterates = 0;
// We don't need more iterates than pixels of height,
// by the pigeonhole principle.
while (iterates++ < height)
{
for (size_t i = 0; i < width; ++i)
{
double r = rmin + (4.0 - rmin) * i / (width - 1);
double y = x[i];
assert(y >= 0 && y <= 1);
size_t j = std::round(y * (height - 1));
pixelValues[j * width + i] = v;
x[i] = r * x[i] * (1 - x[i]);
}
}
ds.AddPointField("pixels", pixelValues);
std::string filename = "logistic.png";
vtkm::io::ImageWriterPNG writer(filename);
writer.WriteDataSet(ds);
std::cout << "Now open " << filename << "\n";
}