FuncViz examples
Generalized bitree/quadtree/octree library
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 @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 example illustrates how to uniformly sample a vector field. Just for fun we have also produced a solution to the Lorenz system, and directly
33 stored it with a MR_cell_cplx.
34*/
35/*******************************************************************************************************************************************************.H.E.**/
36/** @cond exj */
37
38////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39#include "MR_rect_tree.hpp"
40#include "MR_cell_cplx.hpp"
41#include "MR_rt_to_cc.hpp"
42
43////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44typedef mjr::tree15b3d3rT tt_t;
45typedef mjr::MRccT5 cc_t;
46typedef mjr::MR_rt_to_cc<tt_t, cc_t> tc_t;
47
48////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49tt_t::rrpt_t vf(tt_t::drpt_t xvec) {
50 double x = xvec[0];
51 double y = xvec[1];
52 double z = xvec[2];
53 double a = 10.0;
54 double b = 28.0;
55 double c = 8.0/3.0;
56 return { a*y-a*z,
57 x*b-x*z,
58 x*y-c*z
59 };
60}
61
62////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63int main() {
64 tt_t vftree({-30.0, -30.0, -0.0},
65 { 30.0, 30.0, 60.0});
66 cc_t vfccplx;
67
68 /* Uniform sampling */
69 vftree.refine_grid(5, vf);
70
71 /* Dump the vector field */
72 tc_t::construct_geometry_rects(vfccplx,
73 vftree,
74 0,
75 {{tc_t::val_src_spc_t::FDOMAIN, 0},
76 {tc_t::val_src_spc_t::FDOMAIN, 1},
77 {tc_t::val_src_spc_t::FDOMAIN, 2}});
78
79 vfccplx.create_named_datasets({"x", "y", "z"},
80 {{"d", {0, 1, 2}}});
81 vfccplx.dump_cplx(5);
82 vfccplx.write_xml_vtk("vector_field_3d-f.vtu", "vector_field_3d-f");
83
84 /* Now we solve the Lorenz system and directly create a cc_t object */
85 cc_t cvccplx;
86
87 int max_steps = 100000;
88 double delta = 0.001;
89 double t = 0;
90 double x_old = 0.1;
91 double y_old = 0.0;
92 double z_old = 0.0;
93 double a = 10.0;
94 double b = 28.0;
95 double c = 8.0 / 3.0;
96
97 auto p_old = cvccplx.add_node({x_old, y_old, z_old, t});
98 for(int num_steps=0;num_steps<max_steps;num_steps++) {
99 double x_new = x_old + a*(y_old-x_old)*delta;
100 double y_new = y_old + (x_old*(b-z_old)-y_old)*delta;
101 double z_new = z_old + (x_old*y_old-c*z_old)*delta;
102 t += delta;
103 auto p_new = cvccplx.add_node({x_new, y_new, z_new, t});
104 cvccplx.add_cell(cc_t::cell_kind_t::SEGMENT, {p_old, p_new});
105 x_old=x_new;
106 y_old=y_new;
107 z_old=z_new;
108 p_old=p_new;
109 }
110
111 cvccplx.dump_cplx(5);
112 cvccplx.write_xml_vtk("vector_field_3d-c.vtu", "vector_field_3d-c");
113}
114/** @endcond */
int main()