-
Notifications
You must be signed in to change notification settings - Fork 9
/
inner_region.h
528 lines (494 loc) · 17.9 KB
/
inner_region.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#ifndef _INNER_REGION_H_
#define _INNER_REGION_H_
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <boost/ptr_container/ptr_vector.hpp>
#include <hdf5.h>
#include <hdf5_hl.h>
#include "config.h"
#include "spatial_mesh.h"
#include "node_reference.h"
#include "particle.h"
#include "vec3d.h"
class Inner_region{
public:
std::string name;
std::string object_type;
double potential;
int total_absorbed_particles;
double total_absorbed_charge;
//int absorbed_particles_current_timestep_current_proc;
//double absorbed_charge_current_timestep_current_proc;
public:
std::vector<Node_reference> inner_nodes;
// todo: delete
std::vector<Node_reference> inner_nodes_not_at_domain_edge;
std::vector<Node_reference> near_boundary_nodes;
std::vector<Node_reference> near_boundary_nodes_not_at_domain_edge;
public:
Inner_region( Config &conf, Inner_region_config_part &inner_region_conf );
Inner_region( hid_t h5_inner_region_group_id );
virtual ~Inner_region() {};
virtual void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z ) = 0;
bool check_if_particle_inside( Particle &p );
bool check_if_particle_inside_and_count_charge( Particle &p );
bool check_if_node_inside( Node_reference &node, double dx, double dy, double dz );
void print_inner_nodes() {
std::cout << "Inner nodes of '" << name << "' object." << std::endl;
for( auto &node : inner_nodes )
node.print();
};
void print_near_boundary_nodes() {
std::cout << "Near-boundary nodes of '" << name << "' object." << std::endl;
for( auto &node : near_boundary_nodes )
node.print();
};
// Write to file
void write_to_file( hid_t regions_group_id );
void hdf5_status_check( herr_t status );
protected:
void mark_inner_nodes( Spatial_mesh &spat_mesh );
void select_inner_nodes_not_at_domain_edge( Spatial_mesh &spat_mesh );
void mark_near_boundary_nodes( Spatial_mesh &spat_mesh );
void select_near_boundary_nodes_not_at_domain_edge( Spatial_mesh &spat_mesh );
void write_hdf5_common_parameters( hid_t current_region_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id ) = 0;
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_config_part &inner_region_conf );
virtual void get_values_from_config(
Inner_region_config_part &inner_region_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_group_id );
};
class Inner_region_box : public Inner_region{
public:
double x_left;
double x_right;
double y_bottom;
double y_top;
double z_near;
double z_far;
public:
Inner_region_box( Config &conf,
Inner_region_box_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_box( hid_t h5_inner_region_box_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_box() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "x_left = " << x_left << std::endl;
std::cout << "x_right = " << x_right << std::endl;
std::cout << "y_bottom = " << y_bottom << std::endl;
std::cout << "y_top = " << y_top << std::endl;
std::cout << "z_near = " << z_near << std::endl;
std::cout << "z_far = " << z_far << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_box_config_part &inner_region_box_conf );
virtual void get_values_from_config(
Inner_region_box_config_part &inner_region_box_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_box_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_region_sphere : public Inner_region{
public:
double origin_x;
double origin_y;
double origin_z;
double radius;
public:
Inner_region_sphere(
Config &conf,
Inner_region_sphere_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_sphere( hid_t h5_inner_region_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_sphere() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "origin_x = " << origin_x << std::endl;
std::cout << "origin_y = " << origin_y << std::endl;
std::cout << "origin_z = " << origin_z << std::endl;
std::cout << "radius = " << radius << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_sphere_config_part &inner_region_sphere_conf );
virtual void get_values_from_config(
Inner_region_sphere_config_part &inner_region_sphere_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_sphere_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_region_cylinder : public Inner_region{
public:
double axis_start_x;
double axis_start_y;
double axis_start_z;
double axis_end_x;
double axis_end_y;
double axis_end_z;
double radius;
public:
Inner_region_cylinder(
Config &conf,
Inner_region_cylinder_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_cylinder( hid_t h5_inner_region_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_cylinder() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "axis_start_x = " << axis_start_x << std::endl;
std::cout << "axis_start_y = " << axis_start_y << std::endl;
std::cout << "axis_start_z = " << axis_start_z << std::endl;
std::cout << "axis_end_x = " << axis_end_x << std::endl;
std::cout << "axis_end_y = " << axis_end_y << std::endl;
std::cout << "axis_end_z = " << axis_end_z << std::endl;
std::cout << "radius = " << radius << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_cylinder_config_part &inner_region_cylinder_conf );
virtual void get_values_from_config(
Inner_region_cylinder_config_part &inner_region_cylinder_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_cylinder_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_region_tube : public Inner_region{
public:
double axis_start_x;
double axis_start_y;
double axis_start_z;
double axis_end_x;
double axis_end_y;
double axis_end_z;
double inner_radius;
double outer_radius;
public:
Inner_region_tube(
Config &conf,
Inner_region_tube_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_tube( hid_t h5_inner_region_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_tube() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "axis_start_x = " << axis_start_x << std::endl;
std::cout << "axis_start_y = " << axis_start_y << std::endl;
std::cout << "axis_start_z = " << axis_start_z << std::endl;
std::cout << "axis_end_x = " << axis_end_x << std::endl;
std::cout << "axis_end_y = " << axis_end_y << std::endl;
std::cout << "axis_end_z = " << axis_end_z << std::endl;
std::cout << "inner_radius = " << inner_radius << std::endl;
std::cout << "outer_radius = " << outer_radius << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_tube_config_part &inner_region_tube_conf );
virtual void get_values_from_config(
Inner_region_tube_config_part &inner_region_tube_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_tube_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_region_tube_along_z_segment : public Inner_region{
public:
double axis_x;
double axis_y;
double axis_start_z;
double axis_end_z;
double inner_radius;
double outer_radius;
double start_angle_deg;
double end_angle_deg;
public:
Inner_region_tube_along_z_segment(
Config &conf,
Inner_region_tube_along_z_segment_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_tube_along_z_segment( hid_t h5_inner_region_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_tube_along_z_segment() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "axis_x = " << axis_x << std::endl;
std::cout << "axis_y = " << axis_y << std::endl;
std::cout << "axis_start_z = " << axis_start_z << std::endl;
std::cout << "axis_end_z = " << axis_end_z << std::endl;
std::cout << "inner_radius = " << inner_radius << std::endl;
std::cout << "outer_radius = " << outer_radius << std::endl;
std::cout << "start_angle_deg = " << start_angle_deg << std::endl;
std::cout << "end_angle_deg = " << end_angle_deg << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_tube_along_z_segment_config_part
&inner_region_tube_along_z_segment_conf );
virtual void get_values_from_config(
Inner_region_tube_along_z_segment_config_part
&inner_region_tube_along_z_segment_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_tube_along_z_segment_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_region_cone_along_z : public Inner_region{
public:
double axis_x;
double axis_y;
double axis_start_z;
double axis_end_z;
double start_inner_radius;
double start_outer_radius;
double end_inner_radius;
double end_outer_radius;
public:
Inner_region_cone_along_z(
Config &conf,
Inner_region_cone_along_z_config_part &inner_region_conf,
Spatial_mesh &spat_mesh );
Inner_region_cone_along_z( hid_t h5_inner_region_group_id,
Spatial_mesh &spat_mesh );
virtual ~Inner_region_cone_along_z() {};
void print() {
std::cout << "Inner region: name = " << name << std::endl;
std::cout << "potential = " << potential << std::endl;
std::cout << "axis_x = " << axis_x << std::endl;
std::cout << "axis_y = " << axis_y << std::endl;
std::cout << "axis_start_z = " << axis_start_z << std::endl;
std::cout << "axis_end_z = " << axis_end_z << std::endl;
std::cout << "start_inner_radius = "
<< start_inner_radius << std::endl;
std::cout << "start_outer_radius = "
<< start_outer_radius << std::endl;
std::cout << "end_inner_radius = "
<< end_inner_radius << std::endl;
std::cout << "end_outer_radius = "
<< end_outer_radius << std::endl;
}
virtual bool check_if_point_inside( double x, double y, double z );
bool point_inside_cone( double axis_x, double axis_y,
double axis_start_z, double axis_end_z,
double r_start, double r_end,
double x, double y, double z );
private:
virtual void check_correctness_of_related_config_fields(
Config &conf,
Inner_region_cone_along_z_config_part &inner_region_cone_along_z_conf );
virtual void get_values_from_config(
Inner_region_cone_along_z_config_part &inner_region_cone_along_z_conf );
virtual void get_values_from_h5(
hid_t h5_inner_region_cone_along_z_group_id );
virtual void write_hdf5_region_specific_parameters(
hid_t current_region_group_id );
};
class Inner_regions_manager{
public:
boost::ptr_vector<Inner_region> regions;
public:
Inner_regions_manager( Config &conf, Spatial_mesh &spat_mesh )
{
for( auto &inner_region_conf : conf.inner_regions_config_part ){
if( Inner_region_box_config_part *box_conf =
dynamic_cast<Inner_region_box_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_box( conf,
*box_conf,
spat_mesh ) );
} else if( Inner_region_sphere_config_part *sphere_conf =
dynamic_cast<Inner_region_sphere_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_sphere( conf,
*sphere_conf,
spat_mesh ) );
} else if( Inner_region_cylinder_config_part *cyl_conf =
dynamic_cast<Inner_region_cylinder_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_cylinder( conf,
*cyl_conf,
spat_mesh ) );
} else if( Inner_region_tube_config_part *tube_conf =
dynamic_cast<Inner_region_tube_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_tube( conf,
*tube_conf,
spat_mesh ) );
} else if( Inner_region_tube_along_z_segment_config_part
*tube_along_z_segment_conf =
dynamic_cast<Inner_region_tube_along_z_segment_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_tube_along_z_segment(
conf,
*tube_along_z_segment_conf,
spat_mesh ) );
} else if( Inner_region_cone_along_z_config_part
*cone_along_z_conf =
dynamic_cast<Inner_region_cone_along_z_config_part*>( &inner_region_conf ) ){
regions.push_back( new Inner_region_cone_along_z(
conf,
*cone_along_z_conf,
spat_mesh ) );
} else {
std::cout << "In Inner_regions_manager constructor-from-conf: "
<< "Unknown inner_region type. Aborting"
<< std::endl;
exit( EXIT_FAILURE );
}
}
}
Inner_regions_manager( hid_t h5_inner_region_group,
Spatial_mesh &spat_mesh )
{
hsize_t nobj;
ssize_t len;
herr_t err;
int otype;
size_t MAX_NAME = 1024;
char memb_name_cstr[MAX_NAME];
hid_t current_ir_grpid;
err = H5Gget_num_objs(h5_inner_region_group, &nobj);
for( hsize_t i = 0; i < nobj; i++ ){
len = H5Gget_objname_by_idx(h5_inner_region_group, i,
memb_name_cstr, MAX_NAME );
hdf5_status_check( len );
otype = H5Gget_objtype_by_idx( h5_inner_region_group, i );
if ( otype == H5G_GROUP ) {
current_ir_grpid = H5Gopen( h5_inner_region_group,
memb_name_cstr, H5P_DEFAULT );
parse_hdf5_inner_reg( current_ir_grpid, spat_mesh );
err = H5Gclose( current_ir_grpid );
hdf5_status_check( err );
}
}
// To iterate over subgroups, there is H5Giterate function.
// However, it needs a callback function as one of it's arguments.
// H5Giterate( h5_inner_region_group, "./", NULL,
// parse_hdf5_inner_reg, &spat_mesh );
// It is difficult to pass a C++ method as a callback.
// See: https://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr
// Therefore, iteration is manual.
}
void parse_hdf5_inner_reg( hid_t current_ir_grpid,
Spatial_mesh &spat_mesh )
{
herr_t status;
char object_type_cstr[50];
status = H5LTget_attribute_string(
current_ir_grpid, "./", "object_type", object_type_cstr );
hdf5_status_check( status );
std::string obj_type( object_type_cstr );
if( obj_type == "box" ){
regions.push_back( new Inner_region_box( current_ir_grpid,
spat_mesh ) );
} else if ( obj_type == "sphere" ) {
regions.push_back( new Inner_region_sphere( current_ir_grpid,
spat_mesh ) );
} else if ( obj_type == "cylinder" ) {
regions.push_back( new Inner_region_cylinder( current_ir_grpid,
spat_mesh ) );
} else if ( obj_type == "tube" ) {
regions.push_back( new Inner_region_tube( current_ir_grpid,
spat_mesh ) );
} else if ( obj_type == "tube_along_z_segment" ) {
regions.push_back( new Inner_region_tube_along_z_segment(
current_ir_grpid, spat_mesh ) );
} else if ( obj_type == "cone_along_z" ) {
regions.push_back( new Inner_region_cone_along_z(
current_ir_grpid, spat_mesh ) );
} else {
std::cout << "In Inner_regions_manager constructor-from-h5: "
<< "Unknown inner_region type. Aborting"
<< std::endl;
exit( EXIT_FAILURE );
}
}
virtual ~Inner_regions_manager() {};
bool check_if_particle_inside( Particle &p )
{
for( auto ®ion : regions ){
if( region.check_if_particle_inside( p ) )
return true;
}
return false;
}
bool check_if_particle_inside_and_count_charge( Particle &p )
{
for( auto ®ion : regions ){
if( region.check_if_particle_inside_and_count_charge( p ) )
return true;
}
return false;
}
void print( )
{
for( auto ®ion : regions )
region.print();
}
void print_inner_nodes() {
for( auto ®ion : regions )
region.print_inner_nodes();
}
void print_near_boundary_nodes() {
for( auto ®ion : regions )
region.print_near_boundary_nodes();
}
void write_to_file( hid_t hdf5_file_id )
{
hid_t group_id;
herr_t status;
int single_element = 1;
std::string hdf5_groupname = "/InnerRegions";
int n_of_regions = regions.size();
group_id = H5Gcreate2(
hdf5_file_id, hdf5_groupname.c_str(),
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
hdf5_status_check( group_id );
status = H5LTset_attribute_int(
hdf5_file_id, hdf5_groupname.c_str(),
"number_of_regions", &n_of_regions, single_element );
hdf5_status_check( status );
for( auto ® : regions )
reg.write_to_file( group_id );
status = H5Gclose(group_id);
hdf5_status_check( status );
};
void hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while "
<< "writing or reading Inner_regions group. Aborting."
<< std::endl;
exit( EXIT_FAILURE );
}
};
};
#endif /* _INNER_REGION_H_ */