KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Kernel_Iter_Adapt.cpp
Go to the documentation of this file.
2
3#include "kids/hash_fnv1a.h"
4#include "kids/macro_utils.h"
5#include "kids/vars_list.h"
6
7#define FMTF(X) \
8 " " << std::setiosflags(std::ios::fixed) /*scientific notation*/ \
9 << std::setprecision(X) /*precision*/ \
10 << std::setw(X + 4) /*precision*/
11
12namespace PROJECT_NS {
13
14const std::string Kernel_Iter_Adapt::getName() { return "Kernel_Iter_Adapt"; }
15
17
18void Kernel_Iter_Adapt::setInputParam_impl(std::shared_ptr<Param>& PM) {
19 t0 = PM->get_double("t0", LOC(), phys::time_d, 0.0f);
20 tend = PM->get_double("tend", LOC(), phys::time_d, 1.0f);
21 dt = PM->get_double("dt", LOC(), phys::time_d, 0.1f);
22
23 time_unit = PM->get_double("time_unit", LOC(), phys::time_d, 1.0f);
24
25 sstep = PM->get_int("sstep", LOC(), 1);
26 msize = PM->get_int("msize", LOC(), 128);
27 nbackup = PM->get_int("nbackup", LOC(), 1);
28 nstep = sstep * (int((tend - t0) / (sstep * dt)) + 1); // @bug?
29 nsamp = nstep / sstep + 1;
30}
31
32void Kernel_Iter_Adapt::setInputDataSet_impl(std::shared_ptr<DataSet>& DS) {
33 t_ptr = DS->def(DATA::iter::t);
34 dt_ptr = DS->def(DATA::iter::dt);
39
42
43 succ_ptr = DS->def(DATA::iter::succ);
45 frez_ptr = DS->def(DATA::iter::frez);
47
48 // initializarion
49 DS->def_int("iter.sstep", &sstep);
50 DS->def_int("iter.nstep", &nstep);
51 DS->def_int("iter.nsamp", &nsamp);
52 DS->def_int("iter.msize", &msize);
53}
54
56 t_ptr[0] = t0;
57 dt_ptr[0] = dt;
58 isamp_ptr[0] = 0;
59 istep_ptr[0] = 0;
60 tsize_ptr[0] = 0;
61 dtsize_ptr[0] = msize;
62
63 succ_ptr[0] = true;
64 last_attempt_ptr[0] = false;
65 frez_ptr[0] = false;
66 fail_type_ptr[0] = 0;
67 return stat;
68}
69
71 int last_tried_dtsize = msize;
72
73 std::cout << "S|: " //
74 << std::setw(10) << "Progress" //
75 << std::setw(10) << "Time" //
76 << std::setw(10) << "tsize" //
77 << std::setw(10) << "dtsize" //
78 << std::setw(10) << "try" //
79 << "\n";
80
81 while (istep_ptr[0] < nstep) {
82 int tsize_before_loop = tsize_ptr[0];
83 int tsize_after_loop = tsize_ptr[0] + dtsize_ptr[0];
84
85 bool at_fullstep_initially = tsize_before_loop % (msize) == 0;
86 bool at_fullstep_finally = tsize_after_loop % (msize) == 0;
87 at_samplingstep_initially_ptr[0] = tsize_before_loop % (sstep * msize) == 0;
88 at_samplingstep_finally_ptr[0] = tsize_after_loop % (sstep * msize) == 0;
89 t_ptr[0] = t0 + dt * (tsize_ptr[0] / ((double) msize));
90 dt_ptr[0] = dt * (dtsize_ptr[0] / ((double) msize));
91
92 // backup before loop
93 for (auto& fname : backup_fields) {
94 for (int bto = nbackup, bfrom = bto - 1; bto > 1; --bto, --bfrom) {
95 _dataset->_def(utils::concat("backup.", bto, ".", fname), utils::concat("backup.", bfrom, ".", fname));
96 }
97 _dataset->_def(utils::concat("backup.", 1, ".", fname), //
98 utils::concat("integrator.", fname));
99 }
100
101 // for each loop, we always previously set succ_ptr[0] = true
102 succ_ptr[0] = true;
103 for (auto& pkernel : _child_kernels) { pkernel->executeKernel(stat); }
104
105 char statc = '?';
106 if (succ_ptr[0] && !last_attempt_ptr[0]) statc = 'T';
107 if (succ_ptr[0] && last_attempt_ptr[0]) statc = 'R';
108 if (!succ_ptr[0] && last_attempt_ptr[0]) statc = 'X';
109 if (!succ_ptr[0] && !last_attempt_ptr[0] && dtsize_ptr[0] > 1) statc = 'F';
110 if (!succ_ptr[0] && !last_attempt_ptr[0] && dtsize_ptr[0] == 1) statc = 'L';
111 if (frez_ptr[0]) statc = 'Z';
112
113 if (std::ifstream{"X_STAT"}.good() && !frez_ptr[0]) statc = 'X';
114 if (std::ifstream{utils::concat("X_STAT", stat.icalc)}.good() && !frez_ptr[0]) statc = 'X';
115
116 switch (statc) {
117 case 'X': {
118 // save breakdown information
119 std::string directory = _param->get_string("directory", LOC());
120 std::ofstream ofs{utils::concat(directory, "/fail", stat.icalc, "-", istep_ptr[0], ".ds")};
121 _dataset->dump(ofs);
122 ofs.close();
123
124 frez_ptr[0] = true;
125 // not break here!
126 }
127 case 'T':
128 case 'R':
129 case 'Z': {
130 last_attempt_ptr[0] = false;
131 if (at_fullstep_finally) istep_ptr[0]++;
132 tsize_ptr[0] += dtsize_ptr[0];
133
134 int extend_dtsize = (at_fullstep_finally) ? 2 * last_tried_dtsize : 2 * dtsize_ptr[0];
135 int remain_dtsize = msize - (tsize_ptr[0] % msize);
136 int new_dtsize = std::min({msize, extend_dtsize, remain_dtsize});
137 last_tried_dtsize = dtsize_ptr[0];
138 dtsize_ptr[0] = new_dtsize;
139 break;
140 }
141 case 'L':
142 case 'F': {
143 last_attempt_ptr[0] = (dtsize_ptr[0] == 1);
144 last_tried_dtsize = dtsize_ptr[0];
145 dtsize_ptr[0] = (last_attempt_ptr[0]) ? dtsize_ptr[0] : dtsize_ptr[0] / 2;
146 tsize_ptr[0] += 0;
147
148 for (auto& fname : backup_fields) {
149 _dataset->_def(utils::concat("integrator.", fname), //
150 utils::concat("backup.", 1, ".", fname) //
151 );
152 for (int bto = nbackup, bfrom = bto - 1; bto > 1; --bto, --bfrom) {
153 _dataset->_def(utils::concat("backup.", bfrom, ".", fname),
154 utils::concat("backup.", bto, ".", fname));
155 }
156 }
157 break;
158 }
159 }
160 std::cout << statc << "|:" //
161 << std::resetiosflags(std::ios::scientific) << std::setiosflags(std::ios::fixed)
162 << std::setprecision(2) << std::setw(10) << 100 * t_ptr[0] / tend << "%" //
163 << std::resetiosflags(std::ios::fixed) << std::setiosflags(std::ios::scientific)
164 << std::setprecision(2) << std::setw(10) << t_ptr[0] / time_unit //
165 << std::setw(10) << tsize_before_loop //
166 << std::setw(10) << last_tried_dtsize //
167 << std::setw(10) << dtsize_ptr[0] << "\n";
168 isamp_ptr[0] = istep_ptr[0] / sstep;
169 }
171 dt_ptr[0] = 0;
172 return stat;
173}
174
175}; // namespace PROJECT_NS
virtual void setInputParam_impl(std::shared_ptr< Param > &PM)
Virtual function to set input parameters for the kernel implementation.
virtual Status & initializeKernel_impl(Status &stat)
Virtual function to initialize the kernel implementation.
virtual void setInputDataSet_impl(std::shared_ptr< DataSet > &DS)
Virtual function to set input data set for the kernel implementation.
virtual const std::string getName()
Get the name of the kernel.
virtual Status & executeKernel_impl(Status &stat)
Virtual function to execute the kernel implementation.
const std::vector< std::string > backup_fields
virtual int getType() const
Get the type of the kernel.
std::vector< std::shared_ptr< Kernel > > _child_kernels
Vector containing shared pointers to the child kernels of this kernel.
Definition Kernel.h:298
std::shared_ptr< Param > _param
Shared pointer to the Param object associated with this kernel.
Definition Kernel.h:273
std::shared_ptr< DataSet > _dataset
Shared pointer to the DataSet object associated with this kernel.
Definition Kernel.h:278
#define LOC()
show the location information for debug
Definition fmt.h:49
#define FUNCTION_NAME
Definition macro_utils.h:9
VARIABLE< kids_real > dt
VARIABLE< kids_bint > frez
VARIABLE< kids_real > t
VARIABLE< kids_bint > at_samplingstep_finally
VARIABLE< kids_bint > at_samplingstep_initially
VARIABLE< kids_bint > last_attempt
VARIABLE< kids_int > istep
VARIABLE< kids_int > dtsize
VARIABLE< kids_int > tsize
VARIABLE< kids_int > isamp
VARIABLE< kids_int > fail_type
VARIABLE< kids_bint > succ
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
constexpr dimension7 time_d
[T]
Definition phys.h:187
std::basic_string< CharT > concat(const separator_t< CharT > &sep, Args &&... seq)
Definition concat.h:242
constexpr uint32_t hash(const char *str)
Definition hash_fnv1a.h:12
declaration of variables used in the program.