From 00663d3e6291976a1274b2a2c54a0171e257f2c5 Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Tue, 21 Jun 2022 16:29:05 -0700 Subject: [PATCH 1/2] fix(move_tcells): use findloc to set speed --- app/main.f90 | 2 +- src/tcelsim/move_m.f90 | 3 +-- src/tcelsim/move_s.f90 | 59 ++++++++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/app/main.f90 b/app/main.f90 index dcc73f7..656f6e3 100644 --- a/app/main.f90 +++ b/app/main.f90 @@ -29,6 +29,6 @@ program tcell call random_number(random_number_table) - call move_tcells(x,y,z,vel,cumulative_distribution,random_number_table,ncells,npositions,nintervals) + call move_tcells(x,y,z,vel,cumulative_distribution,random_number_table) end program diff --git a/src/tcelsim/move_m.f90 b/src/tcelsim/move_m.f90 index 018dbc0..a577240 100644 --- a/src/tcelsim/move_m.f90 +++ b/src/tcelsim/move_m.f90 @@ -3,9 +3,8 @@ module move_m interface - module subroutine move_tcells(x,y,z,vel,cumulative_distribution,random_number_table,ncells,npositions,nintervals) + module subroutine move_tcells(x,y,z,vel,cumulative_distribution,random_number_table) implicit none - integer, intent(in) :: ncells,npositions,nintervals double precision, intent(in) :: random_number_table(:,:,:) double precision, intent(inout) :: x(:,:) double precision, intent(inout) :: y(:,:) diff --git a/src/tcelsim/move_s.f90 b/src/tcelsim/move_s.f90 index c09d434..d475c72 100644 --- a/src/tcelsim/move_s.f90 +++ b/src/tcelsim/move_s.f90 @@ -5,40 +5,43 @@ module procedure move_tcells ! Local variables - integer i,j,k - double precision dt - double precision, dimension(ncells,npositions-1) :: speed,rr2,rr3,rr4,sum - - ! Time step - dt = .1 + integer i, j + double precision, allocatable, dimension(:,:) :: speed - rr2(:,:) = random_number_table(:,2:,2) - rr3(:,:) = random_number_table(:,2:,3) - rr4(:,:) = random_number_table(:,2:,4) + associate(ncells => size(random_number_table,1), npositions => size(random_number_table,2) ) + allocate(speed(ncells, npositions-1)) - do i = 1,ncells - do j = 2,npositions - ! Sample from the distribution - do k = 1,nintervals - if (random_number_table(i,j,1) .ge. cumulative_distribution(k) .and. & - random_number_table(i,j,1) .lt. cumulative_distribution(k+1)) then - speed(i,j) = vel(k) - end if - end do + ! Sample from the distribution + do concurrent(i = 1:ncells, j = 1:npositions-1) + associate(k => findloc(random_number_table(i,j,1) >= cumulative_distribution, value=.true., dim=1)) + speed(i,j) = vel(k) + end associate end do - end do - ! Create a random unit vector + block + ! Time step + double precision, allocatable, dimension(:,:,:) :: dir + double precision, parameter :: dt = .1 + + ! Create a random unit vector + dir = random_number_table(:, 1:npositions-1, 2:4) - sum(:,:) = rr2(:,:) + rr3(:,:) + rr4(:,:) - rr2(:,:) = rr2(:,:)/sum(:,:) - rr3(:,:) = rr3(:,:)/sum(:,:) - rr4(:,:) = rr4(:,:)/sum(:,:) + associate(dir_mag => sqrt(dir(:,:,1)**2 +dir(:,:,2)**2 + dir(:,:,3)**2)) + associate(dir_mag_ => merge(dir_mag, epsilon(dir_mag), dir_mag/=0.)) + dir(:,:,1) = dir(:,:,1)/dir_mag_ + dir(:,:,2) = dir(:,:,2)/dir_mag_ + dir(:,:,3) = dir(:,:,3)/dir_mag_ + end associate + end associate - ! Use a forward Euler to advance the cell position - x(:,2:) = x(:,:) + dt*speed(:,:)*rr2(:,:) - y(:,2:) = y(:,:) + dt*speed(:,:)*rr3(:,:) - z(:,2:) = z(:,:) + dt*speed(:,:)*rr4(:,:) + ! Use a forward Euler to advance the cell position + do i=2,npositions + x(:,i) = x(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,1) + y(:,i) = y(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,2) + z(:,i) = z(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,3) + end do + end block + end associate end procedure move_tcells From bb0595fae62f09918b8260c403011ef44c6e94e3 Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Tue, 21 Jun 2022 17:06:15 -0700 Subject: [PATCH 2/2] refac(create_distribution): calculate nintervals --- app/main.f90 | 6 ++++-- src/tcelsim/distribution_m.f90 | 3 +-- src/tcelsim/distribution_s.f90 | 33 +++++++++------------------------ src/tcelsim/move_s.f90 | 16 ++++++++-------- src/tcelsim/t_cell_s.f90 | 3 +-- 5 files changed, 23 insertions(+), 38 deletions(-) diff --git a/app/main.f90 b/app/main.f90 index 656f6e3..7e41326 100644 --- a/app/main.f90 +++ b/app/main.f90 @@ -15,7 +15,9 @@ program tcell double precision, allocatable :: y(:,:) double precision, allocatable :: z(:,:) - allocate(random_number_table(ncells,npositions,nveldim)) + associate(nsteps => npositions-1) + allocate(random_number_table(ncells,nsteps,nveldim)) + end associate allocate(random_positions(ncells,ndim)) allocate(x(ncells,npositions)) allocate(y(ncells,npositions)) @@ -25,7 +27,7 @@ program tcell call initialize_positions(x(:,1),y(:,1),z(:,1),random_positions) - call create_distribution(vel,cumulative_distribution,nintervals) + call create_distribution(vel,cumulative_distribution) call random_number(random_number_table) diff --git a/src/tcelsim/distribution_m.f90 b/src/tcelsim/distribution_m.f90 index 6f3de77..8729242 100644 --- a/src/tcelsim/distribution_m.f90 +++ b/src/tcelsim/distribution_m.f90 @@ -4,10 +4,9 @@ module distribution_m interface - module subroutine create_distribution(vel,cumulative_distribution,nintervals) + module subroutine create_distribution(vel,cumulative_distribution) implicit none - integer, intent(in) :: nintervals double precision, intent(out) :: cumulative_distribution(:) double precision, intent(out) :: vel(:) diff --git a/src/tcelsim/distribution_s.f90 b/src/tcelsim/distribution_s.f90 index 21cc7f7..880303c 100644 --- a/src/tcelsim/distribution_s.f90 +++ b/src/tcelsim/distribution_s.f90 @@ -8,31 +8,16 @@ ! Local variables integer i - double precision sum,rr1 double precision, allocatable :: sample_distribution(:) - - allocate(sample_distribution(nintervals)) - - ! Create a distribution - sum = 0.d0 - do i = 1,nintervals - call random_number(rr1) - sample_distribution(i) = rr1 - sum = sum + sample_distribution(i) - end do - - do i = 1,nintervals - sample_distribution(i) = sample_distribution(i)/sum - ! Assign speeds to each distribution bin - vel(i) = dble(i) - end do - - ! Form the cumulative distribution - cumulative_distribution(1) = 0.d0 - do i = 2,nintervals+1 - cumulative_distribution(i) = cumulative_distribution(i-1) + & - sample_distribution(i-1) - end do + + ! Create a distribution + associate(nintervals => size(cumulative_distribution,1)-1) + allocate(sample_distribution(nintervals)) + call random_number(sample_distribution) + sample_distribution = sample_distribution/ sum(sample_distribution) + vel = [(dble(i), i =1, nintervals)] ! Assign speeds to each distribution bin + cumulative_distribution = [(0.D0, sum(sample_distribution(1:i)), i=1,nintervals)] ! Form the cumulative distribution + end associate end procedure create_distribution diff --git a/src/tcelsim/move_s.f90 b/src/tcelsim/move_s.f90 index d475c72..9faa8b0 100644 --- a/src/tcelsim/move_s.f90 +++ b/src/tcelsim/move_s.f90 @@ -8,11 +8,11 @@ integer i, j double precision, allocatable, dimension(:,:) :: speed - associate(ncells => size(random_number_table,1), npositions => size(random_number_table,2) ) - allocate(speed(ncells, npositions-1)) + associate(ncells => size(random_number_table,1), nsteps => size(random_number_table,2) ) + allocate(speed(ncells, nsteps)) ! Sample from the distribution - do concurrent(i = 1:ncells, j = 1:npositions-1) + do concurrent(i = 1:ncells, j = 1:nsteps) associate(k => findloc(random_number_table(i,j,1) >= cumulative_distribution, value=.true., dim=1)) speed(i,j) = vel(k) end associate @@ -24,7 +24,7 @@ double precision, parameter :: dt = .1 ! Create a random unit vector - dir = random_number_table(:, 1:npositions-1, 2:4) + dir = random_number_table(:, 1:nsteps, 2:4) associate(dir_mag => sqrt(dir(:,:,1)**2 +dir(:,:,2)**2 + dir(:,:,3)**2)) associate(dir_mag_ => merge(dir_mag, epsilon(dir_mag), dir_mag/=0.)) @@ -35,10 +35,10 @@ end associate ! Use a forward Euler to advance the cell position - do i=2,npositions - x(:,i) = x(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,1) - y(:,i) = y(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,2) - z(:,i) = z(:,i-1) + dt*speed(:,i-1)*dir(:,i-1,3) + do i=1,nsteps + x(:,i+1) = x(:,i) + dt*speed(:,i)*dir(:,i,1) + y(:,i+1) = y(:,i) + dt*speed(:,i)*dir(:,i,2) + z(:,i+1) = z(:,i) + dt*speed(:,i)*dir(:,i,3) end do end block end associate diff --git a/src/tcelsim/t_cell_s.f90 b/src/tcelsim/t_cell_s.f90 index 7520c6e..d3796db 100644 --- a/src/tcelsim/t_cell_s.f90 +++ b/src/tcelsim/t_cell_s.f90 @@ -5,10 +5,9 @@ module procedure initialize_positions - ! Local variables double precision, parameter :: scaling_factor = 100. - ! Assign initial positions to T cells randomly in a [100x100x100] grid + ! Assign initial positions to T cells randomly in a cube with sides of length scaling_factor x = random_positions(:,1)*scaling_factor y = random_positions(:,2)*scaling_factor z = random_positions(:,3)*scaling_factor