23 subroutine initialize(arg_chemfile, arg_aerofile, arg_numericsfile, &
25 bind(c, name="initialize")
27 character(kind=c_char),
intent(in) :: arg_chemfile(*)
28 character(kind=c_char),
intent(in) :: arg_aerofile(*)
29 character(kind=c_char),
intent(in) :: arg_numericsfile(*)
30 integer(c_int),
intent(in),
value :: n_batch
34 function tchem_getnumberofspecies()
bind(c, name="TChem_getNumberOfSpecies")
36 integer(kind=c_int) :: tchem_getnumberofspecies
38 function tchem_getlengthofstatevector() bind(c, &
39 name="TChem_getLengthOfStateVector")
41 integer(kind=c_int) :: tchem_getlengthofstatevector
43 subroutine tchem_getstatevector(array, i_batch) bind(c, &
44 name="TChem_getStateVector")
46 real(kind=c_double) :: array(*)
47 integer(c_int),
value :: i_batch
49 subroutine tchem_setstatevector(array, i_batch) bind(c, &
50 name="TChem_setStateVector")
52 real(kind=c_double) :: array(*)
53 integer(c_int),
value :: i_batch
55 integer(kind=c_size_t) function tchem_getspeciesname(index, result, &
56 buffer_size)
bind(C, name="TChem_getSpeciesName")
58 integer(kind=c_int),
intent(in) :: index
59 character(kind=c_char),
intent(out) :: result(*)
60 integer(kind=c_size_t),
intent(in),
value :: buffer_size
62 subroutine tchem_dotimestep(del_t)
bind(C, name="TChem_doTimestep")
64 real(kind=c_double),
intent(in) :: del_t
73 subroutine pmc_tchem_interface_solve(env_state, aero_data, aero_state, &
74 gas_data, gas_state, del_t)
77 type(env_state_t),
intent(in) :: env_state
87 real(kind=
dp),
intent(in) :: del_t
89 call tchem_from_partmc(aero_data, aero_state, gas_data, gas_state, &
92 call tchem_timestep(del_t)
94 call tchem_to_partmc(aero_data, aero_state, gas_data, gas_state, env_state)
96 end subroutine pmc_tchem_interface_solve
101 subroutine pmc_tchem_initialize(gas_config_filename, aero_config_filename, &
102 solver_config_filename, gas_data, aero_data, n_grid_cells)
106 character(len=*),
intent(in) :: gas_config_filename
108 character(len=*),
intent(in) :: aero_config_filename
110 character(len=*),
intent(in) :: solver_config_filename
116 integer,
intent(in) :: n_grid_cells
118 integer(kind=c_int) :: nSpec, nAeroSpec
121 real(kind=c_double),
dimension(:),
allocatable :: array
122 character(:),
allocatable :: val
125 call tchem_initialize(trim(gas_config_filename), &
126 trim(aero_config_filename), trim(solver_config_filename), &
130 nspec = tchem_getnumberofspecies()
131 call ensure_string_array_size(gas_data%name, nspec)
135 val = tchem_species_name(i-1)
136 gas_data%name(i) = trim(val)
141 gas_data%mosaic_index(:) = 0
158 end subroutine pmc_tchem_initialize
163 subroutine pmc_tchem_cleanup()
167 end subroutine pmc_tchem_cleanup
172 subroutine tchem_to_partmc(aero_data, aero_state, gas_data, gas_state, &
184 type(env_state_t),
intent(in) :: env_state
186 integer(c_int) :: nSpec, stateVecDim
188 real(kind=c_double),
dimension(:),
allocatable :: statevector
191 statevecdim = tchem_getlengthofstatevector()
192 nspec = tchem_getnumberofspecies()
193 allocate(statevector(statevecdim))
194 call tchem_getstatevector(statevector, 0)
196 gas_state%mix_rat = 0.0
198 gas_state%mix_rat = statevector(4:nspec+3) * 1000.d0
205 end subroutine tchem_to_partmc
210 subroutine tchem_from_partmc(aero_data, aero_state, gas_data, gas_state, &
222 type(env_state_t),
intent(in) :: env_state
224 real(kind=
dp),
allocatable :: statevector(:)
225 integer :: stateVecDim
230 statevecdim = tchem_getlengthofstatevector()
231 allocate(statevector(statevecdim))
233 statevector(1) = env_state_air_den(env_state)
234 statevector(2) = env_state%pressure
235 statevector(3) = env_state%temp
240 gas_state%mix_rat(i_water) = env_state_rel_humid_to_mix_rat(env_state)
242 statevector(4:
gas_data_n_spec(gas_data)+3) = gas_state%mix_rat / 1000.d0
249 call tchem_setstatevector(statevector, 0)
251 end subroutine tchem_from_partmc
256 subroutine tchem_timestep(del_t)
260 real(kind=c_double) :: del_t
262 call tchem_dotimestep(del_t)
264 end subroutine tchem_timestep
269 subroutine tchem_initialize(chemFile, aeroFile, NumericsFile, n_batch)
273 character(kind=c_char,len=*),
intent(in) :: chemFile
275 character(kind=c_char,len=*),
intent(in) :: aeroFile
277 character(kind=c_char,len=*),
intent(in) :: numericsFile
279 integer(kind=c_int),
intent(in) :: n_batch
281 call initialize(chemfile//c_null_char, aerofile//c_null_char, &
282 numericsfile//c_null_char, n_batch)
284 end subroutine tchem_initialize
289 function tchem_species_name(i_spec)
result(species_name)
293 character(:),
allocatable :: species_name
295 integer(kind=c_int),
intent(in) :: i_spec
296 character(kind=c_char, len=:),
allocatable :: cbuf
297 integer(kind=c_size_t) :: N
299 allocate(
character(256) :: cbuf)
301 n = tchem_getspeciesname(i_spec, cbuf, n)
302 allocate(
character(N) :: species_name)
303 species_name = cbuf(:n)
305 end function tchem_species_name