RFOF - RF Library for Orbit Following Codes
/Users/thomas/codes/rfof/trunk/testbed/dummy_orbit.F90
Go to the documentation of this file.
00001 
00011 
00012 #include "../src/config.h"
00013 
00014 module dummy_orbit
00015 
00016 #ifdef USE_ISO_C_BINDING
00017   use, intrinsic :: ISO_C_BINDING
00018 #endif
00019 
00020   use dum_magnetic_field
00021   use RFOF_magnetic_field
00022   use RFOF_local_magnetic_field, only: get_local_magnetic_field
00023   use RFOF_waves
00024   use RFOF_resonance_memory
00025   use RFOF_markers
00026   use RFOF_main
00027   use euitm_schemas
00028   use euitm_waves_interface
00029   use RFOF_diagnostics
00030   use RFOF_constants
00031   use RFOF_parameters
00032   use RFOF_wiener_sample_paths
00033 
00034   implicit none
00035 
00036   logical :: theta_step_constant = .true.
00037   real(8) :: d_theta = 1e-2
00038 contains
00039 
00040   !--------------------------------------------------------------------------------
00041   ! subroutine run_dumorb
00044   subroutine run_dumorb
00045 
00046     use RFOF_Efield_update
00047 
00048     ! Local
00049     type(rf_wave_global) :: RFglobal
00050     type(particle) :: marker
00051     type(particle_static), target :: marker_static
00052     type(resonance_memory), pointer :: mem(:,:)
00053     type(RFOF_cumlative_diagnostics) :: diagno
00054     logical :: interaction_failed_particle_overshot_resonance
00055     real(8) :: time_at_resonance
00056     integer :: jtime, NtimeSteps
00057     real(8) :: time, dt, dtACC, time_at_last_Erenorm
00058     integer :: MPI_nod_Id = 0
00059     real(8), target :: NACC = 1
00060     integer :: Nstep_for_updating_Efield = 20000
00061     integer :: ierr ! Error flag
00062 
00063     !call test_sample_path
00064 
00065     print *, "init_dumorb"
00066     call init_dumorb(RFglobal,marker,marker_static,mem,diagno,NtimeSteps,dt,NACC)
00067     
00068     time=0.
00069     time_at_last_Erenorm = time
00070     do jtime=1,NtimeSteps
00071        !print *, "  --  step_orbit_dumorb -- ", time, dt
00072 
00073        !---------------------------
00074        ! START LOOP over markers
00075 
00076        ! Time stepping
00077        NACC = real(mod(jtime , 3)+1)  ! For testing acceleration; can we find resonances also with acceleration?
00078        dtACC=dt*NACC
00079        !write(0,*)"nacc=",nacc,dtACC,time
00080        call step_orbit_dumorb(marker,dtACC,NACC)
00081        time = time + dtACC
00082 
00083        ! Add RF kicks
00084        call RFOF_master(time,time-1e-6, &
00085             RFglobal,marker,mem,diagno,MPI_nod_Id, &
00086             interaction_failed_particle_overshot_resonance, &
00087             time_at_resonance)
00088 
00089        ! END LOOP over markers
00090        !---------------------------
00091 
00092 
00093        if ( (mod(jtime , Nstep_for_updating_Efield) == 0) .or. (jtime.eq.NtimeSteps) ) then
00094           call output_heating_to_screen(RFglobal, diagno, time)
00095 
00096           call store2file_RFOF_cumlative_diagnostics(diagno,RFglobal)
00097           call update_efield_normalisation(time - time_at_last_Erenorm,RFglobal,MPI_nod_Id,ierr,diagno=diagno)
00098           time_at_last_Erenorm = time
00099        endif
00100 
00101        if (interaction_failed_particle_overshot_resonance) then
00102           write(0,*)'...'
00103           print *, 'kick failed: t_res, t',time_at_resonance, time
00104        endif
00105     enddo
00106     write(0,*)'loop done'
00107 
00108     !call store2file_RFOF_cumlative_diagnostics(diagno,RFglobal)
00109 
00110     print *, "exit_dumorb"
00111     call exit_dumorb(RFglobal,mem,diagno)
00112 
00113     !call test_ridder
00114 
00115   end subroutine run_dumorb
00116 
00117 
00118   !--------------------------------------------------------------------------------
00119   ! subroutine init_dumorb
00120   !--------------------------------------------------------------------------------
00121   subroutine init_dumorb(RFglobal,marker,marker_static,mem,diagno,NtimeSteps,dt,time_acceleration)
00122 
00123     ! Output
00124     type(rf_wave_global), intent(out) :: RFglobal
00125     type(particle), intent(inout) :: marker
00126     type(particle_static), target, intent(inout) :: marker_static
00127     type(resonance_memory), pointer, intent(out) :: mem(:,:)
00128     type(RFOF_cumlative_diagnostics), intent(inout) :: diagno
00129     integer, intent(out) ::NtimeSteps
00130     real(8), intent(out) :: dt
00131     real(8), target, intent(in) :: time_acceleration
00132 
00133     type(type_waves) :: itm_waves
00134 
00135     ! Local
00136     real(8) :: R0, B0, q, aminor
00137     real(8) :: weight, charge, mass,R,phi,z,E,xi,tauBounce
00138     type(magnetic_field_local) :: Blocal
00139     real(8) :: Rmin
00140     real(8) :: Rmax
00141     real(8) :: Zmin
00142     real(8) :: Zmax
00143 
00144     NAMELIST/input_control/dt, NtimeSteps
00145     NAMELIST/input_magnetifield/R0, aminor, B0, q
00146     NAMELIST/input_marker/weight,R,phi,z,mass,charge,E,xi
00147     NAMELIST/simplify_rfof/ &
00148          simplify__static_resonance_position_during_RF_kick , &
00149          simplify__drift_velocity_does_not_affect_resonance_condition , &
00150          simplify__parallel_velocity_does_not_affect_resonance_condition , &
00151          simplify__assume_zero_larmor_radius_in_KPERPxRHO , &
00152          simplify__kpar_is_nphi_over_R
00153     NAMELIST/IO_control/&
00154          start_time_event_output , &
00155          output__2D_RZ_out , &
00156          output__Orbit , &
00157          MAX_number_of_points_stored_in_the_Orbit , &
00158          output__rf_kicks , &
00159          MAX_number_of_points_stored_in_the_rf_kick , &
00160          output__resonace_predictions , &
00161          MAX_number_of_points_stored_in_the_resonance_memory
00162     NAMELIST/rz_boundingbox/Rmin, Rmax, Zmin, Zmax
00163 
00164     ! Default:
00165     dt = 1e-4
00166     NtimeSteps = 10
00167 !    time_acceleration = NACC
00168 
00169     open( io_channel_3872, FILE='input.rfof')
00170     read( io_channel_3872, input_control)
00171     read( io_channel_3872, input_magnetifield)
00172     read( io_channel_3872, input_marker)
00173     !read( io_channel_3872, simplify_rfof)
00174     !read( io_channel_3872, IO_control)
00175     !read( io_channel_3872, rz_boundingbox)
00176     close(io_channel_3872)
00177 
00178     ! Set plasma bounding box in R,Z
00179     call initialise_RFOF_parameters()
00180 
00181     ! Get B-field
00182     print *, " ---Init B field---"
00183     call magnetic_field_constructor_simple_field(R0,B0,q,aminor,Bglobal)
00184     !print *, " B0=", Bglobal%B0
00185     !print *, " R0=", Bglobal%R0
00186 
00187     ! Get wave field
00188     print *, " ---Init Wave field---"
00189     call dummy_rf_wave_field(RFglobal)
00190 
00191     !call construct_dummy_euitm_waves(itm_waves)
00192 
00193 
00194     ! Get resonance memory
00195     print *, " ---Init resonance memory---"
00196     call constructor_rf_resonance_memory_matrix(mem,RFglobal%nfreq,RFglobal%max_nnphi)
00197 
00198     ! Get marker
00199     print *, " ---Init marker---"
00200     E = E * 1.6022e-19
00201     Blocal = get_local_magnetic_field(R,phi,z)
00202     tauBounce = R0 / sqrt(2*E/1.66e-27)
00203 
00204     call make_marker(marker_static, weight,charge,mass,E,xi,tauBounce,Blocal)
00205     !call set_marker_pointers_from_marker(marker_static , marker)
00206 
00207     call set_marker_pointers(marker,   marker_static%Id,              marker_static%weight, &
00208          marker_static%R,              marker_static%phi,             marker_static%z,  &
00209          marker_static%charge,         marker_static%mass,            marker_static%energy, &
00210          marker_static%energy_kinetic, marker_static%velocity,        marker_static%magneticMoment, &
00211          marker_static%Pphi,           marker_static%vpar,            marker_static%vperp, &
00212          marker_static%omega_gyro,     marker_static%tauBounce,       marker_static%vDrift, &
00213          marker_static%vDriftRho,      marker_static%vDriftDia,       marker_static%d_vpar_d_rho, &
00214          marker_static%d_vpar_d_dia,   marker_static%d_vperp_d_rho,   marker_static%d_vperp_d_dia, &
00215          marker_static%d_vDriftRho_d_rho, marker_static%d_vDriftRho_d_dia, &
00216          marker_static%d_vDriftDia_d_rho, marker_static%d_vDriftDia_d_dia, &
00217          time_acceleration)
00218 
00219     ! Get diagnostics
00220     print *, " ---Init diagnostics---"
00221     call contructor_RFOF_cumlative_diagnostics(diagno,RFglobal%nfreq,RFglobal%max_nnphi)
00222 
00223   end subroutine init_dumorb
00224 
00225   !--------------------------------------------------------------------------------
00226   ! subroutine exit_dumorb
00227   !--------------------------------------------------------------------------------
00228   subroutine exit_dumorb(RFglobal,mem,diagno)
00229 
00230     ! Input/Output
00231     type(rf_wave_global), intent(inout) :: RFglobal
00232     type(resonance_memory), pointer, intent(out) :: mem(:,:)
00233     type(RFOF_cumlative_diagnostics), intent(inout) :: diagno
00234 
00235     call RFOF_destructor(RFglobal,mem,diagno)
00236 
00237   end subroutine exit_dumorb
00238 
00239   !--------------------------------------------------------------------------------
00240   ! subroutine step_orbit_dumorb
00241   !--------------------------------------------------------------------------------
00242   subroutine step_orbit_dumorb(marker,dt,NACC)
00243 
00244     ! Input
00245     real(8), intent(in) :: dt
00246     real(8), intent(in) :: NACC
00247 
00248     ! Input/Output
00249     type(particle), intent(inout) :: marker
00250 
00251     ! Local
00252     real(8) :: Bphi, Bpol, Bmod
00253     real(8) :: vpol, vphi
00254     real(8) :: rho, theta0, theta, R, phi, z
00255     real(8) :: dtORB
00256     type(magnetic_field_local) :: Blocal
00257 
00258     dtORB = (dt/NACC) ! Non-accelerated time step - orbit time step
00259 
00260     rho = sqrt( (marker%R-Bglobal%R0)**2 + marker%z**2 + 1e-5 )
00261 
00262 !    rho = 0.30166
00263     theta0 = sign(1d0,marker%z)*acos(( marker%R-Bglobal%R0)/rho)
00264 
00265     ! Local B-field before kick
00266     Blocal = get_local_magnetic_field(marker%R,marker%phi,marker%z)
00267 
00268 !    print *, "-"
00269 
00270     Bphi = Blocal%F / marker%R
00271     Bpol = Bglobal%B0 * rho / ( Bglobal%q * marker%R )
00272     Bmod = Blocal%Bmod
00273 
00274     vpol = marker%vpar * Bpol / Blocal%Bmod
00275     vphi = marker%vpar * Bphi / Blocal%Bmod
00276 
00277     if ( theta_step_constant ) then
00278        theta = theta0 + d_theta
00279        marker%tauBounce = dtORB * 2.*3.141592/d_theta
00280     else
00281        theta = theta0 + abs(vpol * dtORB / rho) + 1e-5
00282     endif
00283     phi = Blocal%phi + vphi * dtORB / Blocal%R
00284     R = Bglobal%R0 + rho*cos(theta)
00285     z =              rho*sin(theta)
00286 
00287     ! Local B-field after kick
00288     Blocal = get_local_magnetic_field(R,phi,z)
00289     call update_marker(marker,Blocal)
00290 
00291   end subroutine step_orbit_dumorb
00292 
00293 
00294   !--------------------------------------------------------------------------------
00295   !
00296   !--------------------------------------------------------------------------------
00297   subroutine output_heating_to_screen(RFglobal, diagno, time)
00298 
00299     type(rf_wave_global) :: RFglobal
00300     type(RFOF_cumlative_diagnostics) :: diagno
00301     real(8) :: time
00302 
00303     real(8) :: dt_diag
00304 
00305     if (sum(diagno%kick_counter) == 0) then
00306        write(0,*)" "
00307        write(0,*)" NO KICK IN THE CUMULATIVE DIAGNO, TIME=",time
00308        write(0,*)" "
00309        return
00310     endif
00311 
00312     dt_diag =  max(1d-40 , (diagno%time_end_sum_diagnostics - diagno%time_start_sum_diagnostics))
00313 
00314     write(0,*)" "
00315     print *, "Power  transfered to markers [W]    =", diagno%energy_to_markers / dt_diag
00316     print *, "Torque transfered to markers [Nm/s] =", diagno%toroidal_momentum_to_markers / dt_diag
00317     write(0,*)" "
00318     print *, "Energy transfered to markers [J]  =", diagno%energy_to_markers
00319     print *, "Pphi   transfered to markers [Nm] =", diagno%toroidal_momentum_to_markers
00320 
00321     if (RFglobal%nfreq .eq. 1 .and. RFglobal%max_nnphi .eq. 1) then
00322        print *, "               nphi/omega dE [Nm] =", &
00323             diagno%energy_to_markers * RFglobal%waves%coherentwave(1)%global_param%ntor(1) / &
00324             (2d0*rfof_pi*RFglobal%waves%coherentwave(1)%global_param%frequency)
00325     endif
00326     write(0,*)" "
00327 
00328   end subroutine output_heating_to_screen
00329 
00330 
00331   !--------------------------------------------------------------------------------
00332   !--------------------------------------------------------------------------------
00333   function anyfunc(x) result(y)
00334     real(8) :: x
00335     real(8) :: y    
00336     y=x**5
00337   end function anyfunc
00338 
00339   !--------------------------------------------------------------------------------
00340   !--------------------------------------------------------------------------------
00341   subroutine test_ridder
00342 
00343     real(8) :: x, h, rel_tol, err, fprim
00344 
00345     !interface
00346     !   function anyfunc(x) result(y)
00347     !     real(8) :: x
00348     !     real(8) :: y
00349     !   end function anyfunc
00350     !end interface
00351 
00352     x=1.d2
00353     h=0.5
00354     rel_tol = 1e-6
00355 
00356     call Ridders_derivative(anyfunc, x, h, rel_tol, err, fprim)
00357 
00358     print *,  x, err, fprim, err/fprim
00359 
00360   end subroutine test_ridder
00361 
00362 end module dummy_orbit