diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index cbc8a4e25..fcc2f92bf 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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) diff --git a/examples/logistic_map/CMakeLists.txt b/examples/logistic_map/CMakeLists.txt new file mode 100644 index 000000000..4e6f359aa --- /dev/null +++ b/examples/logistic_map/CMakeLists.txt @@ -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) diff --git a/examples/logistic_map/LogisticMap.cxx b/examples/logistic_map/LogisticMap.cxx new file mode 100644 index 000000000..270b698ee --- /dev/null +++ b/examples/logistic_map/LogisticMap.cxx @@ -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 +#include +#include + + +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 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 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"; +}