MRPTree v0.5.0.0 examples 0.5.0.0
MR 2^P Trees (MRPTree)
Loading...
Searching...
No Matches
hello_world_adaptive.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file hello_world_adaptive.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2024-07-13
7 @brief Minimal example for MR_rect_tree with adaptive sampling.@EOL
8 @std C++23
9 @copyright
10 @parblock
11 Copyright (c) 2024, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
21 without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 DAMAGE.
29 @endparblock
30 @filedetails
31
32 This program writes a simple tabular file containing the tree's data -- one sample per line with space separated coordinates and values. These
33 may be interrupted as points in 3D, and graphed by tools like GNU Plot. On Windows running MSYS2, the following command sequence will produce a
34 graph on the screen:
35
36 make hello_world_adaptive
37 ./hello_world_adaptive.exe
38 gnuplot.exe ../examples/hello_world_adaptive.gp
39
40*/
41/*******************************************************************************************************************************************************.H.E.**/
42/** @cond exj */
43
44////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45#include "MR_rect_tree.hpp"
46
47////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48typedef mjr::tree15b2d5rT tt_t;
49
50////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51tt_t::rrpt_t damp_cos_wave(tt_t::drpt_t xvec) {
52 double x = xvec[0];
53 double y = xvec[1];
54 double d = x*x+y*y;
55 double m = std::exp(-d/4);
56 double s = std::sqrt(d);
57 double z = m*cos(4*s);
58 double dd = -m*(cos(4*s)*s+8*sin(4*s));
59 if (s>1.0e-5)
60 dd = dd / (4 * s);
61 else
62 dd = 1;
63 return {z, dd};
64}
65
66////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67int main() {
68 tt_t tree({-2.1, -2.1},
69 { 2.1, 2.1});
70
71 // Make a few samples on a uniform grid
72 tree.refine_grid(2, damp_cos_wave);
73
74 // Use the directional derivative radiating from the origin to deep sample on the humps
75 tree.refine_leaves_recursive_cell_pred(6, damp_cos_wave, [&tree](tt_t::diti_t i) { return tree.cell_cross_range_level(i, 1, 0.0); });
76
77 // Balance the three to the traditional level of 1 (no cell borders a cell more than half it's size)
78 tree.balance_tree(1, damp_cos_wave);
79
80 tree.dump_tree_datafile("hello_world_adaptive.tab");
81}
82/** @endcond */