45 type(c_ptr) function
solver_new(n_state_var, n_cells, var_type, &
46 n_rxn, n_rxn_int_param, n_rxn_float_param, &
47 n_rxn_env_param, n_aero_phase, n_aero_phase_int_param, &
48 n_aero_phase_float_param, n_aero_rep, &
49 n_aero_rep_int_param, n_aero_rep_float_param, &
50 n_aero_rep_env_param, n_sub_model, n_sub_model_int_param,&
51 n_sub_model_float_param, n_sub_model_env_param)
bind (c)
55 integer(kind=c_int),
value :: n_state_var
57 integer(kind=c_int),
value :: n_cells
59 type(c_ptr),
value :: var_type
61 integer(kind=c_int),
value :: n_rxn
63 integer(kind=c_int),
value :: n_rxn_int_param
65 integer(kind=c_int),
value :: n_rxn_float_param
67 integer(kind=c_int),
value :: n_rxn_env_param
69 integer(kind=c_int),
value :: n_aero_phase
71 integer(kind=c_int),
value :: n_aero_phase_int_param
73 integer(kind=c_int),
value :: n_aero_phase_float_param
75 integer(kind=c_int),
value :: n_aero_rep
77 integer(kind=c_int),
value :: n_aero_rep_int_param
80 integer(kind=c_int),
value :: n_aero_rep_float_param
83 integer(kind=c_int),
value :: n_aero_rep_env_param
85 integer(kind=c_int),
value :: n_sub_model
87 integer(kind=c_int),
value :: n_sub_model_int_param
89 integer(kind=c_int),
value :: n_sub_model_float_param
91 integer(kind=c_int),
value :: n_sub_model_env_param
96 max_conv_fails)
bind (c)
99 type(c_ptr),
value :: solver_data
101 type(c_ptr),
value :: abs_tol
103 real(kind=c_double),
value :: rel_tol
105 integer(kind=c_int),
value :: max_steps
107 integer(kind=c_int),
value :: max_conv_fails
112 integer(kind=c_int) function solver_set_debug_out(solver_data, &
116 type(c_ptr),
value :: solver_data
118 integer(kind=c_int),
value :: do_output
119 end function solver_set_debug_out
122 integer(kind=c_int) function solver_set_eval_jac(solver_data, &
126 type(c_ptr),
value :: solver_data
128 integer(kind=c_int),
value :: eval_Jac
129 end function solver_set_eval_jac
133 integer(kind=c_int) function solver_run(solver_data, state, env, &
134 t_initial, t_final)
bind (c)
137 type(c_ptr),
value :: solver_data
139 type(c_ptr),
value :: state
141 type(c_ptr),
value :: env
143 real(kind=c_double),
value :: t_initial
145 real(kind=c_double),
value :: t_final
152 type(c_ptr),
value :: solver_data
157 RHS_evals, LS_setups, error_test_fails, NLS_iters, &
158 NLS_convergence_fails, DLS_Jac_evals, DLS_RHS_evals, &
159 last_time_step__s, next_time_step__s, Jac_eval_fails, &
160 RHS_evals_total, Jac_evals_total, RHS_time__s, &
161 Jac_time__s, max_loss_precision)
bind (c)
164 type(c_ptr),
value :: solver_data
166 type(c_ptr),
value :: solver_flag
168 type(c_ptr),
value :: num_steps
170 type(c_ptr),
value :: RHS_evals
172 type(c_ptr),
value :: LS_setups
174 type(c_ptr),
value :: error_test_fails
176 type(c_ptr),
value :: NLS_iters
178 type(c_ptr),
value :: NLS_convergence_fails
180 type(c_ptr),
value :: DLS_Jac_evals
182 type(c_ptr),
value :: DLS_RHS_evals
184 type(c_ptr),
value :: last_time_step__s
186 type(c_ptr),
value :: next_time_step__s
188 type(c_ptr),
value :: Jac_eval_fails
190 type(c_ptr),
value :: RHS_evals_total
192 type(c_ptr),
value :: Jac_evals_total
194 type(c_ptr),
value :: RHS_time__s
196 type(c_ptr),
value :: Jac_time__s
198 type(c_ptr),
value :: max_loss_precision
203 n_float_param, n_env_param, int_param, float_param, &
204 solver_data)
bind (c)
207 integer(kind=c_int),
value :: rxn_type
209 integer(kind=c_int),
value :: n_int_param
211 integer(kind=c_int),
value :: n_float_param
213 integer(kind=c_int),
value :: n_env_param
215 type(c_ptr),
value :: int_param
217 type(c_ptr),
value :: float_param
219 type(c_ptr),
value :: solver_data
227 integer(kind=c_int),
value :: cell_id
229 integer(kind=c_int) :: rxn_id
231 integer(kind=c_int),
value :: rxn_type
233 type(c_ptr),
value :: update_data
235 type(c_ptr),
value :: solver_data
242 type(c_ptr),
value :: solver_data
247 n_float_param, n_env_param, int_param, float_param, &
251 integer(kind=c_int),
value :: sub_model_type
253 integer(kind=c_int),
value :: n_int_param
255 integer(kind=c_int),
value :: n_float_param
257 integer(kind=c_int),
value :: n_env_param
259 type(c_ptr),
value :: int_param
261 type(c_ptr),
value :: float_param
263 type(c_ptr),
value :: solver_data
268 update_data, solver_data)
bind(c)
271 integer(kind=c_int),
value :: cell_id
273 integer(kind=c_int) :: sub_model_id
275 integer(kind=c_int),
value :: sub_model_type
277 type(c_ptr),
value :: update_data
279 type(c_ptr),
value :: solver_data
286 type(c_ptr),
value :: solver_data
291 int_param, float_param, solver_data)
bind(c)
294 integer(kind=c_int),
value :: n_int_param
296 integer(kind=c_int),
value :: n_float_param
298 type(c_ptr),
value :: int_param
300 type(c_ptr),
value :: float_param
302 type(c_ptr),
value :: solver_data
309 type(c_ptr),
value :: solver_data
314 n_float_param, n_env_param, int_param, float_param, &
318 integer(kind=c_int),
value :: aero_rep_type
320 integer(kind=c_int),
value :: n_int_param
322 integer(kind=c_int),
value :: n_float_param
324 integer(kind=c_int),
value :: n_env_param
326 type(c_ptr),
value :: int_param
328 type(c_ptr),
value :: float_param
330 type(c_ptr),
value :: solver_data
335 update_data, solver_data)
bind(c)
338 integer(kind=c_int),
value :: cell_id
340 integer(kind=c_int) :: aero_rep_id
342 integer(kind=c_int),
value :: aero_rep_type
344 type(c_ptr),
value :: update_data
346 type(c_ptr),
value :: solver_data
353 type(c_ptr),
value :: solver_data
360 type(c_ptr),
value,
intent(in) :: solver_data
374 type(c_ptr),
public :: solver_c_ptr
380 integer(kind=i_kind),
public :: max_conv_fails = &
383 logical :: initialized = .false.
430 subroutine initialize(this, var_type, abs_tol, mechanisms, aero_phases, &
431 aero_reps, sub_models, rxn_phase, n_cells)
437 integer(kind=i_kind),
allocatable,
intent(in) :: var_type(:)
441 real(kind=dp),
allocatable,
intent(in) :: abs_tol(:)
443 type(mechanism_data_ptr),
pointer,
intent(in) :: mechanisms(:)
445 type(aero_phase_data_ptr),
pointer,
intent(in) :: aero_phases(:)
447 type(aero_rep_data_ptr),
pointer,
intent(in) :: aero_reps(:)
449 type(sub_model_data_ptr),
pointer,
intent(in) :: sub_models(:)
453 integer(kind=i_kind),
intent(in) :: rxn_phase
455 integer(kind=i_kind),
optional :: n_cells
458 integer(kind=c_int),
pointer :: var_type_c(:)
460 real(kind=c_double),
pointer :: abs_tol_c(:)
462 integer(kind=i_kind) :: i_mech, i_rxn, i_aero_phase, i_aero_rep, &
465 class(rxn_data_t),
pointer :: rxn
467 type(rxn_factory_t) :: rxn_factory
469 type(aero_phase_data_t),
pointer :: aero_phase
471 class(aero_rep_data_t),
pointer :: aero_rep
474 type(aero_rep_factory_t) :: aero_rep_factory
476 class(sub_model_data_t),
pointer :: sub_model
478 type(sub_model_factory_t) :: sub_model_factory
480 integer(kind=c_int),
pointer :: int_param(:)
482 real(kind=c_double),
pointer :: float_param(:)
484 integer(kind=c_int) :: n_rxn
486 integer(kind=c_int) :: n_rxn_int_param
488 integer(kind=c_int) :: n_rxn_float_param
490 integer(kind=c_int) :: n_rxn_env_param
492 integer(kind=c_int) :: n_aero_phase
494 integer(kind=c_int) :: n_aero_phase_int_param
496 integer(kind=c_int) :: n_aero_phase_float_param
498 integer(kind=c_int) :: n_aero_rep
500 integer(kind=c_int) :: n_aero_rep_int_param
502 integer(kind=c_int) :: n_aero_rep_float_param
504 integer(kind=c_int) :: n_aero_rep_env_param
506 integer(kind=c_int) :: n_sub_model
508 integer(kind=c_int) :: n_sub_model_int_param
510 integer(kind=c_int) :: n_sub_model_float_param
512 integer(kind=c_int) :: n_sub_model_env_param
514 integer(kind=c_int) :: l_n_cells
516 if (
present(n_cells))
then
524 call assert_msg(825843466,
size(abs_tol).eq.
size(var_type), &
525 "Mismatched absolute tolerance and variable type arrays: "// &
526 "abs_tol size: "//trim(to_string(
size(abs_tol)))// &
527 "; var_type: "//trim(to_string(
size(var_type))))
530 allocate(var_type_c(
size(var_type)))
531 allocate(abs_tol_c(
size(abs_tol)))
532 var_type_c(:) = int(var_type(:), kind=c_int)
533 abs_tol_c(:) = real(abs_tol(:), kind=c_double)
538 n_rxn_float_param = 0
542 do i_mech=1,
size(mechanisms)
543 do i_rxn=1, mechanisms(i_mech)%val%size()
544 rxn => mechanisms(i_mech)%val%get_rxn(i_rxn)
545 select case (rxn%rxn_phase)
547 if (rxn_phase.eq.aero_rxn) cycle
549 if (rxn_phase.eq.gas_rxn) cycle
551 if (rxn_phase.eq.gas_rxn) cycle
554 n_rxn_int_param = n_rxn_int_param +
size(rxn%condensed_data_int)
555 n_rxn_float_param = n_rxn_float_param +
size(rxn%condensed_data_real)
556 n_rxn_env_param = n_rxn_env_param + rxn%num_env_params
562 n_aero_phase =
size(aero_phases)
563 n_aero_phase_int_param = 0
564 n_aero_phase_float_param = 0
567 do i_aero_phase=1, n_aero_phase
568 aero_phase => aero_phases(i_aero_phase)%val
569 n_aero_phase_int_param = n_aero_phase_int_param + &
570 size(aero_phase%condensed_data_int)
571 n_aero_phase_float_param = n_aero_phase_float_param + &
572 size(aero_phase%condensed_data_real)
577 n_aero_rep =
size(aero_reps)
578 n_aero_rep_int_param = 0
579 n_aero_rep_float_param = 0
580 n_aero_rep_env_param = 0
583 do i_aero_rep=1, n_aero_rep
584 aero_rep => aero_reps(i_aero_rep)%val
585 n_aero_rep_int_param = n_aero_rep_int_param + &
586 size(aero_rep%condensed_data_int)
587 n_aero_rep_float_param = n_aero_rep_float_param + &
588 size(aero_rep%condensed_data_real)
589 n_aero_rep_env_param = n_aero_rep_env_param + &
590 aero_rep%num_env_params
595 n_sub_model =
size(sub_models)
596 n_sub_model_int_param = 0
597 n_sub_model_float_param = 0
598 n_sub_model_env_param = 0
601 do i_sub_model=1, n_sub_model
602 sub_model => sub_models(i_sub_model)%val
603 n_sub_model_int_param = n_sub_model_int_param + &
604 size(sub_model%condensed_data_int)
605 n_sub_model_float_param = n_sub_model_float_param + &
606 size(sub_model%condensed_data_real)
607 n_sub_model_env_param = n_sub_model_env_param + sub_model%num_env_params
613 int(
size(var_type_c), kind=c_int), &
621 n_aero_phase_int_param, &
622 n_aero_phase_float_param, &
624 n_aero_rep_int_param, &
625 n_aero_rep_float_param, &
626 n_aero_rep_env_param, &
628 n_sub_model_int_param, &
629 n_sub_model_float_param, &
630 n_sub_model_env_param &
635 do i_mech=1,
size(mechanisms)
636 do i_rxn=1, mechanisms(i_mech)%val%size()
643 rxn => mechanisms(i_mech)%val%get_rxn(i_rxn)
646 select case (rxn%rxn_phase)
648 if (rxn_phase.eq.aero_rxn) cycle
650 if (rxn_phase.eq.gas_rxn) cycle
652 if (rxn_phase.eq.gas_rxn) cycle
656 allocate(int_param(
size(rxn%condensed_data_int)))
657 allocate(float_param(
size(rxn%condensed_data_real)))
658 int_param(:) = int(rxn%condensed_data_int(:), kind=c_int)
659 float_param(:) = real(rxn%condensed_data_real(:), kind=c_double)
663 int(rxn_factory%get_type(rxn), kind=c_int),&
664 int(
size(int_param), kind=c_int), &
665 int(
size(float_param), kind=c_int), &
666 rxn%num_env_params, &
668 c_loc(float_param), &
673 deallocate(int_param)
674 deallocate(float_param)
681 do i_aero_phase=1,
size(aero_phases)
684 aero_phase => aero_phases(i_aero_phase)%val
687 allocate(int_param(
size(aero_phase%condensed_data_int)))
688 allocate(float_param(
size(aero_phase%condensed_data_real)))
689 int_param(:) = int(aero_phase%condensed_data_int(:), kind=c_int)
690 float_param(:) = real(aero_phase%condensed_data_real(:), kind=c_double)
694 int(
size(int_param), kind=c_int), &
695 int(
size(float_param), kind=c_int), &
697 c_loc(float_param), &
702 deallocate(int_param)
703 deallocate(float_param)
710 do i_aero_rep=1,
size(aero_reps)
713 aero_rep => aero_reps(i_aero_rep)%val
716 allocate(int_param(
size(aero_rep%condensed_data_int)))
717 allocate(float_param(
size(aero_rep%condensed_data_real)))
718 int_param(:) = int(aero_rep%condensed_data_int(:), kind=c_int)
719 float_param(:) = real(aero_rep%condensed_data_real(:), kind=c_double)
723 int(aero_rep_factory%get_type(aero_rep), kind=c_int), &
725 int(
size(int_param), kind=c_int), &
726 int(
size(float_param), kind=c_int), &
727 aero_rep%num_env_params, &
729 c_loc(float_param), &
734 deallocate(int_param)
735 deallocate(float_param)
741 do i_sub_model=1,
size(sub_models)
744 sub_model => sub_models(i_sub_model)%val
747 allocate(int_param(
size(sub_model%condensed_data_int)))
748 allocate(float_param(
size(sub_model%condensed_data_real)))
749 int_param(:) = int(sub_model%condensed_data_int(:), kind=c_int)
750 float_param(:) = real(sub_model%condensed_data_real(:), kind=c_double)
754 int(sub_model_factory%get_type(sub_model), kind=c_int), &
756 int(
size(int_param), kind=c_int), &
757 int(
size(float_param), kind=c_int), &
758 sub_model%num_env_params, &
760 c_loc(float_param), &
765 deallocate(int_param)
766 deallocate(float_param)
775 real(this%rel_tol, kind=c_double), &
776 int(this%max_steps, kind=c_int), &
777 int(this%max_conv_fails, kind=c_int)&
781 this%initialized = .true.
784 deallocate(abs_tol_c)
785 deallocate(var_type_c)
797 class(sub_model_update_data_t),
intent(in) :: update_data
800 update_data%get_cell_id()-1, &
801 update_data%sub_model_solver_id, &
802 update_data%get_type(), &
803 update_data%get_data(), &
817 class(rxn_update_data_t),
intent(in) :: update_data
820 update_data%get_cell_id()-1, &
821 update_data%rxn_solver_id, &
822 update_data%get_type(), &
823 update_data%get_data(), &
838 class(aero_rep_update_data_t),
intent(in) :: update_data
841 update_data%get_cell_id()-1, &
842 update_data%aero_rep_solver_id, &
843 update_data%get_type(), &
844 update_data%get_data(), &
853 subroutine solve(this, camp_state, t_initial, t_final, solver_stats)
858 type(camp_state_t),
target,
intent(inout) :: camp_state
860 real(kind=dp),
intent(in) :: t_initial
862 real(kind=dp),
intent(in) :: t_final
864 type(solver_stats_t),
intent(inout),
optional,
target :: solver_stats
866 integer(kind=c_int) :: solver_status
869 if (
present(solver_stats))
then
871 if (solver_stats%debug_out)
then
872 solver_status = solver_set_debug_out( &
877 solver_status = solver_set_debug_out( &
883 if (solver_stats%eval_Jac)
then
884 solver_stats = solver_set_eval_jac( &
889 solver_stats = solver_set_eval_jac( &
897 call this%reset_timers( )
903 c_loc(camp_state%state_var), &
904 c_loc(camp_state%env_var), &
905 real(t_initial, kind=c_double), &
906 real(t_final, kind=c_double) &
910 if (
present(solver_stats))
then
911 call this%get_solver_stats( solver_stats )
912 solver_stats%status_code = solver_status
913 solver_stats%start_time__s = t_initial
914 solver_stats%end_time__s = t_final
916 call warn_assert_msg(997420005, solver_status.eq.0,
"Solver failed")
941 type(solver_stats_t),
intent(inout),
target :: solver_stats
945 c_loc( solver_stats%solver_flag ), &
946 c_loc( solver_stats%num_steps ), &
947 c_loc( solver_stats%RHS_evals ), &
948 c_loc( solver_stats%LS_setups ), &
949 c_loc( solver_stats%error_test_fails ), &
950 c_loc( solver_stats%NLS_iters ), &
951 c_loc( solver_stats%NLS_convergence_fails ), &
952 c_loc( solver_stats%DLS_Jac_evals ), &
953 c_loc( solver_stats%DLS_RHS_evals ), &
954 c_loc( solver_stats%last_time_step__s ), &
955 c_loc( solver_stats%next_time_step__s ), &
956 c_loc( solver_stats%Jac_eval_fails ), &
957 c_loc( solver_stats%RHS_evals_total ), &
958 c_loc( solver_stats%Jac_evals_total ), &
959 c_loc( solver_stats%RHS_time__s ), &
960 c_loc( solver_stats%Jac_time__s ), &
961 c_loc( solver_stats%max_loss_precision ) )
973#ifdef CAMP_USE_SUNDIALS
1004 if (this%initialized)
call solver_free(this%solver_c_ptr)
Add condensed aerosol phase data to the solver data block.
Add condensed aerosol representation data to the solver data block.
Print the aerosol representation data.
Update aerosol representation data.
Add condensed reaction data to the solver data block.
Free the memory associated with a solver.
Get the solver statistics.
Interface to c ODE solver functions.
Reset the solver function timers.
Add condensed sub model data to the solver data block.
Interface for to_string functions.
The abstract aero_phase_data_t structure and associated subroutines.
The abstract aero_rep_data_t structure and associated subroutines.
The aero_rep_factory_t type and associated subroutines.
The camp_solver_data_t structure and associated subroutines.
subroutine get_solver_stats(this, solver_stats)
Get solver statistics.
subroutine update_rxn_data(this, update_data)
Update reaction data.
subroutine update_sub_model_data(this, update_data)
Update sub-model data.
elemental subroutine finalize(this)
Finalize the solver data.
type(camp_solver_data_t) function, pointer constructor()
Constructor for camp_solver_data_t.
subroutine do_print(this)
Print the solver data.
subroutine reset_timers(this)
Reset the solver function timers.
logical function is_solver_available(this)
Check whether a solver is available for the integration.
subroutine initialize(this, var_type, abs_tol, mechanisms, aero_phases, aero_reps, sub_models, rxn_phase, n_cells)
Initialize the solver.
real(kind=dp), parameter camp_solver_default_rel_tol
Default relative tolerance for integration.
integer, parameter camp_solver_success
Result code indicating successful completion.
integer(kind=i_kind), parameter camp_solver_default_max_conv_fails
Default maximum number of integration convergence failures.
subroutine solve(this, camp_state, t_initial, t_final, solver_stats)
Solve the mechanism(s) for a specified timestep.
subroutine update_aero_rep_data(this, update_data)
Update aerosol representation data based on data passed from the host model related to aerosol proper...
integer(kind=i_kind), parameter camp_solver_default_max_steps
Default max number of integration steps.
The camp_state_t structure and associated subroutines.
integer, parameter dp
Kind of a double precision real number.
integer, parameter i_kind
Kind of an integer.
The mechanism_data_t structure and associated subroutines.
The rxn_data_t structure and associated subroutines.
The abstract rxn_factory_t structure and associated subroutines.
The solver_stats_t type and associated subroutines.
The abstract sub_model_data_t structure and associated subroutines.
The sub_model_factory_t type and associated subroutines.
Common utility subroutines.
subroutine die_msg(code, error_msg)
Error immediately.
subroutine assert_msg(code, condition_ok, error_msg)
Errors unless condition_ok is true.
subroutine warn_assert_msg(code, condition_ok, warning_msg)
Prints a warning message if condition_ok is false.