MRPTree v0.5.0.0 examples-func-viz 0.5.0.0
MR 2^P Trees (MRPTree)
Loading...
Searching...
No Matches
vector_field_3d.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file vector_field_3d.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2024-07-14
7 @brief Vector field for Lorenz system.@EOL
8 @std C++23
9 @see https://www.mitchr.me/SS/MRPTree/func-viz/index.html#vector_field_3d
10 @see https://github.com/richmit/MRPTree/blob/main/examples-func-viz/vector_field_3d.cpp
11 @copyright
12 @parblock
13 Copyright (c) 2024, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
16
17 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
20 and/or other materials provided with the distribution.
21
22 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
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 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
30 DAMAGE.
31 @endparblock
32 @filedetails
33
34 This example illustrates how to uniformly sample a vector field. Just for fun we have also produced a solution to the Lorenz system, and directly
35 stored it with a `MR_cell_cplx`.
36*/
37/*******************************************************************************************************************************************************.H.E.**/
38/** @cond exj */
39
40////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41#include "MR_rect_tree.hpp"
42#include "MR_cell_cplx.hpp"
43#include "MR_rt_to_cc.hpp"
44
45////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46typedef mjr::tree15b3d3rT tt_t;
47typedef mjr::MRccT5 cc_t;
48typedef mjr::MR_rt_to_cc<tt_t, cc_t> tc_t;
49
50////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51tt_t::rrpt_t vf(tt_t::drpt_t xvec) {
52 double x = xvec[0];
53 double y = xvec[1];
54 double z = xvec[2];
55 double a = 10.0;
56 double b = 28.0;
57 double c = 8.0/3.0;
58 return { a*y-a*z,
59 x*b-x*z,
60 x*y-c*z
61 };
62}
63
64////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65int main() {
66 tt_t vftree({-30.0, -30.0, -0.0},
67 { 30.0, 30.0, 60.0});
68 cc_t vfccplx;
69
70 /* Uniform sampling */
71 vftree.refine_grid(5, vf);
72
73 /* Dump the vector field */
74 tc_t::construct_geometry_rects(vfccplx,
75 vftree,
76 0,
77 {{tc_t::val_src_spc_t::FDOMAIN, 0},
78 {tc_t::val_src_spc_t::FDOMAIN, 1},
79 {tc_t::val_src_spc_t::FDOMAIN, 2}});
80
81 vfccplx.create_named_datasets({"x", "y", "z"},
82 {{"d", {0, 1, 2}}});
83 vfccplx.dump_cplx(5);
84 vfccplx.write_xml_vtk("vector_field_3d-f.vtu", "vector_field_3d-f");
85
86 /* Now we solve the Lorenz system and directly create a cc_t object */
87 cc_t cvccplx;
88
89 int max_steps = 100000;
90 double delta = 0.001;
91 double t = 0;
92 double x_old = 0.1;
93 double y_old = 0.0;
94 double z_old = 0.0;
95 double a = 10.0;
96 double b = 28.0;
97 double c = 8.0 / 3.0;
98
99 auto p_old = cvccplx.add_node({x_old, y_old, z_old, t});
100 for(int num_steps=0;num_steps<max_steps;num_steps++) {
101 double x_new = x_old + a*(y_old-x_old)*delta;
102 double y_new = y_old + (x_old*(b-z_old)-y_old)*delta;
103 double z_new = z_old + (x_old*y_old-c*z_old)*delta;
104 t += delta;
105 auto p_new = cvccplx.add_node({x_new, y_new, z_new, t});
106 cvccplx.add_cell(cc_t::cell_kind_t::SEGMENT, {p_old, p_new});
107 x_old=x_new;
108 y_old=y_new;
109 z_old=z_new;
110 p_old=p_new;
111 }
112
113 cvccplx.dump_cplx(5);
114 cvccplx.write_xml_vtk("vector_field_3d-c.vtu", "vector_field_3d-c");
115}
116/** @endcond */
int main()