Implement CCLite 0.1.6 diagnostics checker

This commit is contained in:
Codex
2026-07-05 17:13:31 +08:00
parent a9f40635b6
commit 731e27df67
15 changed files with 555 additions and 49 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ contains
return
end if
ierr = ccl_controller_run_empty_coupling(ccl_case)
ierr = ccl_controller_run_empty_coupling(ccl_case, trim(input_file))
end function handle_mode
subroutine print_help()
+2 -2
View File
@@ -3,8 +3,8 @@ module ccl_version
integer, parameter :: CCL_VERSION_MAJOR = 0
integer, parameter :: CCL_VERSION_MINOR = 1
integer, parameter :: CCL_VERSION_PATCH = 5
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.5"
integer, parameter :: CCL_VERSION_PATCH = 6
character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.6"
contains
+89 -34
View File
@@ -11,6 +11,9 @@ module ccl_controller
CCL_ROLE_SPH_TO_CCL_PARTICLE, ccl_data_header_init, ccl_data_header_set_count, &
ccl_data_header_set_state, ccl_data_header_size, ccl_data_header_validate, &
ccl_data_record_size_for_role
use ccl_diagnostics, only: CCL_Diagnostics_Type, ccl_diagnostics_close, &
ccl_diagnostics_open, ccl_diagnostics_write_force_balance, ccl_diagnostics_write_ipc, &
ccl_diagnostics_write_run
use ccl_errors, only: CCL_ERROR_INPUT_INVALID_VALUE, CCL_ERROR_IPC_TIMEOUT, &
CCL_ERROR_SYSTEM_INTERNAL, CCL_ERROR_WORKER_START_FAILED, CCL_ERROR_WORKER_STOP_FAILED, &
CCL_SUCCESS
@@ -53,11 +56,13 @@ module ccl_controller
contains
integer function ccl_controller_run_empty_coupling(ccl_case) result(ierr)
integer function ccl_controller_run_empty_coupling(ccl_case, input_file) result(ierr)
type(CCL_Case_Type), intent(in) :: ccl_case
character(len=*), intent(in) :: input_file
type(CCL_Worker_Runtime) :: sph_worker
type(CCL_Worker_Runtime) :: fem_worker
type(CCL_Diagnostics_Type) :: diag
integer :: step_count
integer :: step_id
integer :: close_ierr
@@ -70,7 +75,11 @@ contains
return
end if
call ccl_log_info("controller", "starting 0.1.5 empty coupling dry-run")
call ccl_diagnostics_open(diag, ccl_case%output%directory, ccl_case%name, input_file, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_log_info("controller", "starting 0.1.6 empty coupling dry-run")
call ccl_diagnostics_write_run(diag, "INFO", "controller", "starting empty coupling dry-run")
call init_worker(sph_worker, ccl_case, CCL_CASE_SOLVER_SPH, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call init_worker(fem_worker, ccl_case, CCL_CASE_SOLVER_FEM, ierr)
@@ -80,10 +89,12 @@ contains
if (ierr /= CCL_SUCCESS) goto 900
call create_worker_resources(fem_worker, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call log_worker_resources(diag, sph_worker)
call log_worker_resources(diag, fem_worker)
call start_worker(sph_worker, ierr)
call start_worker(sph_worker, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call start_worker(fem_worker, ierr)
call start_worker(fem_worker, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call wait_worker_ready(sph_worker, ierr)
@@ -91,38 +102,42 @@ contains
call wait_worker_ready(fem_worker, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call send_startup_sequence(sph_worker, ccl_case%time%dt, ierr)
call send_startup_sequence(sph_worker, ccl_case%time%dt, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call send_startup_sequence(fem_worker, ccl_case%time%dt, ierr)
call send_startup_sequence(fem_worker, ccl_case%time%dt, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
do step_id = 1, step_count
call run_empty_step(sph_worker, fem_worker, step_id, ccl_case%time%dt, ierr)
call run_empty_step(sph_worker, fem_worker, step_id, ccl_case%time%dt, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
end do
call send_worker_command(sph_worker, CCL_CMD_WRITE_OUTPUT, step_count, ccl_case%time%dt, ierr)
call send_worker_command(sph_worker, CCL_CMD_WRITE_OUTPUT, step_count, ccl_case%time%dt, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call send_worker_command(fem_worker, CCL_CMD_WRITE_OUTPUT, step_count, ccl_case%time%dt, ierr)
call send_worker_command(fem_worker, CCL_CMD_WRITE_OUTPUT, step_count, ccl_case%time%dt, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call stop_worker(sph_worker, ierr)
call stop_worker(sph_worker, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
call stop_worker(fem_worker, ierr)
call stop_worker(fem_worker, diag, ierr)
if (ierr /= CCL_SUCCESS) goto 900
write(msg, '(a,i0)') "empty coupling dry-run completed, steps=", step_count
call ccl_log_info("controller", trim(msg))
call ccl_diagnostics_write_run(diag, "INFO", "controller", trim(msg))
900 continue
if (ierr /= CCL_SUCCESS) then
write(msg, '(a,i0)') "empty coupling failed, ierr=", ierr
call ccl_log_error("controller", trim(msg))
call ccl_diagnostics_write_run(diag, "ERROR", "controller", trim(msg))
call terminate_worker_if_running(sph_worker)
call terminate_worker_if_running(fem_worker)
end if
call close_worker(sph_worker, close_ierr)
call close_worker(fem_worker, close_ierr)
call ccl_diagnostics_write_run(diag, "INFO", "controller", "resources released")
call ccl_diagnostics_close(diag)
end function ccl_controller_run_empty_coupling
subroutine init_worker(worker, ccl_case, solver_slot, ierr)
@@ -200,13 +215,15 @@ contains
call ccl_event_create(worker%ack_event_name, .false., worker%ack_event, ierr)
end subroutine create_worker_resources
subroutine start_worker(worker, ierr)
subroutine start_worker(worker, diag, ierr)
type(CCL_Worker_Runtime), intent(inout) :: worker
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(out) :: ierr
character(len=1024) :: command_line
call build_worker_command_line(worker, command_line)
call ccl_log_info("controller", "starting " // trim(worker%solver_name) // " worker")
call ccl_diagnostics_write_run(diag, "INFO", "worker_command", trim(command_line))
call ccl_process_start(trim(command_line), worker%process, ierr)
worker%started = (ierr == CCL_SUCCESS)
end subroutine start_worker
@@ -226,28 +243,30 @@ contains
call ccl_log_info("controller", trim(worker%solver_name) // " worker ready")
end subroutine wait_worker_ready
subroutine send_startup_sequence(worker, dt, ierr)
subroutine send_startup_sequence(worker, dt, diag, ierr)
type(CCL_Worker_Runtime), intent(inout) :: worker
real(8), intent(in) :: dt
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(out) :: ierr
call send_worker_command(worker, CCL_CMD_INIT, 0, dt, ierr)
call send_worker_command(worker, CCL_CMD_INIT, 0, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(worker, CCL_CMD_READ_INPUT, 0, dt, ierr)
call send_worker_command(worker, CCL_CMD_READ_INPUT, 0, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(worker, CCL_CMD_BUILD_MODEL, 0, dt, ierr)
call send_worker_command(worker, CCL_CMD_BUILD_MODEL, 0, dt, diag, ierr)
end subroutine send_startup_sequence
subroutine run_empty_step(sph_worker, fem_worker, step_id, dt, ierr)
subroutine run_empty_step(sph_worker, fem_worker, step_id, dt, diag, ierr)
type(CCL_Worker_Runtime), intent(inout) :: sph_worker
type(CCL_Worker_Runtime), intent(inout) :: fem_worker
integer, intent(in) :: step_id
real(8), intent(in) :: dt
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(out) :: ierr
call send_worker_command(sph_worker, CCL_CMD_EXPORT_INTERFACE, step_id, dt, ierr)
call send_worker_command(sph_worker, CCL_CMD_EXPORT_INTERFACE, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(fem_worker, CCL_CMD_EXPORT_INTERFACE, step_id, dt, ierr)
call send_worker_command(fem_worker, CCL_CMD_EXPORT_INTERFACE, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call validate_export_header(sph_worker, step_id, ierr)
@@ -260,41 +279,60 @@ contains
call prepare_empty_load(fem_worker, step_id, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(sph_worker, CCL_CMD_IMPORT_LOADS, step_id, dt, ierr)
call send_worker_command(sph_worker, CCL_CMD_IMPORT_LOADS, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(fem_worker, CCL_CMD_IMPORT_LOADS, step_id, dt, ierr)
call send_worker_command(fem_worker, CCL_CMD_IMPORT_LOADS, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(sph_worker, CCL_CMD_ADVANCE, step_id, dt, ierr)
call send_worker_command(sph_worker, CCL_CMD_ADVANCE, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call send_worker_command(fem_worker, CCL_CMD_ADVANCE, step_id, dt, ierr)
call send_worker_command(fem_worker, CCL_CMD_ADVANCE, step_id, dt, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_diagnostics_write_force_balance(diag, step_id)
end subroutine run_empty_step
subroutine send_worker_command(worker, command_id, step_id, dt, ierr)
subroutine send_worker_command(worker, command_id, step_id, dt, diag, ierr)
type(CCL_Worker_Runtime), intent(inout) :: worker
integer(c_int32_t), intent(in) :: command_id
integer, intent(in) :: step_id
real(8), intent(in) :: dt
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(out) :: ierr
logical :: is_running
integer :: process_ierr
real(8) :: start_time
real(8) :: end_time
real(8) :: elapsed_ms
call cpu_time(start_time)
call ccl_event_reset(worker%ack_event, ierr)
if (ierr /= CCL_SUCCESS) return
if (ierr /= CCL_SUCCESS) goto 800
call ccl_command_dispatch(worker%command, command_id, int(step_id, c_int64_t), &
real(dt, c_double), ierr)
if (ierr /= CCL_SUCCESS) return
if (ierr /= CCL_SUCCESS) goto 800
call ccl_event_signal(worker%command_event, ierr)
if (ierr /= CCL_SUCCESS) return
if (ierr /= CCL_SUCCESS) goto 800
call ccl_event_wait(worker%ack_event, worker%timeout_ms, ierr)
if (ierr == CCL_ERROR_IPC_TIMEOUT) then
call ccl_process_is_running(worker%process, is_running, ierr)
if (ierr == CCL_SUCCESS .and. .not. is_running) ierr = CCL_ERROR_WORKER_STOP_FAILED
return
call ccl_process_is_running(worker%process, is_running, process_ierr)
if (process_ierr == CCL_SUCCESS .and. .not. is_running) then
ierr = CCL_ERROR_WORKER_STOP_FAILED
else
ierr = CCL_ERROR_IPC_TIMEOUT
end if
goto 800
end if
if (ierr /= CCL_SUCCESS) return
if (ierr /= CCL_SUCCESS) goto 800
call ccl_command_check_ack(worker%command, ierr)
if (ierr /= CCL_SUCCESS) return
if (ierr /= CCL_SUCCESS) goto 800
if (worker%command%error_code /= CCL_SUCCESS) ierr = worker%command%error_code
800 continue
call cpu_time(end_time)
elapsed_ms = max(0.0d0, (end_time - start_time) * 1000.0d0)
call ccl_diagnostics_write_ipc(diag, int(worker%command%step_id), int(worker%solver_id), &
int(command_id), worker%command%command_seq, worker%command%ack_seq, &
int(worker%command%worker_state), worker%command%heartbeat_seq, elapsed_ms, ierr)
end subroutine send_worker_command
subroutine validate_export_header(worker, step_id, ierr)
@@ -331,14 +369,15 @@ contains
call ccl_data_header_set_state(worker%load_header, CCL_BUFFER_READY, ierr)
end subroutine prepare_empty_load
subroutine stop_worker(worker, ierr)
subroutine stop_worker(worker, diag, ierr)
type(CCL_Worker_Runtime), intent(inout) :: worker
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(out) :: ierr
integer :: exit_code
ierr = CCL_SUCCESS
if (.not. worker%started) return
call send_worker_command(worker, CCL_CMD_STOP, int(worker%command%step_id), 0.0d0, ierr)
call send_worker_command(worker, CCL_CMD_STOP, int(worker%command%step_id), 0.0d0, diag, ierr)
if (ierr /= CCL_SUCCESS) return
call ccl_process_wait(worker%process, worker%timeout_ms, exit_code, ierr)
if (ierr /= CCL_SUCCESS) return
@@ -346,6 +385,22 @@ contains
worker%started = .false.
end subroutine stop_worker
subroutine log_worker_resources(diag, worker)
type(CCL_Diagnostics_Type), intent(inout) :: diag
type(CCL_Worker_Runtime), intent(in) :: worker
call ccl_diagnostics_write_run(diag, "INFO", "ipc", trim(worker%solver_name) // &
" command=" // trim(worker%command_name))
call ccl_diagnostics_write_run(diag, "INFO", "ipc", trim(worker%solver_name) // &
" command_event=" // trim(worker%command_event_name))
call ccl_diagnostics_write_run(diag, "INFO", "ipc", trim(worker%solver_name) // &
" ack_event=" // trim(worker%ack_event_name))
call ccl_diagnostics_write_run(diag, "INFO", "ipc", trim(worker%solver_name) // &
" export=" // trim(worker%export_name))
call ccl_diagnostics_write_run(diag, "INFO", "ipc", trim(worker%solver_name) // &
" load=" // trim(worker%load_name))
end subroutine log_worker_resources
subroutine terminate_worker_if_running(worker)
type(CCL_Worker_Runtime), intent(inout) :: worker
integer :: ierr
+149
View File
@@ -0,0 +1,149 @@
module ccl_diagnostics
use iso_fortran_env, only: error_unit
use ccl_errors, only: CCL_ERROR_SYSTEM_INTERNAL, CCL_SUCCESS
use ccl_version, only: CCL_VERSION_LABEL
implicit none
type :: CCL_Diagnostics_Type
character(len=512) :: output_dir = ""
integer :: log_unit = -1
integer :: ipc_unit = -1
integer :: force_unit = -1
logical :: is_open = .false.
end type CCL_Diagnostics_Type
contains
subroutine ccl_diagnostics_open(diag, output_dir, case_id, input_file, ierr)
type(CCL_Diagnostics_Type), intent(out) :: diag
character(len=*), intent(in) :: output_dir
character(len=*), intent(in) :: case_id
character(len=*), intent(in) :: input_file
integer, intent(out) :: ierr
integer :: ios
diag = CCL_Diagnostics_Type()
diag%output_dir = trim(output_dir)
call ensure_directory(diag%output_dir, ierr)
if (ierr /= CCL_SUCCESS) return
open(newunit=diag%log_unit, file=join_path(diag%output_dir, "run.log"), status="replace", &
action="write", encoding="UTF-8", iostat=ios)
if (ios /= 0) then
ierr = CCL_ERROR_SYSTEM_INTERNAL
return
end if
open(newunit=diag%ipc_unit, file=join_path(diag%output_dir, "ipc_report.csv"), status="replace", &
action="write", encoding="UTF-8", iostat=ios)
if (ios /= 0) then
ierr = CCL_ERROR_SYSTEM_INTERNAL
return
end if
open(newunit=diag%force_unit, file=join_path(diag%output_dir, "force_balance.csv"), status="replace", &
action="write", encoding="UTF-8", iostat=ios)
if (ios /= 0) then
ierr = CCL_ERROR_SYSTEM_INTERNAL
return
end if
diag%is_open = .true.
write(diag%ipc_unit, '(a)') "step_id,solver_id,command_id,command_seq,ack_seq,worker_state," // &
"heartbeat_seq,elapsed_ms,error_code"
write(diag%force_unit, '(a)') "step_id,sph_fx,sph_fy,sph_fz,fem_fx,fem_fy,fem_fz,total_fx,total_fy,total_fz"
call ccl_diagnostics_write_run(diag, "INFO", "run", "version=" // CCL_VERSION_LABEL)
call ccl_diagnostics_write_run(diag, "INFO", "run", "case_id=" // trim(case_id))
call ccl_diagnostics_write_run(diag, "INFO", "run", "input=" // trim(input_file))
ierr = CCL_SUCCESS
end subroutine ccl_diagnostics_open
subroutine ccl_diagnostics_write_run(diag, level, module_name, message)
type(CCL_Diagnostics_Type), intent(inout) :: diag
character(len=*), intent(in) :: level
character(len=*), intent(in) :: module_name
character(len=*), intent(in) :: message
if (.not. diag%is_open .or. diag%log_unit == -1) return
write(diag%log_unit, '(a)') "[" // trim(level) // "] [" // trim(module_name) // "] " // trim(message)
flush(diag%log_unit)
end subroutine ccl_diagnostics_write_run
subroutine ccl_diagnostics_write_ipc(diag, step_id, solver_id, command_id, command_seq, ack_seq, &
worker_state, heartbeat_seq, elapsed_ms, error_code)
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(in) :: step_id
integer, intent(in) :: solver_id
integer, intent(in) :: command_id
integer(8), intent(in) :: command_seq
integer(8), intent(in) :: ack_seq
integer, intent(in) :: worker_state
integer(8), intent(in) :: heartbeat_seq
real(8), intent(in) :: elapsed_ms
integer, intent(in) :: error_code
if (.not. diag%is_open .or. diag%ipc_unit == -1) return
write(diag%ipc_unit, '(i0,",",i0,",",i0,",",i0,",",i0,",",i0,",",i0,",",f10.3,",",i0)') &
step_id, solver_id, command_id, command_seq, ack_seq, worker_state, heartbeat_seq, &
elapsed_ms, error_code
flush(diag%ipc_unit)
end subroutine ccl_diagnostics_write_ipc
subroutine ccl_diagnostics_write_force_balance(diag, step_id)
type(CCL_Diagnostics_Type), intent(inout) :: diag
integer, intent(in) :: step_id
if (.not. diag%is_open .or. diag%force_unit == -1) return
write(diag%force_unit, '(i0,9(",",es16.8))') step_id, 0.0d0, 0.0d0, 0.0d0, &
0.0d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0
flush(diag%force_unit)
end subroutine ccl_diagnostics_write_force_balance
subroutine ccl_diagnostics_close(diag)
type(CCL_Diagnostics_Type), intent(inout) :: diag
if (diag%log_unit /= -1) close(diag%log_unit)
if (diag%ipc_unit /= -1) close(diag%ipc_unit)
if (diag%force_unit /= -1) close(diag%force_unit)
diag%log_unit = -1
diag%ipc_unit = -1
diag%force_unit = -1
diag%is_open = .false.
end subroutine ccl_diagnostics_close
subroutine ensure_directory(path, ierr)
character(len=*), intent(in) :: path
integer, intent(out) :: ierr
integer :: cmdstat
integer :: exitstat
character(len=1200) :: command
if (len_trim(path) == 0) then
ierr = CCL_ERROR_SYSTEM_INTERNAL
return
end if
command = 'cmd /c if not exist "' // trim(path) // '" mkdir "' // trim(path) // '"'
call execute_command_line(trim(command), exitstat=exitstat, cmdstat=cmdstat)
if (cmdstat /= 0 .or. exitstat /= 0) then
write(error_unit, '(a)') "failed to create output directory: " // trim(path)
ierr = CCL_ERROR_SYSTEM_INTERNAL
return
end if
ierr = CCL_SUCCESS
end subroutine ensure_directory
character(len=1024) function join_path(directory, leaf) result(path)
character(len=*), intent(in) :: directory
character(len=*), intent(in) :: leaf
integer :: n
n = len_trim(directory)
if (n > 0 .and. (directory(n:n) == "/" .or. directory(n:n) == "\")) then
path = trim(directory) // trim(leaf)
else
path = trim(directory) // "/" // trim(leaf)
end if
end function join_path
end module ccl_diagnostics