Octopus
external_waves.F90
Go to the documentation of this file.
1!! Copyright (C) 2023 E.I. Albar, F. Bonafe and Heiko Appel
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17
18#include "global.h"
19
21 use accel_oct_m
24 use clock_oct_m
26 use debug_oct_m
30 use global_oct_m
31 use grid_oct_m
32 use index_oct_m
37 use, intrinsic :: iso_fortran_env
39 use io_oct_m
41 use lasers_oct_m
45 use mesh_oct_m
47 use mpi_oct_m
52 use parser_oct_m
56 use string_oct_m
57 use types_oct_m
58 use unit_oct_m
62
63 implicit none
64
65 private
66 public :: &
73
74 type bessel_beam_t
75 integer, allocatable :: helicity(:)
76 integer, allocatable :: m_order(:)
77 real(real64), allocatable :: amp(:)
78 real(real64), allocatable :: theta_k(:)
79 real(real64), allocatable :: omega(:)
80 real(real64), allocatable :: shift(:,:)
81 logical, allocatable :: envelope(:)
82 integer, allocatable :: lin_dir(:)
83 contains
84 procedure :: init => bessel_beam_init
85 procedure :: function => bessel_beam_function
87 end type bessel_beam_t
88
90 integer :: points_number
91 integer, allocatable :: points_map(:)
92 integer :: number
93 integer, allocatable :: modus(:)
94 ! !! (see MAXWELLINCIDENTWAVES)
95 integer, allocatable :: field_type(:)
96 character(len=1024), allocatable :: e_field_string(:,:)
97 real(real64), allocatable :: k_vector(:,:)
98 real(real64), allocatable :: v_vector(:,:)
99 complex(real64), allocatable :: e_field(:,:)
100 real(real64), allocatable :: pw_phase(:)
101 type(mxf_t), allocatable :: mx_function(:)
102 integer :: out_file
103 logical :: output_from_point = .false.
104 real(real64), allocatable :: selected_point_coordinate(:)
105 real(real64), allocatable :: selected_point_field(:)
106 real(real64) :: c_factor
107 type(accel_mem_t) :: buff_map
108 type(bessel_beam_t) :: bessel
109 contains
110 procedure :: init_interaction_as_partner => external_waves_init_interaction_as_partner
111 procedure :: update_quantity => external_waves_update_quantity
112 procedure :: copy_quantities_to_interaction => external_waves_copy_quantities_to_interaction
114 end type external_waves_t
115
116 interface external_waves_t
117 module procedure external_waves_constructor
118 end interface external_waves_t
119
120contains
121
122 function external_waves_constructor(namespace) result(this)
123 class(external_waves_t), pointer :: this
124 type(namespace_t), intent(in) :: namespace
125
126 integer :: iq
127 character(len=:), allocatable :: quantities(:)
128
130
131 safe_allocate(this)
132
133 this%namespace = namespace_t("ExternalSource", parent=namespace)
134
135 message(1) = 'Plane-wave is currently always 3D and non-periodic.'
136 call messages_warning(1)
137 call external_waves_init(this, this%namespace)
138
139 quantities = [character(16) :: "E field", "vector potential", "B field"]
140 do iq = 1, size(quantities)
141 call this%quantities%add(quantity_t(quantities(iq), always_available = .true., &
142 updated_on_demand = .true., iteration = clock_t()))
143 end do
144
145 this%supported_interactions_as_partner = [mxll_e_field_to_matter, mxll_b_field_to_matter, &
147
149 end function external_waves_constructor
150
151 ! ---------------------------------------------------------
152 subroutine external_waves_init_interaction_as_partner(partner, interaction)
153 class(external_waves_t), intent(in) :: partner
154 class(interaction_surrogate_t), intent(inout) :: interaction
155
157
158 select type (interaction)
159 type is (lorentz_force_t)
160 ! Nothing to be initialized
162 ! Nothing to be initialized
164 ! Nothing to be initialized
166 ! Nothing to be initialized
167 class default
168 message(1) = "Unsupported interaction."
169 call messages_fatal(1, namespace=partner%namespace)
170 end select
174 ! ---------------------------------------------------------
175 subroutine external_waves_update_quantity(this, label)
176 class(external_waves_t), intent(inout) :: this
177 character(len=*), intent(in) :: label
180
181 select case (label)
182 case ("E field", "B field", "vector potential")
183 ! We will not update the quantities here because they are computed
184 ! on-the-fly when copying them to the corresponding interaction (see
185 ! copy_quantities_to_interaction routine)
186 case default
187 message(1) = "Incompatible quantity."
188 call messages_fatal(1, namespace=this%namespace)
189 end select
194 ! ---------------------------------------------------------
195 subroutine external_waves_copy_quantities_to_interaction(partner, interaction)
196 class(external_waves_t), intent(inout) :: partner
197 class(interaction_surrogate_t), intent(inout) :: interaction
199 class(quantity_t), pointer :: quantity
202
203 select type(interaction)
205 quantity => partner%quantities%get("E field")
206 interaction%system_field = m_zero
207 call external_waves_eval(partner, quantity%iteration%value(), interaction%system_gr, "E field", &
208 interaction%system_field)
209 call interaction%do_mapping()
210
211 class is (mxll_vec_pot_to_matter_t)
212 quantity => partner%quantities%get("vector potential")
213 interaction%system_field = m_zero
214 call external_waves_eval(partner, quantity%iteration%value(), interaction%system_gr, &
215 "vector potential", interaction%system_field)
216 call interaction%do_mapping()
217
218 class is (mxll_b_field_to_matter_t)
219 quantity => partner%quantities%get("B field")
220 interaction%system_field = m_zero
221 call external_waves_eval(partner, quantity%iteration%value(), interaction%system_gr, "B field", &
222 interaction%system_field, der=interaction%system_gr%der)
223 call interaction%do_mapping()
224
225 class default
226 message(1) = "Incompatible interaction."
227 call messages_fatal(1, namespace=partner%namespace)
228 end select
229
232
233 ! ---------------------------------------------------------
234 ! Load the external source for the multisystem framework
235 subroutine load_external_waves(partners, namespace)
236 class(partner_list_t), intent(inout) :: partners
237 type(namespace_t), intent(in) :: namespace
238
239 logical :: has_source
240
241 push_sub(load_external_waves)
242
243 !%Variable AnalyticalExternalSource
244 !%Type logical
245 !%Default no
246 !%Section Maxwell
247 !%Description
248 !% This means the analytical evaluation of formula will be used, Maxwell propagation will not be used.
249 !%End
250 call parse_variable(namespace, 'AnalyticalExternalSource', .false., has_source)
251
252 if (has_source) then
253 call partners%add(external_waves_t(namespace))
254 end if
255
256 pop_sub(load_external_waves)
257 end subroutine load_external_waves
258
259
261 ! ---------------------------------------------------------
262 subroutine external_waves_init(external_waves, namespace)
263 type(external_waves_t), intent(inout) :: external_waves
264 type(namespace_t), intent(in) :: namespace
265 type(block_t) :: blk
266 integer :: il, nlines, ncols, iex_norm, idim
267 integer, parameter :: sys_dim = 3
268 real(real64) :: k_vector(sys_dim), velocity(sys_dim), x_pos(sys_dim)
269 real(real64) :: x_norm, dummy(sys_dim), k_dot_e , test_limit, k_norm, output_pos(3)
270 complex(real64) :: e_field(sys_dim)
271 character(len=1024) :: k_string(sys_dim)
272 character(len=1), dimension(sys_dim), parameter :: dims = ["x", "y", "z"]
273 character(len=1024) :: mxf_expression
274
275 push_sub(external_waves_init)
276
277 call profiling_in('EXTERNAL_WAVES_INIT')
278
279 test_limit = 10.0e-9_real64
280
281 !%Variable ExternalSourceBesselOutput
282 !%Type block
283 !%Section Maxwell
284 !%Description
285 !% The ExternalSourceBesselOutput block allows to output analytically calculated fields at a
286 !% particular point in space. The columns denote the x, y, and z coordinate of the point.
287 !% Please be aware that ExternalSource lives on the grid of the system that it is applied to.
288 !% Therefore, it might not be evaluated at every point in space. When comparing, please be sure
289 !% to check the log and compare if your required point in space matches the evaluated position.
290 !%
291 !% <tt>%ExternalSourceBesselOutput
292 !% <br>&nbsp;&nbsp; -1.0 | 2.0 | 4.0
293 !% <br>%</tt>
294 !%
295 !%End
296
297 if (parse_block(namespace, 'ExternalSourceBesselOutput', blk) == 0) then
298 nlines = parse_block_n(blk)
299 if (nlines > 1 ) then
300 message(2) = 'ExternalSource output is limited to one point.'
301 call messages_fatal(1, namespace=namespace)
302 end if
303 ncols = parse_block_cols(blk,0)
304 if (ncols /= 3 ) then
305 message(1) = 'ExternalSourceBesselOutput must have 3 columns.'
306 call messages_fatal(1, namespace=namespace)
307 end if
308 external_waves%output_from_point= .true.
309 safe_allocate(external_waves%selected_point_coordinate(1:3))
310 safe_allocate(external_waves%selected_point_field(1:3))
311
312 do idim = 1, 3
313 call parse_block_float(blk, 0, idim-1, output_pos(idim), units_inp%length)
314 end do
315 external_waves%selected_point_coordinate(1:3) = output_pos(1:3)
316 external_waves%selected_point_field(1:3) = m_zero
317
318 call parse_block_end(blk)
319 call io_mkdir('ExternalSource')
320 external_waves%out_file = io_open('bessel_source_at_point', namespace=namespace, action='write')
321 write(external_waves%out_file, '(12a) ') '# Time (a.u.) ' , ' Field-x ' , ' Field-y ' , ' Field-z '
322
323 else
324 external_waves%output_from_point= .false.
325
326 end if
327
328 ! This variable is documented in hamiltonian_mxll.F90
329 call parse_variable(namespace, 'SpeedOfLightFactor', m_one, external_waves%c_factor)
330
331 !%Variable MaxwellIncidentWaves
332 !%Type block
333 !%Section Maxwell
334 !%Description
335 !% The initial electromagnetic fields can be set by the user
336 !% with the <tt>MaxwellIncidentWaves</tt> block variable.
337 !% The electromagnetic fields have to fulfill the
338 !% Maxwells equations in vacuum.
339 !% For a Maxwell propagation, setting the electric field is sufficient,
340 !% the magnetic field (for plane waves) will be calculated from it as 1/(c.|k|) . (k x E).
341 !%
342 !% Example:
343 !%
344 !% <tt>%MaxwellIncidentWaves
345 !% <br>&nbsp;&nbsp; plane_wave_parser | "field_type"| "k1x" | "k1y" | "k1z" | "E1x" | "E1z" | "E1x"
346 !% <br>&nbsp;&nbsp; plane_wave_mx_function | "field_type"| "E4x" | "E4y" | "E4z" | mx_envelope_name | phase
347 !% <br>&nbsp;&nbsp; bessel_function | "field_type"| A_0 | m | omega | helicity | <math>\theta_{k}</math> |
348 !% mx_envelope_name | lin_dir
349 !% <br>%</tt>
350 !%
351 !% Field type can be "electric_field" or "vector_potential". Note that in order to couple to an
352 !% electronic system, the <tt>MaxwellCouplingMode</tt> variable needs to be set to a coupling type
353 !% compatible with the requested field type ("electric_field" is compatible with length gauge,
354 !% while "vector_potential" is compatible with velocity gauge and full minimal coupling).
355 !% Otherwise, the field will not be calculated or applied to the electronic Hamiltonian.
356 !%
357 !%Option plane_wave_parser 0
358 !% Parser input modus
359 !%Option plane_wave_mx_function 1
360 !% The incident wave envelope is defined by an mx_function
361 !%Option bessel_function 2
362 !% The incident source is a generalized Bessel beam, parametrized by its amplitude, opening angle, helicity,
363 !% order and frequency. This beam is a solution of Maxwell equations, and inherently circularly polarized
364 !% and is parametrized by its amplitude,opening angle, helicity, order and frequency.
365 !% Please keep in mind, if you set linear polarization lin_dir,
366 !% you will obtain a linearly polarized Bessel beam.
367 !%End
368
369 if (parse_block(namespace, 'MaxwellIncidentWaves', blk) == 0) then
370
371 call messages_print_with_emphasis(msg='Substitution of the electromagnetic incident waves', namespace=namespace)
372
373 ! find out how many lines (i.e. states) the block has
374 nlines = parse_block_n(blk)
375
376 external_waves%number = nlines
377 safe_allocate(external_waves%modus(1:nlines))
378 safe_allocate(external_waves%e_field_string(1:3, 1:nlines))
379 safe_allocate(external_waves%e_field(1:3, 1:nlines))
380 safe_allocate(external_waves%k_vector(1:3, 1:nlines))
381 safe_allocate(external_waves%v_vector(1:3, 1:nlines))
382 safe_allocate(external_waves%mx_function(1:nlines))
383 safe_allocate(external_waves%field_type(1:nlines))
384 safe_allocate(external_waves%pw_phase(1:nlines))
385 external_waves%pw_phase = m_zero
386
387 call external_waves%bessel%init(nlines, 3)
388
389 do il = 1, nlines
390 ncols = parse_block_cols(blk, il - 1)
391 if ((ncols < 5) .or. (ncols > 9)) then
392 message(1) = 'Each line in the MaxwellIncidentWaves block must have five to nine columns.'
393 call messages_fatal(1, namespace=namespace)
394 end if
395
396 ! check input modus e.g. parser of defined functions
397 call parse_block_integer(blk, il - 1, 0, external_waves%modus(il))
398 call parse_block_integer(blk, il - 1, 1, external_waves%field_type(il))
399
400 ! parse formula string
401 if (external_waves%modus(il) == option__maxwellincidentwaves__plane_wave_parser) then
402 do idim = 1, 3
403 call parse_block_string( blk, il - 1, idim + 1, k_string(idim))
404 call parse_block_string( blk, il - 1, 3 + idim + 1, external_waves%e_field_string(idim, il))
405 end do
406 write(message(1), '(a,i2,a) ') 'Substituting electromagnetic incident wave ', il, ' with the expressions: '
407 call messages_info(1, namespace=namespace)
408 do idim = 1, 3
409 write(message(idim), '(6a)') ' Wave vector k('//dims(idim)//') = ', trim(k_string(idim))
410 write(message(idim+1), '(2a)') ' E-field('//dims(idim)//') for t_0 = ', &
411 trim(external_waves%e_field_string(idim, il))
412 end do
413 call messages_info(6, namespace=namespace)
414
415 do idim = 1, 3
416 call conv_to_c_string(k_string(idim))
417 call conv_to_c_string(external_waves%e_field_string(idim, il))
418 end do
419
420 x_pos(:) = m_zero
421 x_norm = m_zero
422 do idim = 1, 3
423 call parse_expression(k_vector(idim), dummy(idim), idim, x_pos, x_norm, m_zero, k_string(idim))
424 end do
425
426 k_vector = units_to_atomic(unit_one/units_inp%length, k_vector)
427 k_norm = norm2(k_vector)
428
429 velocity(:) = k_vector(:) / k_norm * p_c * external_waves%c_factor
430 external_waves%k_vector(:,il) = k_vector(:)
431 external_waves%v_vector(:,il) = velocity(:)
432
433 else if (external_waves%modus(il) == option__maxwellincidentwaves__plane_wave_mx_function) then
434 do idim = 1, 3
435 call parse_block_cmplx( blk, il - 1, idim + 1, e_field(idim))
436 end do
437 call parse_block_string( blk, il - 1, 3 + 2, mxf_expression)
438
439 write(message(1), '(a,i2) ') 'Substituting electromagnetic incident wave ', il
440 write(message(2), '(a)' ) 'with the expression: '
441 call messages_info(2, namespace=namespace)
442
443 do idim = 1, 3
444 write(message(idim), '(a,f9.4,sp,f9.4,"i")') ' E-field('//trim(dims(idim))//') complex amplitude = ', &
445 real(e_field(idim)), aimag(e_field(idim))
446 end do
447 write(message(4), '(2a)' ) ' Maxwell wave function name = ', trim(mxf_expression)
448 call messages_info(4, namespace=namespace)
449
450 call mxf_read(external_waves%mx_function(il), namespace, trim(mxf_expression), iex_norm)
451 if (iex_norm /= 0) then
452 write(message(1),'(3A)') 'Ex_norm in the ""', trim(mxf_expression), &
453 '"" field defined in the MaxwellIncidentWaves block'
454 call messages_fatal(1, namespace=namespace)
455 end if
456 if (parse_block_cols(blk, il-1) == 7) then
457 call parse_block_float( blk, il - 1, 3 + 3 , external_waves%pw_phase(il))
458 end if
459 e_field = units_to_atomic(units_inp%energy/units_inp%length, e_field)
460 k_vector(1:3) = external_waves%mx_function(il)%k_vector(1:3)
461 k_norm = norm2(k_vector)
462
463 k_dot_e = real(dot_product(k_vector, e_field), real64)
464 if (abs(k_dot_e) > test_limit) then
465 write(message(1), '(a) ') 'The wave vector k or its electric field are not perpendicular. '
466 write(message(2), '(a,f8.3,a)' ) 'Their dot product yields the magnitude', abs(k_dot_e) , ' while'
467 write(message(3), '(a,f8.3,a)' ) 'tolerance is ', test_limit ,'.'
468 call messages_fatal(3, namespace=namespace)
469 end if
470 if (k_norm < 1e-10) then
471 message(1) = 'The k vector is not set correctly: |k|~0 .'
472 call messages_fatal(1, namespace=namespace)
473 end if
474
475 external_waves%e_field(:,il) = e_field(:)
476 external_waves%k_vector(:,il) = k_vector(:)
477 external_waves%v_vector(:,il) = k_vector(:) / k_norm * p_c * external_waves%c_factor
478
479 else if (external_waves%modus(il) == option__maxwellincidentwaves__bessel_function) then
480 call parse_block_float( blk, il - 1, 2 , external_waves%bessel%amp(il))
481 call parse_block_integer( blk, il - 1, 3 , external_waves%bessel%m_order(il))
482 call parse_block_float( blk, il - 1, 4 , external_waves%bessel%omega(il))
483 call parse_block_integer( blk, il - 1, 5 , external_waves%bessel%helicity(il))
484 call parse_block_float( blk, il - 1, 6 , external_waves%bessel%theta_k(il))
485 if (parse_block_cols(blk, il-1) > 7) then
486 call parse_block_string( blk, il - 1, 7 , mxf_expression)
487 external_waves%bessel%envelope(il) = .true.
488 call mxf_read(external_waves%mx_function(il), namespace, trim(mxf_expression), iex_norm)
489 end if
490 if (parse_block_cols(blk, il-1) == 9) then
491 call parse_block_integer( blk, il - 1, 8 , external_waves%bessel%lin_dir(il))
492 end if
493
494 write(message(1), '(a,i2) ') 'Incident Bessel Beam', il
495 call messages_info(1, namespace=namespace)
496
497 if (abs(external_waves%bessel%helicity(il)) /= 1) then
498 write(message(1),'(A)') 'Helicity has to be either +1 or -1 !'
499 call messages_fatal(1, namespace=namespace)
500 end if
501
502 write(message(1), '(a,f5.3)' ) ' Bessel Amplitude ', external_waves%bessel%amp(il)
503 write(message(2), '(a,i2)' ) ' Bessel Order m', external_waves%bessel%m_order(il)
504 write(message(3), '(a,f5.3)' ) ' Bessel Frequency ', external_waves%bessel%omega(il)
505 write(message(4), '(a,i2)' ) ' Bessel Helicity ', external_waves%bessel%helicity(il)
506 write(message(5), '(a,f5.3)' ) ' Bessel Opening Angle ', external_waves%bessel%theta_k(il)
507 call messages_info(4, namespace=namespace)
508
509 if (external_waves%bessel%lin_dir(il)/= 0) then
510 write(message(5), '(a,i2)' ) ' Bessel is Linearly Polarized in Direction : ', external_waves%bessel%lin_dir(il)
511 call messages_info(4, namespace=namespace)
512 end if
513
514 end if
515 end do
516
517 call parse_block_end(blk)
518
519 call messages_print_with_emphasis(namespace=namespace)
520 else
521 external_waves%number = 0
522
523 end if
524
525 !%Variable BesselBeamAxisShift
526 !%Type block
527 !%Section Maxwell
528 !%Description
529 !% The BesselBeamAxisShift block allows to shift the Bessel Beam, which is centered at (0,0,0) as default.
530 !% Selected position point will be used as the new center of the Bessel Beam.
531 !% When defining a BesselBeamAxisShift, please make sure to define a shift for each Bessel source you use,
532 !% then it is possible to tell which source is shifted according to which BesselShift, respectively.
533 !% <tt>%BesselBeamAxisShift
534 !% <br>&nbsp;&nbsp; 0.0 | 2.0 | 5.0
535 !% <br>%</tt>
536 !%
537 !%End
538
539 if (parse_block(namespace, 'BesselBeamAxisShift', blk) == 0) then
540 nlines = parse_block_n(blk)
541 ncols = parse_block_cols(blk,0)
542 if (ncols /= 3 ) then
543 message(1) = 'BesselBeamAxisShift must have 3 columns.'
544 call messages_fatal(1, namespace=namespace)
545 end if
546
547 do il = 1, nlines
548 do idim = 1, 3
549 call parse_block_float(blk, 0, idim-1, external_waves%bessel%shift(il, idim), units_inp%length)
550 end do
551 end do
552
553 call parse_block_end(blk)
554 end if
555
556 call profiling_out('EXTERNAL_WAVES_INIT')
557
558 pop_sub(external_waves_init)
559 end subroutine external_waves_init
560
561 ! ---------------------------------------------------------
562 subroutine external_waves_end(external_waves)
563 type(external_waves_t), intent(inout) :: external_waves
564
565 push_sub(external_waves_end)
566
567 if (external_waves%output_from_point) then
568 call io_close(external_waves%out_file)
569 safe_deallocate_a(external_waves%selected_point_coordinate)
570 safe_deallocate_a(external_waves%selected_point_field)
571 end if
572
573 safe_deallocate_a(external_waves%bessel%shift)
574 safe_deallocate_a(external_waves%points_map)
575 safe_deallocate_a(external_waves%modus)
576 safe_deallocate_a(external_waves%e_field_string)
577 safe_deallocate_a(external_waves%k_vector)
578 safe_deallocate_a(external_waves%v_vector)
579 safe_deallocate_a(external_waves%e_field)
580 safe_deallocate_a(external_waves%mx_function)
581 safe_deallocate_a(external_waves%pw_phase)
582
583 if (accel_is_enabled()) then
584 call accel_release_buffer(external_waves%buff_map)
585 end if
586
587 pop_sub(external_waves_end)
588 end subroutine external_waves_end
589
590 ! ---------------------------------------------------------
592 subroutine external_waves_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
593 type(external_waves_t), intent(inout) :: external_waves
594 real(real64), intent(in) :: time
595 class(mesh_t), intent(in) :: mesh
596 character(len=*), intent(in) :: type_of_field
597 real(real64), intent(out) :: out_field_total(:, :)
598 type(derivatives_t), optional, intent(in):: der
599
600
601 push_sub(external_waves_eval)
602
603 call profiling_in('EXTERNAL_WAVES_EVAL')
604
605 out_field_total = m_zero
606
607 call plane_waves_eval(external_waves, time, mesh, type_of_field, out_field_total, der=der)
608 call bessel_source_eval(external_waves, time, mesh, type_of_field, out_field_total, der=der)
609
610 call profiling_out('EXTERNAL_WAVES_EVAL')
611
612 pop_sub(external_waves_eval)
613 end subroutine external_waves_eval
614
615 ! ---------------------------------------------------------
617 subroutine plane_waves_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
618 type(external_waves_t), intent(inout) :: external_waves
619 real(real64), intent(in) :: time
620 class(mesh_t), intent(in) :: mesh
621 character(len=*), intent(in) :: type_of_field
622 real(real64), intent(out) :: out_field_total(:, :)
623 type(derivatives_t), optional, intent(in):: der
624
625 integer :: wn
626 real(real64), allocatable :: pw_field(:,:), ztmp(:,:), b_field_aux(:,:)
627 real(real64) :: p_c_
628 integer, allocatable :: indices_pw_parser(:)
629 integer, allocatable :: indices_mx_ftc(:)
630 integer :: n_plane_waves, n_points
631
632 push_sub(plane_waves_eval)
633
634 call profiling_in('PLANE_WAVES_EVAL')
635
636 indices_pw_parser = pack([(wn, wn = 1,external_waves%number)], &
637 external_waves%modus == option__maxwellincidentwaves__plane_wave_parser)
638
639 indices_mx_ftc = pack([(wn, wn = 1,external_waves%number)], &
640 external_waves%modus == option__maxwellincidentwaves__plane_wave_mx_function)
641
642 n_plane_waves = size(indices_pw_parser) + size(indices_mx_ftc)
643
644 p_c_ = p_c * external_waves%c_factor
645
646 if (n_plane_waves == 0) then
647 call profiling_out('PLANE_WAVES_EVAL')
648 pop_sub(plane_waves_eval)
649 return
650 end if
651
652 if (type_of_field == "B field" .and. any(external_waves%field_type == e_field_vector_potential)) then
653 assert(present(der))
654 safe_allocate(ztmp(mesh%np, size(out_field_total, dim=2)))
655 n_points = mesh%np_part
656 else
657 n_points = mesh%np
658 end if
659 safe_allocate(pw_field(n_points, size(out_field_total, dim=2)))
660 pw_field(:,:) = m_zero
661
662 ! The E_field (or A_field, rescaled later) we calculate always
663 do wn = 1, external_waves%number
664
665 select case(external_waves%modus(wn))
666 case (option__maxwellincidentwaves__plane_wave_parser)
667 call pw_parsed_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
668
669 case (option__maxwellincidentwaves__plane_wave_mx_function)
670 call pw_mx_function_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
671 end select
672
673 select case (external_waves%field_type(wn))
674
675 case(e_field_electric)
676
677 select case (type_of_field)
678 case ("E field")
679 out_field_total(1:mesh%np,:) = out_field_total(1:mesh%np,:) + pw_field(1:mesh%np,:)
680 case ("vector potential")
681 call messages_not_implemented("Calculation of a vector potential from a plane wave specified as electric field")
682 case ("B field")
683 safe_allocate(b_field_aux(1:mesh%np, 1:mesh%box%dim))
684 call get_pw_b_field(external_waves, mesh, wn, pw_field, b_field_aux)
685 out_field_total(:,:) = out_field_total(:,:) + b_field_aux(:,:)
686 safe_deallocate_a(b_field_aux)
687 end select
688
690
691 select case (type_of_field)
692 case ("E field")
693 call messages_not_implemented("Calculation of an electric field from a plane wave specified as vector potential")
694 case ("vector potential")
695 out_field_total(1:mesh%np,:) = out_field_total(1:mesh%np,:) - m_one/p_c_ * pw_field(1:mesh%np,1:3)
696 case ("B field")
697 call dderivatives_curl(der, pw_field(1:mesh%np_part,1:3), ztmp(1:mesh%np,1:3), set_bc = .false.)
698 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) - m_one/p_c_ * ztmp(1:mesh%np, 1:3)
699 end select
700
701 end select
702 end do
703
704 safe_deallocate_a(pw_field)
705 safe_deallocate_a(ztmp)
706 call profiling_out('PLANE_WAVES_EVAL')
707
708 pop_sub(plane_waves_eval)
709
710 end subroutine plane_waves_eval
711
712 ! ---------------------------------------------------------
714 subroutine pw_parsed_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
715 type(external_waves_t), intent(inout) :: external_waves
716 integer, intent(in) :: wn
717 real(real64), intent(in) :: time
718 class(mesh_t), intent(in) :: mesh
719 integer, intent(in) :: n_points
720 real(real64), intent(out) :: pw_field(:,:)
721
722 real(real64) :: x_prop(3), x_norm
723 real(real64) :: velocity_time(3)
724 real(real64) :: parsed_field(3)
725 real(real64) :: dummy(3)
726 integer :: idim, ip
727
728 velocity_time(:) = external_waves%v_vector(1:3, wn) * time
729 do idim = 1, 3
730 call parse_expression(parsed_field(idim), dummy(idim), 3, x_prop, x_norm, m_zero, &
731 external_waves%e_field_string(idim, wn))
732 do ip = 1, n_points
733 x_prop = mesh%x(ip, :) - velocity_time
734 x_norm = norm2(x_prop(1:3))
735 pw_field(ip, idim) = units_to_atomic(units_inp%energy/units_inp%length, parsed_field(idim))
736 end do
737 end do
738
739 end subroutine pw_parsed_evaluation
740
741 ! ---------------------------------------------------------
743 subroutine pw_mx_function_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
744 type(external_waves_t), intent(inout) :: external_waves
745 integer, intent(in) :: wn
746 real(real64), intent(in) :: time
747 class(mesh_t), intent(in) :: mesh
748 integer, intent(in) :: n_points
749 real(real64), intent(out) :: pw_field(:,:)
750
751 real(real64) :: x_prop(3), x_norm
752 real(real64) :: velocity_time(3)
753 complex(real64) :: efield_ip(3)
754 complex(real64) :: e0(3)
755 integer :: ip
756
757 velocity_time(:) = external_waves%v_vector(1:3, wn) * time
758 e0(:) = external_waves%e_field(1:3, wn)
759 do ip = 1, n_points
760 x_prop = mesh%x(ip, :) - velocity_time
761 x_norm = norm2(x_prop(1:3))
762 efield_ip = mxf(external_waves%mx_function(wn), x_prop, external_waves%pw_phase(wn))
763 pw_field(ip, :) = real(e0(1:3) * efield_ip, real64)
764 end do
765
766 end subroutine pw_mx_function_evaluation
767
768 ! ---------------------------------------------------------
770 subroutine get_pw_b_field(external_waves, mesh, pwidx, e_field, b_field)
771 type(external_waves_t), intent(in) :: external_waves
772 class(mesh_t), intent(in) :: mesh
773 real(real64), intent(in) :: e_field(:,:)
774 real(real64), intent(out) :: b_field(:,:)
775 integer, intent(in) :: pwidx
776
777 real(real64) :: k_vector(3), k_vector_abs
778 real(real64) :: velocity(3)
779 real(real64) :: P_c_
780 complex(real64) :: e0(3)
781 integer :: ip
782
783 velocity = external_waves%v_vector(1:3, pwidx)
784 k_vector = external_waves%k_vector(1:3, pwidx)
785 k_vector_abs = norm2(k_vector(1:3))
786 e0 = external_waves%e_field(1:3, pwidx)
787 p_c_ = p_c * external_waves%c_factor
788
789 b_field = m_zero
790 do ip = 1, mesh%np
791 b_field(ip, :) = m_one/(p_c_ * k_vector_abs) * dcross_product(k_vector, e_field(ip, :))
792 end do
793
794 end subroutine get_pw_b_field
795
796 ! ---------------------------------------------------------
798 subroutine bessel_source_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
799 type(external_waves_t), intent(inout) :: external_waves
800 real(real64), intent(in) :: time
801 class(mesh_t), intent(in) :: mesh
802 character(len=*), intent(in) :: type_of_field
803 real(real64), intent(out) :: out_field_total(:, :)
804 type(derivatives_t), optional, intent(in):: der
805
806 real(real64) :: dmin, omega, k_vector(3), c_factor
807 integer :: iline, wn, pos_index, n_points, rankmin
808 real(real64), allocatable :: shift(:,:)
809 complex(real64), allocatable :: bessel_field_total(:,:), ztmp(:,:), vec_pot(:,:)
810 integer, allocatable :: indices_bessel_ftc(:)
811 type(mxf_t) :: envelope_mxf
812
813 push_sub(bessel_source_eval)
814
815 call profiling_in('BESSEL_SOURCE_EVAL')
816
817 indices_bessel_ftc = pack([(wn, wn = 1,external_waves%number)], &
818 external_waves%modus == option__maxwellincidentwaves__bessel_function)
819
820 if (size(indices_bessel_ftc) == 0) then
821 call profiling_out('BESSEL_SOURCE_EVAL')
822 pop_sub(bessel_source_eval)
823 return
824 end if
825
826 ! Check if the BesselBeamAxisShift is defined for every incoming Bessel Beam.
827 if (allocated(external_waves%bessel%shift) .and. &
828 size(external_waves%bessel%shift(:,1)) /= size(indices_bessel_ftc)) then
829 message(1) = 'Number of BesselBeamAxisShift defined in input file'
830 message(2) = 'does not match the number of Bessel beams.'
831 call messages_fatal(2)
832 end if
833
834 safe_allocate(shift(size(indices_bessel_ftc), 3))
835 if (allocated(external_waves%bessel%shift)) then
836 shift = external_waves%bessel%shift
837 else
838 shift = m_zero
839 end if
840
841 if (type_of_field == "B field") then
842 assert(present(der))
843 safe_allocate(vec_pot(mesh%np_part, size(out_field_total, dim=2)))
844 safe_allocate(ztmp(size(out_field_total, dim=1), size(out_field_total, dim=2)))
845 n_points = mesh%np_part ! needed for curl
846 else
847 n_points = mesh%np
848 end if
849
850
851 safe_allocate(bessel_field_total(1:n_points, 1:3))
852 bessel_field_total = m_zero
853
854 do iline = 1, size(indices_bessel_ftc)
855 wn = indices_bessel_ftc(iline)
856 omega = external_waves%bessel%omega(wn)
857 k_vector = external_waves%mx_function(wn)%k_vector
858 c_factor = external_waves%c_factor
859 envelope_mxf = external_waves%mx_function(wn)
860
861 call external_waves%bessel%function(wn, shift, mesh, n_points, time, k_vector, c_factor, envelope_mxf, bessel_field_total)
862
863 select case (external_waves%field_type(wn))
864
866 ! interpreting bessel_field_total as a vector potential (as requested by the user)
867 select case (type_of_field)
868 case ("E field")
869 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) + real(m_zi*omega*bessel_field_total(1:mesh%np,1:3))
870 case ("vector potential")
871 ! For the vector potential, we multiply by -1/c becuase of the electronic Hamiltonian
872 ! being in Gaussian units
873 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) - m_one/p_c * real(bessel_field_total(1:mesh%np,1:3))
874 case ("B field")
875 call zderivatives_curl(der, bessel_field_total(1:mesh%np_part,1:3), ztmp(1:mesh%np,1:3), set_bc = .false.)
876 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) - m_one/p_c * real(ztmp(1:mesh%np, 1:3))
877 end select
878
879 case(e_field_electric)
880 ! interpreting bessel_field_total as an electric field (as requested by the user)
881 select case (type_of_field)
882 case ("E field")
883 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) + real(bessel_field_total(1:mesh%np,1:3))
884 case ("vector potential")
885 ! We calculate the vector potential as real(E/i*omega),
886 ! and convert it to the proper units by multiplying by -1/c
887 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) - m_one/p_c * &
888 real(bessel_field_total(1:mesh%np,1:3)/M_zI/omega)
889 case ("B field")
890 vec_pot(1:mesh%np_part,1:3) = - m_one/p_c * real(bessel_field_total(1:mesh%np_part,1:3)/m_zi/omega)
891 call zderivatives_curl(der, vec_pot(1:mesh%np_part,1:3), ztmp(1:mesh%np,1:3), set_bc = .false.)
892 out_field_total(1:mesh%np,1:3) = out_field_total(1:mesh%np,1:3) - real(ztmp(1:mesh%np, 1:3))
893 end select
894
895 end select
896 end do
897
898 if (external_waves%output_from_point) then
899 pos_index = mesh_nearest_point(mesh, external_waves%selected_point_coordinate(1:3), dmin, rankmin)
900 if (mesh%mpi_grp%rank == rankmin) then
901 external_waves%selected_point_field(:) = out_field_total(pos_index,:)
902 write(external_waves%out_file, "(4F14.8, 4x)") time, external_waves%selected_point_field(:)
903 end if
904 end if
905
906 safe_deallocate_a(shift)
907 safe_deallocate_a(ztmp)
908 safe_deallocate_a(vec_pot)
909 safe_deallocate_a(bessel_field_total)
910 call profiling_out('BESSEL_SOURCE_EVAL')
911
912 pop_sub(bessel_source_eval)
913
914 end subroutine bessel_source_eval
915
916 ! ---------------------------------------------------------
918 subroutine bessel_beam_function(this, iline, shift, mesh, n_points, time, k_vector, c_factor, envelope_mxf, bessel_field)
919 class(bessel_beam_t) :: this
920 integer, intent(in) :: iline
921 real(real64), intent(in) :: shift(:,:), time, k_vector(3), c_factor
922 class(mesh_t), intent(in) :: mesh
923 integer, intent(in) :: n_points
924 type(mxf_t), intent(in) :: envelope_mxf
925 complex(real64), intent(out) :: bessel_field(:,:)
926
927 real(real64) :: pos(3), temp, temp2, temp3, rho, phi_rho, wigner(3)
928 real(real64) :: hel, theta, omega, amp, kappa, proj, k_norm, velocity_time(3), x_prop(3)
929 complex(real64) :: efield_ip(3)
930 real(real64) :: bessel_plus, bessel_minus
931 integer :: ip, mm, pol
932
933 assert(iline <= size(this%omega))
934 hel = real(this%helicity(iline), real64)
935 theta = this%theta_k(iline)
936 mm = this%m_order(iline)
937 amp = this%amp(iline) / sqrt(m_two)
938 omega = this%omega(iline)
939 proj = omega * cos(theta) / p_c ! k_z
940 kappa = sqrt(omega**2 - (proj* p_c)**2) ! parse omega
941 ! Set Wigner Coefficients from theta
942 wigner(1) = hel * sin(theta) / sqrt(m_two) ! mu = 0
943 wigner(2) = 0.5 * (1 + hel * cos(theta)) ! mu = 1
944 wigner(3) = 0.5 * (1 - hel * cos(theta)) ! mu = -1
945 proj = omega * cos(theta) / p_c ! k_z
946 pol = this%lin_dir(iline) ! Incoming polarization corresponding to beam in question
947
948 do ip = 1, n_points
949 pos(:) = mesh%x(ip, :) - shift(iline,:)
950 rho = norm2(pos(1:2))
951 phi_rho = atan2(pos(2) , pos(1))
952 temp = proj * pos(3) + phi_rho * (mm + 1) - omega*time ! temp, temp2 and temp3 should be unitless
953 temp2 = proj * pos(3) + phi_rho * (mm - 1) - omega*time
954 temp3 = proj * pos(3) + phi_rho * mm - omega*time
955 bessel_plus = loct_bessel(mm+1, kappa * rho / p_c)
956 bessel_minus = loct_bessel(mm-1, kappa * rho / p_c)
957
958 ! Calculate complex Ax component, if generalized bessel OR x -polarized bessel
959 if (pol /= 2) then
960 bessel_field(ip, 1) = amp * (exp(m_zi*temp) * wigner(3) * bessel_plus + exp(m_zi*temp2) * wigner(2) * bessel_minus)
961 end if
962 ! Calculate complex Ay component if generalized bessel OR y -polarized bessel
963 if (pol/=1) then
964 bessel_field(ip, 2) = m_zi * amp * (-exp(m_zi*temp) * wigner(3) * bessel_plus + &
965 exp(m_zi*temp2) * wigner(2) * bessel_minus)
966 end if
967 ! Calculate complex Az component, only iff generalized Bessel
968 if (pol == 0) then
969 bessel_field(ip, 3) = - m_zi * amp * sqrt(m_two) * wigner(1) * loct_bessel(mm, kappa * rho / p_c) * exp(m_zi*temp3)
970 end if
971
972 if (this%envelope(iline)) then
973 k_norm = norm2(k_vector)
974 velocity_time = k_vector * p_c * c_factor * time / k_norm
975 x_prop(:) = pos(:) - velocity_time(:)
976 efield_ip = mxf_envelope_eval(envelope_mxf, x_prop)
977 bessel_field(ip, :) = bessel_field(ip, :) * real(efield_ip, real64)
978 end if
979
980 end do
981
982 end subroutine bessel_beam_function
983
985 subroutine bessel_beam_init(this, nlines, dim)
986 class(bessel_beam_t), intent(out) :: this
987 integer, intent(in) :: nlines
988 integer, intent(in) :: dim
989
990 safe_allocate(this%amp(1: nlines))
991 safe_allocate(this%omega(1:nlines))
992 safe_allocate(this%theta_k(1:nlines))
993 safe_allocate(this%m_order(1:nlines))
994 safe_allocate(this%helicity(1:nlines))
995 safe_allocate(this%shift(1:nlines, 1:dim))
996 safe_allocate(this%envelope(1:nlines))
997 safe_allocate(this%lin_dir(1:nlines))
998 this%amp = m_zero
999 this%omega = m_zero
1000 this%theta_k = m_zero
1001 this%m_order = m_zero
1002 this%helicity = m_zero
1003 this%shift = m_zero
1004 this%lin_dir = m_zero
1005 this%envelope = .false.
1006
1007 end subroutine bessel_beam_init
1008
1010 subroutine bessel_beam_finalize(this)
1011 type(bessel_beam_t), intent(inout) :: this
1012
1013 safe_deallocate_a(this%amp)
1014 safe_deallocate_a(this%omega)
1015 safe_deallocate_a(this%theta_k)
1016 safe_deallocate_a(this%m_order)
1017 safe_deallocate_a(this%helicity)
1018 safe_deallocate_a(this%shift)
1019 safe_deallocate_a(this%lin_dir)
1020 safe_deallocate_a(this%envelope)
1021
1022 end subroutine bessel_beam_finalize
1023
1024end module external_waves_oct_m
1025
1026!! Local Variables:
1027!! mode: f90
1028!! coding: utf-8
1029!! End:
double exp(double __x) __attribute__((__nothrow__
double sin(double __x) __attribute__((__nothrow__
double cos(double __x) __attribute__((__nothrow__
double atan2(double __y, double __x) __attribute__((__nothrow__
subroutine, public accel_release_buffer(this)
Definition: accel.F90:1250
pure logical function, public accel_is_enabled()
Definition: accel.F90:402
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public zderivatives_curl(der, ff, op_ff, ghost_update, set_bc)
apply the curl operator to a vector of mesh functions
subroutine, public dderivatives_curl(der, ff, op_ff, ghost_update, set_bc)
apply the curl operator to a vector of mesh functions
subroutine, public load_external_waves(partners, namespace)
subroutine bessel_beam_function(this, iline, shift, mesh, n_points, time, k_vector, c_factor, envelope_mxf, bessel_field)
. Evaluation of the Bessel beam expression
subroutine, public bessel_source_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
Calculation of Bessel beam from parsed formula.
subroutine external_waves_update_quantity(this, label)
subroutine pw_mx_function_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
Evaluate expression for plane wave that uses predefeined Maxwell function.
subroutine bessel_beam_init(this, nlines, dim)
. Initialization of Bessel beam arrays
subroutine external_waves_copy_quantities_to_interaction(partner, interaction)
subroutine, public external_waves_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
Calculation of external waves from parsed formula.
class(external_waves_t) function, pointer external_waves_constructor(namespace)
subroutine, public external_waves_end(external_waves)
subroutine pw_parsed_evaluation(external_waves, wn, time, mesh, n_points, pw_field)
Evaluate expression for plane wave parsing the provided formula.
subroutine plane_waves_eval(external_waves, time, mesh, type_of_field, out_field_total, der)
Calculation of plane waves from parsed formula.
subroutine get_pw_b_field(external_waves, mesh, pwidx, e_field, b_field)
Calculation of magnetic field for a plane wave.
subroutine external_waves_init_interaction_as_partner(partner, interaction)
subroutine, public external_waves_init(external_waves, namespace)
Here, plane wave is evaluated from analytical formulae on grid.
subroutine bessel_beam_finalize(this)
. Finalize Bessel beam arrays
real(real64), parameter, public m_two
Definition: global.F90:190
real(real64), parameter, public m_zero
Definition: global.F90:188
complex(real64), parameter, public m_zi
Definition: global.F90:202
real(real64), parameter, public p_c
Electron gyromagnetic ratio, see Phys. Rev. Lett. 130, 071801 (2023)
Definition: global.F90:224
real(real64), parameter, public m_one
Definition: global.F90:189
This module implements the underlying real-space grid.
Definition: grid.F90:117
This module implements the index, used for the mesh points.
Definition: index.F90:122
integer, parameter, public mxll_vec_pot_to_matter
integer, parameter, public mxll_b_field_to_matter
integer, parameter, public mxll_e_field_to_matter
This module defines the abstract interaction_t class, and some auxiliary classes for interactions.
This module defines classes and functions for interaction partners.
Definition: io.F90:114
subroutine, public io_close(iunit, grp)
Definition: io.F90:418
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:311
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:352
integer, parameter, public e_field_electric
Definition: lasers.F90:177
integer, parameter, public e_field_vector_potential
Definition: lasers.F90:177
complex(real64) function mxf_envelope_eval(f, x)
Evaluation of envelope itself.
subroutine, public mxf_read(f, namespace, function_name, ierr)
This function initializes "f" from the MXFunctions block.
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
integer function, public mesh_nearest_point(mesh, pos, dmin, rankmin)
Returns the index of the point which is nearest to a given vector position pos.
Definition: mesh.F90:380
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:920
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1113
character(len=512), private msg
Definition: messages.F90:165
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:537
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:414
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:616
Some general things and nomenclature:
Definition: par_vec.F90:171
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:618
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:623
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:552
This module defines the quantity_t class and the IDs for quantities, which can be exposed by a system...
Definition: quantity.F90:138
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:252
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:132
This module defines the unit system, used for input and output.
type(unit_system_t), public units_inp
the units systems for reading and writing
type(unit_t), public unit_one
some special units required for particular quantities
class representing derivatives
abstract class for general interaction partners
surrogate interaction class to avoid circular dependencies between modules.
Lorenz force between a systems of particles and an electromagnetic field.
Describes mesh distribution to nodes.
Definition: mesh.F90:186
class to transfer a Maxwell B field to a matter system
class to transfer a Maxwell field to a medium
class to transfer a Maxwell vector potential to a medium
Systems (system_t) can expose quantities that can be used to calculate interactions with other system...
Definition: quantity.F90:171
int true(void)