-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExternal_field.cpp
832 lines (715 loc) · 27.4 KB
/
External_field.cpp
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include "External_field.h"
External_field::External_field( External_field_config_part &field_conf )
{
name = field_conf.name;
}
External_field::External_field( hid_t h5_external_field_group )
{
size_t grp_name_size = 0;
char *grp_name = NULL;
grp_name_size = H5Iget_name( h5_external_field_group, grp_name, grp_name_size );
grp_name_size = grp_name_size + 1;
grp_name = new char[ grp_name_size ];
grp_name_size = H5Iget_name( h5_external_field_group, grp_name, grp_name_size );
std::string longname = std::string( grp_name );
name = longname.substr( longname.find_last_of("/") + 1 );
delete[] grp_name;
}
void External_field::write_to_file( hid_t fields_group_id )
{
hid_t current_field_group_id;
herr_t status;
std::string hdf5_groupname = "./" + name;
std::string current_group = "./";
current_field_group_id = H5Gcreate( fields_group_id, hdf5_groupname.c_str(),
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hdf5_status_check( current_field_group_id );
write_hdf5_field_parameters( current_field_group_id );
status = H5Gclose( current_field_group_id ); hdf5_status_check( status );
return;
}
void External_field::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing External_field "
<< name << "."
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}
// Uniform magnetic
External_magnetic_field_uniform::External_magnetic_field_uniform(
External_magnetic_field_uniform_config_part &field_conf ) :
External_field( field_conf )
{
field_type = "magnetic_uniform";
check_correctness_of_related_config_fields( field_conf );
get_values_from_config( field_conf );
}
void External_magnetic_field_uniform::check_correctness_of_related_config_fields(
External_magnetic_field_uniform_config_part &field_conf )
{
// nothing to check here
}
void External_magnetic_field_uniform::get_values_from_config(
External_magnetic_field_uniform_config_part &field_conf )
{
magnetic_field = vec3d_init( field_conf.magnetic_field_x,
field_conf.magnetic_field_y,
field_conf.magnetic_field_z );
}
External_magnetic_field_uniform::External_magnetic_field_uniform(
hid_t h5_external_magnetic_field_uniform_group ) :
External_field( h5_external_magnetic_field_uniform_group )
{
herr_t status;
double H_x, H_y, H_z;
field_type = "magnetic_uniform";
status = H5LTget_attribute_double( h5_external_magnetic_field_uniform_group, "./",
"magnetic_uniform_field_x", &H_x );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_external_magnetic_field_uniform_group, "./",
"magnetic_uniform_field_y", &H_y );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_external_magnetic_field_uniform_group, "./",
"magnetic_uniform_field_z", &H_z );
hdf5_status_check( status );
magnetic_field = vec3d_init( H_x, H_y, H_z );
}
Vec3d External_magnetic_field_uniform::field_at_particle_position(
const Particle &p, const double &t )
{
return magnetic_field;
}
// Vec3d External_magnetic_field_uniform::force_on_particle( const Particle &p,
// const double &t )
// {
// double scale = p.charge / p.mass / speed_of_light;
// return vec3d_times_scalar( vec3d_cross_product( p.momentum, magnetic_field ),
// scale );
// }
void External_magnetic_field_uniform::write_hdf5_field_parameters(
hid_t current_field_group_id )
{
double H_x = vec3d_x( magnetic_field );
double H_y = vec3d_y( magnetic_field );
double H_z = vec3d_z( magnetic_field );
herr_t status;
int single_element = 1;
std::string current_group = "./";
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"field_type", field_type.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"magnetic_uniform_field_x", &H_x, single_element );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"magnetic_uniform_field_y", &H_y, single_element );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"magnetic_uniform_field_z", &H_z, single_element );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"speed_of_light", &physconst_speed_of_light,
single_element );
hdf5_status_check( status );
return;
}
void External_magnetic_field_uniform::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing "
<< "External_magnetic_field_uniform group. "
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}
// Uniform electric
External_electric_field_uniform::External_electric_field_uniform(
External_electric_field_uniform_config_part &field_conf ) :
External_field( field_conf )
{
field_type = "electric_uniform";
check_correctness_of_related_config_fields( field_conf );
get_values_from_config( field_conf );
}
void External_electric_field_uniform::check_correctness_of_related_config_fields(
External_electric_field_uniform_config_part &field_conf )
{
// nothing to check here
}
void External_electric_field_uniform::get_values_from_config(
External_electric_field_uniform_config_part &field_conf )
{
electric_field = vec3d_init( field_conf.electric_field_x,
field_conf.electric_field_y,
field_conf.electric_field_z );
}
External_electric_field_uniform::External_electric_field_uniform(
hid_t h5_external_electric_field_uniform_group ) :
External_field( h5_external_electric_field_uniform_group )
{
herr_t status;
double E_x, E_y, E_z;
field_type = "electric_uniform";
status = H5LTget_attribute_double( h5_external_electric_field_uniform_group, "./",
"electric_uniform_field_x", &E_x );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_external_electric_field_uniform_group, "./",
"electric_uniform_field_y", &E_y );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_external_electric_field_uniform_group, "./",
"electric_uniform_field_z", &E_z );
hdf5_status_check( status );
electric_field = vec3d_init( E_x, E_y, E_z );
}
Vec3d External_electric_field_uniform::field_at_particle_position(
const Particle &p, const double &t )
{
return electric_field;
}
// Vec3d External_electric_field_uniform::force_on_particle( const Particle &p,
// const double &t )
// {
// double scale = p.charge / p.mass;
// return vec3d_times_scalar( electric_field, scale );
// }
void External_electric_field_uniform::write_hdf5_field_parameters(
hid_t current_field_group_id )
{
double E_x = vec3d_x( electric_field );
double E_y = vec3d_y( electric_field );
double E_z = vec3d_z( electric_field );
herr_t status;
int single_element = 1;
std::string current_group = "./";
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"field_type", field_type.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"electric_uniform_field_x", &E_x, single_element );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"electric_uniform_field_y", &E_y, single_element );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"electric_uniform_field_z", &E_z, single_element );
hdf5_status_check( status );
return;
}
void External_electric_field_uniform::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing "
<< "External_electric_field_uniform group. "
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}
// Magnetic tinyexpr
External_magnetic_field_tinyexpr::External_magnetic_field_tinyexpr(
External_magnetic_field_tinyexpr_config_part &field_conf ) :
External_field( field_conf )
{
field_type = "magnetic_tinyexpr";
check_correctness_and_get_values_from_config( field_conf );
}
void External_magnetic_field_tinyexpr::check_correctness_and_get_values_from_config(
External_magnetic_field_tinyexpr_config_part &field_conf )
{
int err;
te_variable vars[] = { {"x", &te_x}, {"y", &te_y},
{"z", &te_z}, {"t", &te_t} };
Hx_expr = field_conf.magnetic_field_x;
Hy_expr = field_conf.magnetic_field_y;
Hz_expr = field_conf.magnetic_field_z;
Hx = te_compile( Hx_expr.c_str(), vars, 4, &err );
if ( !Hx ) {
printf("In %s in Hx expression:\n\t%s\n", name.c_str(), Hx_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
Hy = te_compile( Hy_expr.c_str(), vars, 4, &err );
if ( !Hy ) {
printf("In %s in Hy expression:\n\t%s\n", name.c_str(), Hy_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
Hz = te_compile( Hz_expr.c_str(), vars, 4, &err );
if ( !Hz ) {
printf("In %s in Hz expression:\n\t%s\n", name.c_str(), Hz_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
}
External_magnetic_field_tinyexpr::External_magnetic_field_tinyexpr(
hid_t h5_external_magnetic_field_tinyexpr_group ) :
External_field( h5_external_magnetic_field_tinyexpr_group )
{
herr_t status;
char h5_str_read_buffer[1000]; // expr is supposed to be tiny
field_type = "magnetic_tinyexpr";
status = H5LTget_attribute_string( h5_external_magnetic_field_tinyexpr_group, "./",
"magnetic_tinyexpr_field_x",
h5_str_read_buffer );
hdf5_status_check( status );
Hx_expr = std::string( h5_str_read_buffer );
if ( Hx_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
status = H5LTget_attribute_string( h5_external_magnetic_field_tinyexpr_group, "./",
"magnetic_tinyexpr_field_y",
h5_str_read_buffer );
hdf5_status_check( status );
Hy_expr = std::string( h5_str_read_buffer );
if ( Hy_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
status = H5LTget_attribute_string( h5_external_magnetic_field_tinyexpr_group, "./",
"magnetic_tinyexpr_field_z",
h5_str_read_buffer );
hdf5_status_check( status );
Hz_expr = std::string( h5_str_read_buffer );
if ( Hz_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
int err;
te_variable vars[] = { {"x", &te_x}, {"y", &te_y},
{"z", &te_z}, {"t", &te_t} };
Hx = te_compile( Hx_expr.c_str(), vars, 4, &err );
Hy = te_compile( Hy_expr.c_str(), vars, 4, &err );
Hz = te_compile( Hz_expr.c_str(), vars, 4, &err );
}
Vec3d External_magnetic_field_tinyexpr::field_at_particle_position(
const Particle &p, const double &t )
{
Vec3d pos = p.position;
te_x = vec3d_x( pos );
te_y = vec3d_y( pos );
te_z = vec3d_z( pos );
te_t = t;
Vec3d magnetic_field = vec3d_init( te_eval( Hx ),
te_eval( Hy ),
te_eval( Hz ) );
return magnetic_field;
}
// Vec3d External_magnetic_field_tinyexpr::force_on_particle( const Particle &p,
// const double &t )
// {
// double scale = p.charge / p.mass / speed_of_light;
// Vec3d pos = p.position;
// te_x = vec3d_x( pos );
// te_y = vec3d_y( pos );
// te_z = vec3d_z( pos );
// te_t = t;
// Vec3d magnetic_field = vec3d_init( te_eval( Hx ),
// te_eval( Hy ),
// te_eval( Hz ) );
// return vec3d_times_scalar( vec3d_cross_product( p.momentum, magnetic_field ),
// scale );
// }
void External_magnetic_field_tinyexpr::write_hdf5_field_parameters(
hid_t current_field_group_id )
{
herr_t status;
int single_element = 1;
std::string current_group = "./";
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"field_type",
field_type.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"magnetic_tinyexpr_field_x",
Hx_expr.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"magnetic_tinyexpr_field_y",
Hy_expr.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"magnetic_tinyexpr_field_z",
Hz_expr.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_double( current_field_group_id, current_group.c_str(),
"speed_of_light", &physconst_speed_of_light,
single_element );
hdf5_status_check( status );
return;
}
void External_magnetic_field_tinyexpr::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing "
<< "External_magnetic_field_tinyexpr group. "
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}
// Electric tinyexpr
External_electric_field_tinyexpr::External_electric_field_tinyexpr(
External_electric_field_tinyexpr_config_part &field_conf ) :
External_field( field_conf )
{
field_type = "electric_tinyexpr";
check_correctness_and_get_values_from_config( field_conf );
}
void External_electric_field_tinyexpr::check_correctness_and_get_values_from_config(
External_electric_field_tinyexpr_config_part &field_conf )
{
int err;
te_variable vars[] = { {"x", &te_x}, {"y", &te_y},
{"z", &te_z}, {"t", &te_t} };
Ex_expr = field_conf.electric_field_x;
Ey_expr = field_conf.electric_field_y;
Ez_expr = field_conf.electric_field_z;
Ex = te_compile( Ex_expr.c_str(), vars, 4, &err );
if ( !Ex ) {
printf("In %s in Ex expression:\n\t%s\n", name.c_str(), Ex_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
Ey = te_compile( Ey_expr.c_str(), vars, 4, &err );
if ( !Ey ) {
printf("In %s in Ey expression:\n\t%s\n", name.c_str(), Ey_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
Ez = te_compile( Ez_expr.c_str(), vars, 4, &err );
if ( !Ez ) {
printf("In %s in Ez expression:\n\t%s\n", name.c_str(), Ez_expr.c_str() );
printf("\t%*s^\nError near here\n", err-1, "");
printf("Aboring.\n");
exit( EXIT_FAILURE );
}
}
External_electric_field_tinyexpr::External_electric_field_tinyexpr(
hid_t h5_external_electric_field_tinyexpr_group ) :
External_field( h5_external_electric_field_tinyexpr_group )
{
herr_t status;
char h5_str_read_buffer[1000]; // expr is supposed to be tiny
field_type = "electric_tinyexpr";
status = H5LTget_attribute_string( h5_external_electric_field_tinyexpr_group, "./",
"electric_tinyexpr_field_x",
h5_str_read_buffer );
hdf5_status_check( status );
Ex_expr = std::string( h5_str_read_buffer );
if ( Ex_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
status = H5LTget_attribute_string( h5_external_electric_field_tinyexpr_group, "./",
"electric_tinyexpr_field_y",
h5_str_read_buffer );
hdf5_status_check( status );
Ey_expr = std::string( h5_str_read_buffer );
if ( Ey_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
status = H5LTget_attribute_string( h5_external_electric_field_tinyexpr_group, "./",
"electric_tinyexpr_field_z",
h5_str_read_buffer );
hdf5_status_check( status );
Ez_expr = std::string( h5_str_read_buffer );
if ( Ez_expr.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
int err;
te_variable vars[] = { {"x", &te_x}, {"y", &te_y},
{"z", &te_z}, {"t", &te_t} };
Ex = te_compile( Ex_expr.c_str(), vars, 4, &err );
Ey = te_compile( Ey_expr.c_str(), vars, 4, &err );
Ez = te_compile( Ez_expr.c_str(), vars, 4, &err );
}
Vec3d External_electric_field_tinyexpr::field_at_particle_position(
const Particle &p, const double &t )
{
Vec3d pos = p.position;
te_x = vec3d_x( pos );
te_y = vec3d_y( pos );
te_z = vec3d_z( pos );
te_t = t;
Vec3d electric_field = vec3d_init( te_eval( Ex ),
te_eval( Ey ),
te_eval( Ez ) );
return electric_field;
}
// Vec3d External_electric_field_tinyexpr::force_on_particle( const Particle &p,
// const double &t )
// {
// double scale = p.charge / p.mass;
// Vec3d pos = p.position;
// te_x = vec3d_x( pos );
// te_y = vec3d_y( pos );
// te_z = vec3d_z( pos );
// te_t = t;
// Vec3d electric_field = vec3d_init( te_eval( Ex ),
// te_eval( Ey ),
// te_eval( Ez ) );
// return vec3d_times_scalar( electric_field, scale );
// }
void External_electric_field_tinyexpr::write_hdf5_field_parameters(
hid_t current_field_group_id )
{
herr_t status;
std::string current_group = "./";
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"field_type",
field_type.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"electric_tinyexpr_field_x",
Ex_expr.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"electric_tinyexpr_field_y",
Ey_expr.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"electric_tinyexpr_field_z",
Ez_expr.c_str() );
hdf5_status_check( status );
return;
}
void External_electric_field_tinyexpr::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing "
<< "External_electric_field_tinyexpr group. "
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}
// Electric on_regular_grid
External_electric_field_on_regular_grid::External_electric_field_on_regular_grid(
External_electric_field_on_regular_grid_config_part &field_conf ) :
External_field( field_conf )
{
field_type = "electric_on_regular_grid";
check_correctness_and_get_values_from_config( field_conf );
load_field_from_grid();
}
void External_electric_field_on_regular_grid::check_correctness_and_get_values_from_config(
External_electric_field_on_regular_grid_config_part &field_conf )
{
h5filename = field_conf.h5filename;
}
External_electric_field_on_regular_grid::External_electric_field_on_regular_grid(
hid_t h5_external_electric_field_on_regular_grid_group ) :
External_field( h5_external_electric_field_on_regular_grid_group )
{
char h5_str_read_buffer[1000]; // todo
field_type = "electric_on_regular_grid";
herr_t status;
status = H5LTget_attribute_string( h5_external_electric_field_on_regular_grid_group,
"./",
"h5filename",
h5_str_read_buffer );
hdf5_status_check( status );
h5filename = std::string( h5_str_read_buffer );
if ( h5filename.length() >= 900 ) {
printf( "Implement support for longer strings! Aborting.\n" );
exit( EXIT_FAILURE );
}
load_field_from_grid();
}
void External_electric_field_on_regular_grid::load_field_from_grid()
{
hid_t h5file_id = H5Fopen( h5filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
if( h5file_id < 0 ){
std::cout << "Can't open file: " << h5filename << std::endl;
exit( EXIT_FAILURE );
}
// todo: read group from config
hid_t h5_spat_mesh_group = H5Gopen( h5file_id, "/Spatial_mesh", H5P_DEFAULT );
if( h5_spat_mesh_group < 0 ){
std::cout << "Something went wrong while opening Spatial_mesh group. Aborting."
<< std::endl;
exit( EXIT_FAILURE );
}
//
herr_t status;
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"x_volume_size", &x_volume_size );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"y_volume_size", &y_volume_size );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"z_volume_size", &z_volume_size );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"x_cell_size", &x_cell_size );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"y_cell_size", &y_cell_size );
hdf5_status_check( status );
status = H5LTget_attribute_double( h5_spat_mesh_group, "./",
"z_cell_size", &z_cell_size );
hdf5_status_check( status );
status = H5LTget_attribute_int( h5_spat_mesh_group, "./",
"x_n_nodes", &x_n_nodes );
hdf5_status_check( status );
status = H5LTget_attribute_int( h5_spat_mesh_group, "./",
"y_n_nodes", &y_n_nodes );
hdf5_status_check( status );
status = H5LTget_attribute_int( h5_spat_mesh_group, "./",
"z_n_nodes", &z_n_nodes );
hdf5_status_check( status );
//
int nx = x_n_nodes;
int ny = y_n_nodes;
int nz = z_n_nodes;
electric_field.resize( boost::extents[nx][ny][nz] );
//
int dim = electric_field.num_elements();
double *h5_tmp_buf_1 = new double[ dim ];
double *h5_tmp_buf_2 = new double[ dim ];
double *h5_tmp_buf_3 = new double[ dim ];
//
H5LTread_dataset_double( h5_spat_mesh_group, "./electric_field_x", h5_tmp_buf_1);
H5LTread_dataset_double( h5_spat_mesh_group, "./electric_field_y", h5_tmp_buf_2);
H5LTread_dataset_double( h5_spat_mesh_group, "./electric_field_z", h5_tmp_buf_3);
for ( int i = 0; i < dim; i++ ) {
( electric_field.data() )[i] = vec3d_init( h5_tmp_buf_1[i],
h5_tmp_buf_2[i],
h5_tmp_buf_3[i] );
}
//
delete[] h5_tmp_buf_1;
delete[] h5_tmp_buf_2;
delete[] h5_tmp_buf_3;
//
status = H5Gclose( h5_spat_mesh_group ); hdf5_status_check( status );
status = H5Fclose( h5file_id ); hdf5_status_check( status );
//
return;
}
Vec3d External_electric_field_on_regular_grid::field_at_particle_position(
const Particle &p, const double &t )
{
// copied from domain
double x = vec3d_x( p.position );
double y = vec3d_y( p.position );
double z = vec3d_z( p.position );
bool out =
( x >= x_volume_size ) || ( x <= 0 ) ||
( y >= y_volume_size ) || ( y <= 0 ) ||
( z >= z_volume_size ) || ( z <= 0 ) ;
if ( out ) {
return vec3d_zero();
}
// copied from particle_to_mesh_map
double dx = x_cell_size;
double dy = y_cell_size;
double dz = z_cell_size;
int tlf_i, tlf_j, tlf_k; // 'tlf' = 'top_left_far'
double tlf_x_weight, tlf_y_weight, tlf_z_weight;
Vec3d field_from_node, total_field;
//
next_node_num_and_weight( vec3d_x( p.position ), dx, &tlf_i, &tlf_x_weight );
next_node_num_and_weight( vec3d_y( p.position ), dy, &tlf_j, &tlf_y_weight );
next_node_num_and_weight( vec3d_z( p.position ), dz, &tlf_k, &tlf_z_weight );
// tlf
total_field = vec3d_zero();
field_from_node = vec3d_times_scalar(
electric_field[tlf_i][tlf_j][tlf_k],
tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// trf
field_from_node = vec3d_times_scalar(
electric_field[tlf_i-1][tlf_j][tlf_k],
1.0 - tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// blf
field_from_node = vec3d_times_scalar(
electric_field[tlf_i][tlf_j - 1][tlf_k],
tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// brf
field_from_node = vec3d_times_scalar(
electric_field[tlf_i-1][tlf_j-1][tlf_k],
1.0 - tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// tln
field_from_node = vec3d_times_scalar(
electric_field[tlf_i][tlf_j][tlf_k-1],
tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// trn
field_from_node = vec3d_times_scalar(
electric_field[tlf_i-1][tlf_j][tlf_k-1],
1.0 - tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// bln
field_from_node = vec3d_times_scalar(
electric_field[tlf_i][tlf_j - 1][tlf_k-1],
tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
// brn
field_from_node = vec3d_times_scalar(
electric_field[tlf_i-1][tlf_j-1][tlf_k-1],
1.0 - tlf_x_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_y_weight );
field_from_node = vec3d_times_scalar( field_from_node, 1.0 - tlf_z_weight );
total_field = vec3d_add( total_field, field_from_node );
//
return total_field;
}
void External_electric_field_on_regular_grid::next_node_num_and_weight(
const double x, const double grid_step,
int *next_node, double *weight )
{
double x_in_grid_units = x / grid_step;
*next_node = ceil( x_in_grid_units );
*weight = 1.0 - ( *next_node - x_in_grid_units );
return;
}
void External_electric_field_on_regular_grid::write_hdf5_field_parameters(
hid_t current_field_group_id )
{
herr_t status;
std::string current_group = "./";
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"field_type",
field_type.c_str() );
hdf5_status_check( status );
status = H5LTset_attribute_string( current_field_group_id, current_group.c_str(),
"h5filename",
h5filename.c_str() );
hdf5_status_check( status );
return;
}
void External_electric_field_on_regular_grid::hdf5_status_check( herr_t status )
{
if( status < 0 ){
std::cout << "Something went wrong while reading or writing "
<< "External_electric_field_on_regular_grid group. "
<< "Aborting." << std::endl;
exit( EXIT_FAILURE );
}
}