init_particles_from_config Subroutine

public subroutine init_particles_from_config(cfg, pcls)

設定全体ぶんの粒子群を生成し、SoA へ詰める。 粒子種ごとに乱数サンプルした後、種ごとに rank を揃えて interleave する。

Arguments

Type IntentOptional Attributes Name
type(app_config), intent(in) :: cfg

粒子種設定を含むアプリ設定。

type(particles_soa), intent(out) :: pcls

生成した全粒子群。


Calls

proc~~init_particles_from_config~~CallsGraph proc~init_particles_from_config init_particles_from_config proc~init_particles init_particles proc~init_particles_from_config->proc~init_particles proc~lower_ascii lower_ascii proc~init_particles_from_config->proc~lower_ascii proc~sample_species_state sample_species_state proc~init_particles_from_config->proc~sample_species_state proc~seed_rng seed_rng proc~init_particles_from_config->proc~seed_rng proc~total_particles_from_config total_particles_from_config proc~init_particles_from_config->proc~total_particles_from_config proc~sample_species_state->proc~lower_ascii proc~sample_reservoir_face_particles sample_reservoir_face_particles proc~sample_species_state->proc~sample_reservoir_face_particles proc~sample_shifted_maxwell_velocities sample_shifted_maxwell_velocities proc~sample_species_state->proc~sample_shifted_maxwell_velocities proc~sample_uniform_positions sample_uniform_positions proc~sample_species_state->proc~sample_uniform_positions proc~species_temperature_k~2 species_temperature_k proc~sample_species_state->proc~species_temperature_k~2 proc~particles_per_batch_from_config particles_per_batch_from_config proc~total_particles_from_config->proc~particles_per_batch_from_config proc~sample_reservoir_face_particles->proc~sample_shifted_maxwell_velocities

Source Code

  subroutine init_particles_from_config(cfg, pcls)
    type(app_config), intent(in) :: cfg
    type(particles_soa), intent(out) :: pcls

    integer(i32) :: s, total_n, max_n, out_idx, rank_idx
    integer(i32), allocatable :: counts(:)
    real(dp), allocatable :: x_species(:, :, :), v_species(:, :, :)
    real(dp), allocatable :: q(:), m(:), w(:), x(:, :), v(:, :)

    if (cfg%n_particle_species <= 0) error stop 'At least one [[particles.species]] entry is required.'

    call seed_rng([cfg%sim%rng_seed])

    allocate (counts(cfg%n_particle_species))
    counts = 0_i32
    do s = 1, cfg%n_particle_species
      if (cfg%particle_species(s)%enabled) then
        if (trim(lower_ascii(cfg%particle_species(s)%source_mode)) == 'reservoir_face' .or. &
            trim(lower_ascii(cfg%particle_species(s)%source_mode)) == 'photo_raycast') then
          error stop 'init_particles_from_config supports volume_seed only. Use init_particle_batch_from_config.'
        end if
        if (cfg%particle_species(s)%npcls_per_step < 0_i32) then
          error stop 'particles.species.npcls_per_step must be >= 0.'
        end if
        counts(s) = cfg%sim%batch_count*cfg%particle_species(s)%npcls_per_step
      end if
    end do

    total_n = total_particles_from_config(cfg)
    max_n = max(1_i32, maxval(counts))

    allocate (x_species(3, max_n, cfg%n_particle_species))
    allocate (v_species(3, max_n, cfg%n_particle_species))
    x_species = 0.0d0
    v_species = 0.0d0

    do s = 1, cfg%n_particle_species
      if (counts(s) <= 0_i32) cycle
      call sample_species_state( &
        cfg%sim, cfg%particle_species(s), counts(s), x_species(:, 1:counts(s), s), v_species(:, 1:counts(s), s) &
        )
    end do

    allocate (x(3, total_n), v(3, total_n), q(total_n), m(total_n), w(total_n))
    out_idx = 0_i32
    do rank_idx = 1, maxval(counts)
      do s = 1, cfg%n_particle_species
        if (rank_idx > counts(s)) cycle
        out_idx = out_idx + 1_i32
        x(:, out_idx) = x_species(:, rank_idx, s)
        v(:, out_idx) = v_species(:, rank_idx, s)
        q(out_idx) = cfg%particle_species(s)%q_particle
        m(out_idx) = cfg%particle_species(s)%m_particle
        w(out_idx) = cfg%particle_species(s)%w_particle
      end do
    end do

    call init_particles(pcls, x, v, q, m, w)
  end subroutine init_particles_from_config