!======================================================================= ! emissions_mod : prescribed point-source emissions of tracers ! ! Injects a prescribed mass flux (kg/s) of any tracer listed in ! traceur.def at one grid cell and one model layer, following a schedule ! read from emissions.def (recurring events with a frequency, an offset, ! a duration and an end time, all in sols). Called from physiq_mod when ! callemissions=.true. (callphys.def). ! ! Origin: LMDZ.GENERIC/libf/phygeneric/volcano.F90 (trunk r4503, last ! changed r3893). L. Wilson; adapted for the LMD GCM by L. Kerber, ! J.-B. Madeleine and S. Hamid; generalised by A. Braude (2023); ! parallel version by E. Millour (2024). ! Generalisation: S. Hamid (Arizona State Univ.), 2025-2026. This file ! replaces volcano.F90 in the Generic PCM. The Mars PCM has the same file ! (LMDZ.MARS/libf/phymars/emissions.F90); the two differ only in [V2]. ! ! Differences from volcano.F90, marked [V1]..[V9] where they occur: ! [V1] Renamed volcano_mod/volcano/read_volcano/volcano.def to ! emissions_mod/emissions/read_emissions/emissions.def, and the ! internal variables likewise: nvolc->nsource, volci->isrc, ! ivolc->source_cell, latlonvals->source_latlon, ! heightvals->source_layer, eruptfreqs->schedule, volfluxes-> ! source_flux, eruptbool->source_active, dtmp->remaining_sols. ! [V2] Generic build: tracer names and lookup come from tracer_h (noms, ! tracer_index) and pi from comcstfi_mod. The Mars copy uses ! tracer_mod and comcstfi_h and a local tracer lookup instead. ! [V3] The third source coordinate is a model layer index (1 = lowest ! layer). volcano.F90 reads an altitude in km and snaps it to a level. ! [V4] The timing line has four values: frequency offset duration ! end_sol; end_sol is the exclusive run-elapsed sol after which ! no event starts. volcano.F90 reads three values. For the log ! records a source is labelled "continuous" when only one event fits ! before end_sol (offset+frequency >= end_sol), "pulsed" otherwise. ! [V5] Time is counted from the first physics call of the run ! (run_elapsed), not from zday, so offset=0 means "start when ! this run starts" whatever the season. ! [V6] Event activation tests whether the physics interval overlaps the ! current scheduled event; volcano.F90 tests modulo(step)==0 and ! keeps two separate application blocks. An event ending inside a ! physics step is weighted by its active fraction, so the emitted ! mass equals flux x duration exactly. ! [V7] emissions.def may contain blank lines and '#' comment lines ! anywhere (read_next_emission_data_line). ! [V8] Input validation at read time: layer in 1:nlay, positive ! frequency/duration, offset >= 0, duration < frequency, end_sol > ! offset, end_sol does not cut the last event short, and the former ! five-value timing line is refused. ! [V9] Greppable run-log records (EMISSIONS_*), a lastcall argument, and ! end-of-run assertions on event count, active time and emitted mass. ! EMISSIONS_PLAN, written at the first call, states what the run ! should emit so that the end-of-run records can be checked by eye. ! ! Reference for the original volcanic application: ! Wilson, L. and Head, J. W. (2007), J. Volcanol. Geotherm. Res., 163. !======================================================================= MODULE emissions_mod IMPLICIT NONE ! Source kind labels, derived from the schedule ([V4], log records only) integer,parameter :: SOURCE_CONTINUOUS=1, SOURCE_PULSED=2 ! Per-source state, local to this core (only sources inside the core's ! horizontal domain are kept; see read_emissions step 3). integer,save,protected :: nsource ! number of sources on this core !$OMP THREADPRIVATE(nsource) real,save,allocatable,protected :: source_latlon(:,:) ! (nsource,2) lat, lon (deg) !$OMP THREADPRIVATE(source_latlon) integer,save,allocatable,protected :: source_layer(:) ! (nsource) model layer index [V3] !$OMP THREADPRIVATE(source_layer) real,save,allocatable,protected :: schedule(:,:) ! (nsource,3) frequency, offset, ! duration of events (sols) !$OMP THREADPRIVATE(schedule) real,save,allocatable,protected :: source_end_sol(:) ! (nsource) exclusive end of the ! schedule ((run sol)) [V4] !$OMP THREADPRIVATE(source_end_sol) real,save,allocatable,protected :: source_flux(:,:) ! (nsource,nq) flux of each tracer (kg/s) !$OMP THREADPRIVATE(source_flux) integer,save,allocatable,protected :: source_cell(:) ! (nsource) horizontal grid index !$OMP THREADPRIVATE(source_cell) real,save,allocatable,protected :: remaining_sols(:) ! (nsource) remaining duration of the ! current event (sols) !$OMP THREADPRIVATE(remaining_sols) logical,save,allocatable,protected :: source_active(:) ! (nsource) event in progress !$OMP THREADPRIVATE(source_active) ! Diagnostics and assertions [V9] integer,save,allocatable,protected :: source_kind(:), event_count(:) !$OMP THREADPRIVATE(source_kind,event_count) real,save,allocatable,protected :: cumulative_mass(:,:), event_mass(:,:) ! (nsource,nq) kg !$OMP THREADPRIVATE(cumulative_mass,event_mass) real,save,allocatable,protected :: cumulative_active_seconds(:) !$OMP THREADPRIVATE(cumulative_active_seconds) real,save,protected :: run_start_zday=0.0 ! zday of the first physics call [V5] !$OMP THREADPRIVATE(run_start_zday) integer,save,protected :: physics_step_count=0 ! physics calls since the run started [V5] !$OMP THREADPRIVATE(physics_step_count) CONTAINS SUBROUTINE emissions(ngrid,nlay,pplev,wu,wv,pt,ssource,nq, & massarea,zday,ptimestep,lastcall) ! Called once per physics step. Returns ssource, the tracer tendency ! (kg/kg/s) due to the sources active during this step. use time_phylmdz_mod, only: daysec ! day length (s) use time_phylmdz_mod, only: nday ! number of sols in the run use vertical_layers_mod, only: pseudoalt ! layer pseudo-altitude (km) use geometry_mod, only: longitude_deg, latitude_deg use tracer_h, only: noms IMPLICIT NONE INTEGER, intent(in) :: nq ! number of tracers INTEGER, intent(in) :: ngrid ! number of grid points INTEGER, intent(in) :: nlay ! number of vertical layers REAL,intent(in) :: pplev(ngrid,nlay+1) ! inter-layer pressure (Pa) (unused) REAL,intent(in) :: wv(ngrid,nlay+1) ! wind (unused) REAL,intent(in) :: wu(ngrid,nlay+1) ! wind (unused) REAL,intent(in) :: pt(ngrid,nlay+1) ! temperature (unused) REAL,intent(in) :: massarea(ngrid,nlay) ! air mass of each cell and layer (kg) REAL,intent(in) :: zday ! time (sols) since Ls=0 REAL,intent(in) :: ptimestep ! physics timestep (s) LOGICAL,intent(in) :: lastcall ! last physics call of the run [V9] REAL, intent(out) :: ssource(ngrid,nlay,nq) ! source tendency (kg/kg/s) INTEGER :: l, iq, ig, isrc INTEGER :: expected_events, event_index, planned_events REAL :: run_elapsed, step_days, active_seconds REAL :: run_length_sols, planned_end, planned_active_seconds REAL :: reference_hour, local_hour REAL :: event_start, event_stop, overlap_start, overlap_stop REAL :: run_end_elapsed, schedule_run_end, expected_active_seconds REAL :: expected_mass, mass_tolerance, duration_tolerance REAL :: continuous_total, pulsed_total, combined_total CHARACTER(len=10) :: source_name CHARACTER(len=*),parameter :: rname="emissions" LOGICAL,SAVE :: firstcall=.true. !$OMP THREADPRIVATE(firstcall) !----------------------------------------------------------------------- ! 1. First call: read emissions.def, start the run clock, check timing !----------------------------------------------------------------------- if (firstcall) then call read_emissions(nq,ngrid,nlay) ! [V5] run time zero = this run's first physics call run_start_zday=zday physics_step_count=0 write(*,*) 'EMISSIONS_TRACER_ORDER ',(trim(noms(iq)),iq=1,nq) step_days=ptimestep/daysec do isrc=1,nsource ! Frequency and offset must be multiples of the physics step so ! that every event starts on a physics call. Duration need not be: ! a partial final step is weighted by its active fraction [V6]. if ((abs(schedule(isrc,1)/step_days-nint(schedule(isrc,1)/step_days)).gt.1.e-5).or. & (abs(schedule(isrc,2)/step_days-nint(schedule(isrc,2)/step_days)).gt.1.e-5)) then write(*,*) isrc,schedule(isrc,1)/step_days,schedule(isrc,2)/step_days call abort_physic(rname,'frequency and offset must both be multiples of the physics timestep',1) endif if (schedule(isrc,3)-schedule(isrc,1).ge.0.0) then write(*,*) isrc, schedule(isrc,1), schedule(isrc,3) call abort_physic(rname,'duration must be lower than frequency',1) endif if (source_kind(isrc)==SOURCE_CONTINUOUS) then source_name='continuous' else source_name='pulsed' endif ! Time of day of the first event: hour on the reference meridian, ! and local solar time at the selected cell (15 degrees per hour). reference_hour=modulo(run_start_zday+schedule(isrc,2),1.0)*24.0 local_hour=modulo(reference_hour+longitude_deg(source_cell(isrc))/15.0,24.0) write(*,'(A,I0,1X,A)') 'EMISSIONS_CONFIG source=',isrc,trim(source_name) write(*,'(A,2F12.5,A,2F12.5)') ' requested_lat_lon=',source_latlon(isrc,1:2), & ' selected_lat_lon=',latitude_deg(source_cell(isrc)),longitude_deg(source_cell(isrc)) write(*,'(A,I0,A,F10.5)') ' layer=',source_layer(isrc),' pseudoalt_km=', & pseudoalt(source_layer(isrc)) write(*,'(A,F10.4,A,F10.4,A,F12.6,A,F12.6)') & ' first_reference_hour=',reference_hour, & ' first_local_hour=',local_hour, & ' duration_seconds=',schedule(isrc,3)*daysec, & ' schedule_end_sol=',source_end_sol(isrc) ! Sanity check for the user: what this source is expected to emit, ! over its whole schedule and within this run (nday sols). ! The EMISSIONS_ASSERT_PASS and EMISSIONS_BUDGET records written at ! the last call must match the "in_this_run" numbers. run_length_sols=real(nday) call expected_schedule(isrc,source_end_sol(isrc),planned_events,planned_active_seconds) write(*,'(A,I0,A,I0,A,F14.3,A,99ES12.4)') & 'EMISSIONS_PLAN source=',isrc,' whole_schedule: events=',planned_events, & ' active_seconds=',planned_active_seconds, & ' mass_kg=',(source_flux(isrc,iq)*planned_active_seconds,iq=1,nq) planned_end=min(run_length_sols,source_end_sol(isrc)) call expected_schedule(isrc,planned_end,planned_events,planned_active_seconds) write(*,'(A,I0,A,F10.3,A,I0,A,F14.3,A,99ES12.4)') & 'EMISSIONS_PLAN source=',isrc,' in_this_run (',run_length_sols, & ' sols): events=',planned_events, & ' active_seconds=',planned_active_seconds, & ' mass_kg=',(source_flux(isrc,iq)*planned_active_seconds,iq=1,nq) enddo firstcall=.false. endif !----------------------------------------------------------------------- ! 2. Every call: apply the sources active during this physics step !----------------------------------------------------------------------- ssource(1:ngrid,1:nlay,1:nq)=0 ! an ended source must not persist step_days=ptimestep/daysec run_elapsed=real(physics_step_count)*step_days ! sols since run start [V5] do isrc=1,nsource if (source_kind(isrc)==SOURCE_CONTINUOUS) then source_name='continuous' else source_name='pulsed' endif ! [V6] Find the scheduled event that this physics step may overlap: ! the latest event starting at or before run_elapsed, or the next ! one if that event has already ended. event_start=schedule(isrc,2) if (run_elapsed>=event_start) then event_index=floor((run_elapsed-event_start)/schedule(isrc,1)) event_start=event_start+real(event_index)*schedule(isrc,1) endif event_stop=min(event_start+schedule(isrc,3),source_end_sol(isrc)) if (event_stop<=run_elapsed+1.e-6) then event_start=event_start+schedule(isrc,1) event_stop=min(event_start+schedule(isrc,3),source_end_sol(isrc)) endif overlap_start=max(event_start,run_elapsed) overlap_stop=min(event_stop,run_elapsed+step_days,source_end_sol(isrc)) if (event_startoverlap_start+1.e-6) then if (.not. source_active(isrc)) then ! Open a new event (only if the source is currently off, so a ! multi-step event is not restarted at every call) remaining_sols(isrc)=event_stop-overlap_start source_active(isrc) = .true. event_count(isrc)=event_count(isrc)+1 event_mass(isrc,:)=0.0 reference_hour=modulo(zday,1.0)*24.0 local_hour=modulo(reference_hour+longitude_deg(source_cell(isrc))/15.0,24.0) write(*,'(A,I0,A,A,A,I0,A,F12.6,A,F10.4,A,F10.4)') & 'EMISSIONS_EVENT_START source=',isrc,' kind=',trim(source_name), & ' event=',event_count(isrc), & ' run_sol=',run_elapsed,' reference_hour=',reference_hour, & ' local_hour=',local_hour end if end if ! Apply the active source once per call. The flux is in kg/s; the ! model needs kg/kg/s, hence the division by the cell air mass. ! active_seconds < ptimestep when the event ends inside this step. if (source_active(isrc)) then l = source_layer(isrc) ig=source_cell(isrc) active_seconds=min(remaining_sols(isrc)*daysec,ptimestep) do iq=1,nq ssource(ig,l,iq) = ssource(ig,l,iq) + & (active_seconds/ptimestep) * & source_flux(isrc,iq)/massarea(ig,l) event_mass(isrc,iq)=event_mass(isrc,iq)+source_flux(isrc,iq)*active_seconds cumulative_mass(isrc,iq)=cumulative_mass(isrc,iq)+source_flux(isrc,iq)*active_seconds end do cumulative_active_seconds(isrc)=cumulative_active_seconds(isrc)+active_seconds write(*,'(A,I0,A,A,A,I0,A,F12.6,A,F10.3,A,99ES12.4)') & 'EMISSIONS_STEP source=',isrc,' kind=',trim(source_name), & ' event=',event_count(isrc), & ' run_sol=',run_elapsed,' active_seconds=',active_seconds, & ' added_mass_kg=',(source_flux(isrc,iq)*active_seconds,iq=1,nq) write(*,'(A,I0,A,99ES12.4)') 'EMISSIONS_CUMULATIVE source=',isrc, & ' mass_kg=',(cumulative_mass(isrc,iq),iq=1,nq) remaining_sols(isrc)=max(0.0,remaining_sols(isrc)-active_seconds/daysec) if (remaining_sols(isrc)<=1.e-7*step_days) then ! Close the event and check that flux x duration = emitted mass source_active(isrc)=.false. duration_tolerance=max(0.1,1.e-5*schedule(isrc,3)*daysec) if (abs(sum(event_mass(isrc,:))-sum(source_flux(isrc,:))* & schedule(isrc,3)*daysec)>duration_tolerance*maxval(abs(source_flux(isrc,:)))) then call abort_physic(rname,'event integrated mass failed duration check',1) endif write(*,'(A,I0,A,A,A,I0,A,F12.6,A,F12.3,A,99ES12.4)') & 'EMISSIONS_EVENT_STOP source=',isrc,' kind=',trim(source_name), & ' event=',event_count(isrc), & ' run_sol=',run_elapsed+active_seconds/daysec, & ' emitted_seconds=',schedule(isrc,3)*daysec, & ' event_mass_kg=',(event_mass(isrc,iq),iq=1,nq) endif end if enddo ! of do isrc=1,nsource !----------------------------------------------------------------------- ! 3. Last call: check what was emitted against the schedule [V9] !----------------------------------------------------------------------- if (lastcall) then run_end_elapsed=run_elapsed+step_days do isrc=1,nsource ! Expected number of events and active seconds between the run ! start and min(run end, schedule end) schedule_run_end=min(run_end_elapsed,source_end_sol(isrc)) call expected_schedule(isrc,schedule_run_end,expected_events,expected_active_seconds) if (event_count(isrc)/=expected_events) then call abort_physic(rname,'event count assertion failed',1) endif if (abs(cumulative_active_seconds(isrc)-expected_active_seconds)>max(0.1,1.e-5*expected_active_seconds)) then call abort_physic(rname,'active duration assertion failed',1) endif do iq=1,nq expected_mass=source_flux(isrc,iq)*expected_active_seconds mass_tolerance=max(1.e-30,1.e-5*abs(expected_mass)) if (abs(cumulative_mass(isrc,iq)-expected_mass)>mass_tolerance) then call abort_physic(rname,'integrated source mass assertion failed',1) endif enddo write(*,'(A,I0,A,I0,A,F14.3,A,99ES12.4)') & 'EMISSIONS_ASSERT_PASS source=',isrc,' events=',event_count(isrc), & ' active_seconds=',cumulative_active_seconds(isrc), & ' cumulative_mass_kg=',(cumulative_mass(isrc,iq),iq=1,nq) enddo ! Totals per tracer, split by source kind do iq=1,nq continuous_total=0.0 pulsed_total=0.0 do isrc=1,nsource if (source_kind(isrc)==SOURCE_CONTINUOUS) & continuous_total=continuous_total+cumulative_mass(isrc,iq) if (source_kind(isrc)==SOURCE_PULSED) & pulsed_total=pulsed_total+cumulative_mass(isrc,iq) enddo combined_total=continuous_total+pulsed_total mass_tolerance=max(1.e-30,1.e-6*abs(combined_total)) if (abs(combined_total-continuous_total-pulsed_total)>mass_tolerance) then call abort_physic(rname,'combined source budget identity failed',1) endif write(*,'(A,A,A,3ES14.6)') 'EMISSIONS_BUDGET tracer=',trim(noms(iq)), & ' continuous_pulsed_combined_kg=',continuous_total,pulsed_total,combined_total enddo endif physics_step_count=physics_step_count+1 END SUBROUTINE emissions !======================================================================= SUBROUTINE read_emissions(nq,ngrid,nlay) ! Reads emissions.def (master only), validates each source, finds the ! grid cell containing it, and keeps on each core only the sources that ! fall inside its domain. USE mod_phys_lmdz_para, only: is_master, bcast use mod_phys_lmdz_transfert_para, only: reduce_sum use comcstfi_mod, only: pi use tracer_h, only: tracer_index ! index of a tracer name in traceur.def [V2] use mod_grid_phy_lmdz, only: nvertex ! number of grid cell corners use geometry_mod, only: boundslon, boundslat ! grid cell boundaries (rad) use geometry_mod, only: longitude_deg, latitude_deg ! grid cell centres (deg) use vertical_layers_mod, only: pseudoalt ! layer pseudo-altitude (km) IMPLICIT NONE integer, intent(in) :: nq ! number of tracers INTEGER, intent(in) :: ngrid ! number of atmospheric columns INTEGER, intent(in) :: nlay ! number of vertical layers ! Planet-wide arrays read by the master and broadcast to all cores real, allocatable :: source_latlon_total(:,:) integer, allocatable :: source_layer_total(:) integer, allocatable :: source_kind_total(:) real, allocatable :: source_end_sol_total(:) real, allocatable :: schedule_total(:,:) real, allocatable :: source_flux_total(:,:) real, allocatable :: remaining_sols_total(:) logical, allocatable :: source_active_total(:) integer :: ierr ! error return code integer :: iq,l,isrc,itrac,ntrac,blank real :: freq,offset,duration,end_sol,last_event_start real :: extra_value ! to detect the former five-value timing line integer :: ierr5 integer :: ig,i integer :: nsource_total ! total (planet-wide) number of sources integer :: nsource_check real :: lat_src,lon_src,flux integer :: layer_src character(len=500) :: source_tracer(nq) character(len=500) :: tracline ! one line of emissions.def real :: boundslon_deg(nvertex) ! cell longitude boundaries (deg) real :: boundslat_deg(nvertex) ! cell latitude boundaries (deg) real :: minlat, minlon, maxlat, maxlon logical,allocatable :: belongs_here(:) integer,allocatable :: cell_index(:) character(len=*),parameter :: rname="read_emissions" ! 1. Only the master reads and processes emissions.def ! 1.1 Open the file and get the total number of sources if (is_master) then open(407, form = 'formatted', status = 'old', & file = 'emissions.def', iostat=ierr) if (ierr /=0) then print*,trim(rname)//': Problem in opening emissions.def' call abort_physic(rname, "Problem in opening emissions.def", 1) end if write(*,*)trim(rname)//' Reading file emissions.def' call read_next_emission_data_line(407,tracline,ierr) ! [V7] if (ierr/=0) then print*,trim(rname)//": unable to read number of sources in emissions.def" call abort_physic(rname,"Unable to read number of sources in emissions.def",1) endif read(tracline,*,iostat=ierr) nsource_total if (ierr/=0) call abort_physic(rname,'invalid number of sources',1) endif ! of if (is_master) CALL bcast(nsource_total) ! 1.2 Allocate the planet-wide arrays on every core ALLOCATE(source_latlon_total(nsource_total,2),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_latlon_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(source_layer_total(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_layer_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(source_kind_total(nsource_total),stat=ierr) if (ierr/=0) then call abort_physic(rname,"failed allocation for source_kind_total",1) endif ALLOCATE(schedule_total(nsource_total,3),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for schedule_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(source_end_sol_total(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_end_sol_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(source_flux_total(nsource_total,nq),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_flux_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(remaining_sols_total(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for remaining_sols_total()" call abort_physic(rname,"failed allocation",1) endif ALLOCATE(source_active_total(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_active_total()" call abort_physic(rname,"failed allocation",1) endif source_flux_total(:,:)=0.0 ! tracers not listed for a source get no flux ! 1.3 Master only: read and validate each source if (is_master) then isrc = 0 do while (isrc.lt.nsource_total) call read_next_emission_data_line(407,tracline,ierr) if (ierr/=0) call abort_physic(rname,'unable to read source number',1) read(tracline,*,iostat=ierr) isrc if (ierr/=0) call abort_physic(rname,'invalid source number',1) write(*,*)trim(rname)//': emission source number:',isrc ! Position: latitude (deg), longitude (deg), model layer index [V3] call read_next_emission_data_line(407,tracline,ierr) if (ierr==0) read(tracline,*,iostat=ierr) lat_src,lon_src,layer_src if (ierr/=0) call abort_physic(rname,'unable to read source coordinates/layer',1) if (layer_src<1 .or. layer_src>nlay) then call abort_physic(rname,'source layer index outside 1:nlay',1) endif l=layer_src write(*,*)trim(rname)//' latitude:',lat_src write(*,*)trim(rname)//' longitude:',lon_src write(*,*)trim(rname)//' requested model layer:',l write(*,*)trim(rname)//' layer pseudo-altitude (km):',pseudoalt(l) source_latlon_total(isrc,1) = lat_src source_latlon_total(isrc,2) = lon_src source_layer_total(isrc) = l ! Number of tracers emitted by this source call read_next_emission_data_line(407,tracline,ierr) if (ierr==0) read(tracline,*,iostat=ierr) ntrac if (ierr/=0) call abort_physic(rname,'unable to read number of source tracers',1) write(*,*)trim(rname)//' number of emitted tracers:',ntrac ! Timing line [V4]: frequency offset duration end_sol (sols) call read_next_emission_data_line(407,tracline,ierr) if (ierr==0) then read(tracline,*,iostat=ierr) freq, offset, duration, end_sol endif if (ierr/=0) then call abort_physic(rname, & 'timing line requires frequency offset duration end_sol',1) endif ! A fifth value means the former format (with a source_kind field): ! refuse it rather than silently reading source_kind as end_sol. read(tracline,*,iostat=ierr5) freq, offset, duration, end_sol, extra_value if (ierr5==0) then write(*,*) trim(rname)//': timing line "'//trim(tracline)//'" has 5 values' call abort_physic(rname, & 'timing line now has 4 values (frequency offset duration end_sol); remove source_kind',1) endif ! Validation [V8] if (freq<=0.0) call abort_physic(rname,'source frequency must be positive',1) if (offset<0.0) call abort_physic(rname,'source offset must be nonnegative',1) if (duration<=0.0) call abort_physic(rname,'source duration must be positive',1) if (end_sol<=offset) then call abort_physic(rname,'schedule_end_sol must be greater than source offset',1) endif ! The last event admitted by the (exclusive) end must finish before it last_event_start=offset+floor((end_sol-offset-1.e-6)/freq)*freq if (last_event_start+duration>end_sol+1.e-6) then call abort_physic(rname,'schedule_end_sol cuts the final event short',1) endif write(*,*)trim(rname)//' event frequency (sols):',freq write(*,*)trim(rname)//' event offset (sols):',offset write(*,*)trim(rname)//' event duration (sols):',duration write(*,*)trim(rname)//' source schedule end (exclusive sol):',end_sol schedule_total(isrc,1) = freq schedule_total(isrc,2) = offset schedule_total(isrc,3) = duration ! Label for the log records: continuous if only one event fits ! before end_sol, pulsed if events repeat if (offset+freq>=end_sol) then source_kind_total(isrc) = SOURCE_CONTINUOUS else source_kind_total(isrc) = SOURCE_PULSED endif write(*,*)trim(rname)//' source label (1 continuous, 2 pulsed):',source_kind_total(isrc) source_end_sol_total(isrc) = end_sol remaining_sols_total(isrc) = duration source_active_total(isrc) = .false. ! 1.4 Tracer name and flux (kg/s), one pair per emitted tracer. ! The name must exist in traceur.def [V2]. do itrac=1,ntrac call read_next_emission_data_line(407,tracline,ierr) if (ierr/=0) call abort_physic(rname,'unable to read source tracer name',1) blank = index(tracline,' ') ! name ends at the first blank if (blank>0) then source_tracer(itrac) = tracline(1:blank) else source_tracer(itrac) = trim(tracline) endif iq=tracer_index(source_tracer(itrac)) if (iq>0) then call read_next_emission_data_line(407,tracline,ierr) if (ierr==0) read(tracline,*,iostat=ierr) flux if (ierr/=0) call abort_physic(rname,'unable to read tracer flux',1) source_flux_total(isrc,iq) = flux else write(*,*) trim(rname)//": problem with ",trim(source_tracer(itrac)) call abort_physic(rname, 'tracer '//& trim(source_tracer(itrac))//' not found', 1) endif enddo ! of do itrac=1,ntrac enddo ! of do while (isrc.lt.nsource_total) close(407) endif ! of if (is_master) ! 2. Share the data with all cores call bcast(source_latlon_total) call bcast(source_layer_total) call bcast(source_kind_total) call bcast(schedule_total) call bcast(source_end_sol_total) call bcast(source_flux_total) call bcast(remaining_sols_total) call bcast(source_active_total) ! 3. Each core keeps only the sources inside its domain ! 3.1 Find the cell containing each source. Cell boundaries are tested ! (not the nearest centre); the +/-360 alternatives handle cells ! straddling the longitude seam. nsource=0 allocate(belongs_here(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for belong_here()" call abort_physic(rname,"failed allocation",1) endif allocate(cell_index(nsource_total),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for cell_index()" call abort_physic(rname,"failed allocation",1) endif do isrc=1,nsource_total belongs_here(isrc)=.false. cell_index(isrc)=0 lat_src=source_latlon_total(isrc,1) lon_src=source_latlon_total(isrc,2) do ig=1,ngrid boundslon_deg(:)=boundslon(ig,:)/pi*180. boundslat_deg(:)=boundslat(ig,:)/pi*180. minlon=minval(boundslon_deg) maxlon=maxval(boundslon_deg) minlat=minval(boundslat_deg) maxlat=maxval(boundslat_deg) if ((minlat<=lat_src).and.(lat_src<=maxlat).and. & (((minlon<=lon_src).and.(lon_src<=maxlon)).or. & ((minlon+360.<=lon_src).and.(lon_src<=maxlon+360.)).or. & ((minlon-360.<=lon_src).and.(lon_src<=maxlon-360.)))) then nsource=nsource+1 belongs_here(isrc)=.true. cell_index(isrc)=ig exit endif enddo ! of do ig=1,ngrid enddo ! of isrc=1,nsource_total ! 3.2 Allocate the local arrays now that nsource is known if (nsource>0) then allocate(source_latlon(nsource,2),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_latlon()" call abort_physic(rname,"failed allocation",1) endif allocate(source_layer(nsource),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_layer()" call abort_physic(rname,"failed allocation",1) endif allocate(source_kind(nsource),event_count(nsource),stat=ierr) if (ierr/=0) call abort_physic(rname,"failed source diagnostic integer allocation",1) allocate(schedule(nsource,3),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for schedule()" call abort_physic(rname,"failed allocation",1) endif allocate(source_end_sol(nsource),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_end_sol()" call abort_physic(rname,"failed allocation",1) endif allocate(source_flux(nsource,nq),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_flux()" call abort_physic(rname,"failed allocation",1) endif allocate(source_cell(nsource),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_cell()" call abort_physic(rname,"failed allocation",1) endif allocate(remaining_sols(nsource),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for remaining_sols()" call abort_physic(rname,"failed allocation",1) endif allocate(source_active(nsource),stat=ierr) if (ierr/=0) then write(*,*) trim(rname)//" failed allocation for source_active()" call abort_physic(rname,"failed allocation",1) endif allocate(cumulative_mass(nsource,nq),event_mass(nsource,nq),stat=ierr) if (ierr/=0) call abort_physic(rname,"failed source mass diagnostic allocation",1) allocate(cumulative_active_seconds(nsource),stat=ierr) if (ierr/=0) call abort_physic(rname,"failed source time diagnostic allocation",1) endif ! of if (nsource>0) ! 3.3 Copy the local sources and zero their counters do isrc=1,nsource do i=1,nsource_total if (belongs_here(i)) then source_latlon(isrc,1:2)=source_latlon_total(i,1:2) source_layer(isrc)=source_layer_total(i) source_kind(isrc)=source_kind_total(i) schedule(isrc,1:3)=schedule_total(i,1:3) source_end_sol(isrc)=source_end_sol_total(i) source_flux(isrc,1:nq)=source_flux_total(i,1:nq) source_cell(isrc)=cell_index(i) remaining_sols(isrc) = remaining_sols_total(i) source_active(isrc) = source_active_total(i) event_count(isrc)=0 cumulative_mass(isrc,:)=0.0 event_mass(isrc,:)=0.0 cumulative_active_seconds(isrc)=0.0 belongs_here(i)=.false. ! done, move on to the next one exit endif ! of if (belongs_here(i)) enddo ! of do i=1,nsource_total enddo ! of do isrc=1,nsource ! 4. Sanity check: the sources over all cores must add up to nsource_total if (is_master) nsource_check=0 call bcast(nsource_check) call reduce_sum(nsource,nsource_check) call bcast(nsource_check) if (nsource_check/=nsource_total) then write(*,*) trim(rname)//" error. Sum of emission sources over all cores ", & nsource_check," differs from nsource_total=",nsource_total call abort_physic(rname," error. Check and fix emissions.F90",1) endif ! Summary for this core write(*,*) trim(rname)//": nsource=",nsource," /",nsource_total do isrc=1,nsource write(*,*) trim(rname)//": source_latlon(1:2)=",source_latlon(isrc,1:2) write(*,*) trim(rname)//": source_layer=",source_layer(isrc) write(*,*) trim(rname)//": layer pseudoaltitude (km)=",pseudoalt(source_layer(isrc)) write(*,*) trim(rname)//": source_kind=",source_kind(isrc) write(*,*) trim(rname)//": schedule(1:3)=",schedule(isrc,1:3) write(*,*) trim(rname)//": source_end_sol=",source_end_sol(isrc) write(*,*) trim(rname)//": source_flux(1:nq)=",source_flux(isrc,1:nq) write(*,*) trim(rname)//": source_cell=",source_cell(isrc) write(*,*) trim(rname)//": longitude(source_cell)=",longitude_deg(source_cell(isrc)) write(*,*) trim(rname)//": latitude(source_cell)=",latitude_deg(source_cell(isrc)) enddo END SUBROUTINE read_emissions !======================================================================= SUBROUTINE expected_schedule(isrc,t_end,n_events,active_seconds) ! Number of events of source isrc that start before t_end ((run sol)s, ! exclusive) and their total active time in seconds, clipped at t_end. ! Used for the EMISSIONS_PLAN record and the end-of-run assertions [V9]. use time_phylmdz_mod, only: daysec IMPLICIT NONE INTEGER,INTENT(IN) :: isrc REAL,INTENT(IN) :: t_end INTEGER,INTENT(OUT) :: n_events REAL,INTENT(OUT) :: active_seconds INTEGER :: ievent if (t_end+1.e-6>=schedule(isrc,2)) then n_events=1+int(max(0.0,t_end-schedule(isrc,2)-1.e-6)/schedule(isrc,1)) else n_events=0 endif active_seconds=0.0 do ievent=0,n_events-1 active_seconds=active_seconds+max(0.0,min(schedule(isrc,3), & t_end-(schedule(isrc,2)+real(ievent)*schedule(isrc,1))))*daysec enddo END SUBROUTINE expected_schedule !======================================================================= SUBROUTINE read_next_emission_data_line(unit_number,line,ierr) ! Returns the next line of emissions.def that is neither blank nor a ! '#' comment [V7]. ierr /= 0 at end of file or read error. IMPLICIT NONE INTEGER,INTENT(IN) :: unit_number CHARACTER(len=*),INTENT(OUT) :: line INTEGER,INTENT(OUT) :: ierr CHARACTER(len=len(line)) :: adjusted_line do read(unit_number,'(A)',iostat=ierr) line if (ierr/=0) return adjusted_line=adjustl(line) if (len_trim(adjusted_line)==0) cycle if (adjusted_line(1:1)=='#') cycle return enddo END SUBROUTINE read_next_emission_data_line END MODULE emissions_mod