From 69ae2a1c9ceac7c5a7ce7ed02253362b8a8415dc Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 5 Jul 2026 16:54:31 +0800 Subject: [PATCH] Implement CCLite 0.1.4 data block --- CMakeLists.txt | 9 +- src/common/ccl_version.f90 | 4 +- src/interface_data/ccl_data_block.f90 | 407 ++++++++++++++++++++++++++ tests/unit/test_data_block.f90 | 155 ++++++++++ todo.md | 18 +- 5 files changed, 581 insertions(+), 12 deletions(-) create mode 100644 src/interface_data/ccl_data_block.f90 create mode 100644 tests/unit/test_data_block.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b8dca9..062f716 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) -project(CCLite VERSION 0.1.3 LANGUAGES C Fortran) +project(CCLite VERSION 0.1.4 LANGUAGES C Fortran) set(CMAKE_Fortran_STANDARD 2018) set(CMAKE_Fortran_STANDARD_REQUIRED ON) @@ -15,6 +15,7 @@ set(CCL_COMMON_SOURCES src/case/ccl_case_types.f90 src/case/ccl_inp_parser.f90 src/interface_data/ccl_command_block.f90 + src/interface_data/ccl_data_block.f90 ) add_library(ccl_common STATIC ${CCL_COMMON_SOURCES}) @@ -57,6 +58,11 @@ add_executable(test_command_block ) target_link_libraries(test_command_block PRIVATE ccl_common) +add_executable(test_data_block + tests/unit/test_data_block.f90 +) +target_link_libraries(test_data_block PRIVATE ccl_common) + add_test(NAME smoke_cclite_version COMMAND cclite --version) add_test(NAME smoke_cclite_help COMMAND cclite --help) add_test(NAME smoke_mock_sph_worker_version COMMAND mock_sph_worker --version) @@ -78,3 +84,4 @@ set_tests_properties(check_invalid_ipc_type PROPERTIES WILL_FAIL TRUE) add_test(NAME ipc_lifecycle COMMAND test_ipc_lifecycle) add_test(NAME command_block_semantics COMMAND test_command_block) +add_test(NAME data_block_semantics COMMAND test_data_block) diff --git a/src/common/ccl_version.f90 b/src/common/ccl_version.f90 index af31aa4..612fe7c 100644 --- a/src/common/ccl_version.f90 +++ b/src/common/ccl_version.f90 @@ -3,8 +3,8 @@ module ccl_version integer, parameter :: CCL_VERSION_MAJOR = 0 integer, parameter :: CCL_VERSION_MINOR = 1 - integer, parameter :: CCL_VERSION_PATCH = 3 - character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.3" + integer, parameter :: CCL_VERSION_PATCH = 4 + character(len=*), parameter :: CCL_VERSION_LABEL = "0.1.4" contains diff --git a/src/interface_data/ccl_data_block.f90 b/src/interface_data/ccl_data_block.f90 new file mode 100644 index 0000000..7740bc8 --- /dev/null +++ b/src/interface_data/ccl_data_block.f90 @@ -0,0 +1,407 @@ +module ccl_data_block + use iso_c_binding, only: c_char, c_double, c_int32_t, c_int64_t, c_sizeof + use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_SUCCESS + implicit none + + integer(c_int32_t), parameter :: CCL_DATA_MAGIC = 1128488004_c_int32_t + integer(c_int32_t), parameter :: CCL_DATA_VERSION_MAJOR = 0_c_int32_t + integer(c_int32_t), parameter :: CCL_DATA_VERSION_MINOR = 1_c_int32_t + + integer(c_int32_t), parameter :: CCL_LAYOUT_AOR_V1 = 1_c_int32_t + integer(c_int32_t), parameter :: CCL_LAYOUT_SOA_V2 = 2_c_int32_t + + integer(c_int32_t), parameter :: CCL_ROLE_SPH_TO_CCL_PARTICLE = 101_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_SPH_TO_CCL_FACET = 102_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_FEM_TO_CCL_FACET = 201_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_CCL_TO_SPH_FORCE = 301_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_CCL_TO_FEM_LOAD = 302_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_CCL_DIAGNOSTIC = 401_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_CCL_COMMAND_SPH = 901_c_int32_t + integer(c_int32_t), parameter :: CCL_ROLE_CCL_COMMAND_FEM = 902_c_int32_t + + integer(c_int32_t), parameter :: CCL_BUFFER_EMPTY = 0_c_int32_t + integer(c_int32_t), parameter :: CCL_BUFFER_WRITING = 1_c_int32_t + integer(c_int32_t), parameter :: CCL_BUFFER_READY = 2_c_int32_t + integer(c_int32_t), parameter :: CCL_BUFFER_READING = 3_c_int32_t + integer(c_int32_t), parameter :: CCL_BUFFER_CONSUMED = 4_c_int32_t + integer(c_int32_t), parameter :: CCL_BUFFER_ERROR = 5_c_int32_t + + type, bind(C) :: CCL_Data_Block_Header_Type + integer(c_int32_t) :: magic + integer(c_int32_t) :: version_major + integer(c_int32_t) :: version_minor + integer(c_int32_t) :: layout_id + integer(c_int32_t) :: block_role + integer(c_int32_t) :: producer_id + integer(c_int32_t) :: consumer_id + integer(c_int32_t) :: step_id + integer(c_int32_t) :: buffer_id + integer(c_int32_t) :: state + integer(c_int32_t) :: item_count + integer(c_int32_t) :: item_stride + integer(c_int64_t) :: payload_bytes + integer(c_int32_t) :: header_bytes + integer(c_int32_t) :: max_item_count + integer(c_int32_t) :: error_code + integer(c_int32_t) :: reserved_i4(16) + end type CCL_Data_Block_Header_Type + + type, bind(C) :: CCL_SPH_Particle_Record_Type + integer(c_int64_t) :: external_id + integer(c_int32_t) :: internal_index + integer(c_int32_t) :: phase + integer(c_int32_t) :: status + integer(c_int32_t) :: material_id + integer(c_int32_t) :: section_id + integer(c_int32_t) :: valid_flags + integer(c_int32_t) :: reserved_i4(2) + real(c_double) :: mass + real(c_double) :: volume + real(c_double) :: density + real(c_double) :: pressure + real(c_double) :: internal_energy + real(c_double) :: temperature + real(c_double) :: h + real(c_double) :: smoothing_length + real(c_double) :: contact_radius + real(c_double) :: sound_speed + real(c_double) :: x(3) + real(c_double) :: v(3) + real(c_double) :: a(3) + end type CCL_SPH_Particle_Record_Type + + type, bind(C) :: CCL_SPH_Facet_Record_Type + integer(c_int64_t) :: facet_id + integer(c_int32_t) :: particle_count + integer(c_int32_t) :: section_id + integer(c_int32_t) :: material_id + integer(c_int32_t) :: reserved_i4 + integer(c_int64_t) :: particle_ids(8) + real(c_double) :: area + real(c_double) :: normal(3) + real(c_double) :: x(3, 8) + real(c_double) :: v(3, 8) + end type CCL_SPH_Facet_Record_Type + + type, bind(C) :: CCL_FEM_Facet_Record_Type + integer(c_int64_t) :: facet_id + integer(c_int64_t) :: elem_id + integer(c_int32_t) :: face_id + integer(c_int32_t) :: node_count + integer(c_int32_t) :: element_type + integer(c_int32_t) :: surface_type + integer(c_int64_t) :: surface_id + integer(c_int32_t) :: valid_flags + integer(c_int32_t) :: reserved_i4(3) + integer(c_int64_t) :: node_ids(8) + real(c_double) :: area + real(c_double) :: thickness + real(c_double) :: shell_thickness + real(c_double) :: mass_per_area + real(c_double) :: normal(3) + real(c_double) :: x(3, 8) + real(c_double) :: v(3, 8) + end type CCL_FEM_Facet_Record_Type + + type, bind(C) :: CCL_SPH_Force_Record_Type + integer(c_int64_t) :: external_id + integer(c_int32_t) :: internal_index + integer(c_int32_t) :: load_mode + integer(c_int32_t) :: contact_state + integer(c_int32_t) :: coupling_step + integer(c_int32_t) :: valid_flags + integer(c_int32_t) :: reserved_i4 + integer(c_int64_t) :: source_pair_id + integer(c_int64_t) :: source_facet_id + real(c_double) :: force(3) + real(c_double) :: moment(3) + real(c_double) :: normal(3) + real(c_double) :: gap + real(c_double) :: penetration + real(c_double) :: pressure + real(c_double) :: target_motion(3) + end type CCL_SPH_Force_Record_Type + + type, bind(C) :: CCL_FEM_Load_Record_Type + integer(c_int64_t) :: facet_id + integer(c_int64_t) :: source_pair_id + integer(c_int64_t) :: source_particle_id + integer(c_int32_t) :: node_count + integer(c_int32_t) :: load_type + integer(c_int32_t) :: distribution_mode + integer(c_int32_t) :: contact_state + integer(c_int32_t) :: valid_flags + integer(c_int32_t) :: reserved_i4 + integer(c_int64_t) :: node_ids(8) + real(c_double) :: node_forces(3, 8) + real(c_double) :: pressure + real(c_double) :: traction(3) + real(c_double) :: resultant(3) + real(c_double) :: normal(3) + end type CCL_FEM_Load_Record_Type + + type, bind(C) :: CCL_Diagnostic_Record_Type + integer(c_int32_t) :: step_id + integer(c_int32_t) :: source_id + integer(c_int32_t) :: metric_id + integer(c_int32_t) :: reserved_i4 + integer(c_int64_t) :: count_value + real(c_double) :: real_value + character(kind=c_char) :: message(128) + end type CCL_Diagnostic_Record_Type + +contains + + subroutine ccl_data_header_init(header, block_role, producer_id, consumer_id, max_item_count) + type(CCL_Data_Block_Header_Type), intent(out) :: header + integer(c_int32_t), intent(in) :: block_role + integer(c_int32_t), intent(in) :: producer_id + integer(c_int32_t), intent(in) :: consumer_id + integer(c_int32_t), intent(in) :: max_item_count + + header%magic = CCL_DATA_MAGIC + header%version_major = CCL_DATA_VERSION_MAJOR + header%version_minor = CCL_DATA_VERSION_MINOR + header%layout_id = CCL_LAYOUT_AOR_V1 + header%block_role = block_role + header%producer_id = producer_id + header%consumer_id = consumer_id + header%step_id = 0_c_int32_t + header%buffer_id = 0_c_int32_t + header%state = CCL_BUFFER_EMPTY + header%item_count = 0_c_int32_t + header%item_stride = ccl_data_record_size_for_role(block_role) + header%payload_bytes = 0_c_int64_t + header%header_bytes = ccl_data_header_size() + header%max_item_count = max(0_c_int32_t, max_item_count) + header%error_code = CCL_SUCCESS + header%reserved_i4 = 0_c_int32_t + end subroutine ccl_data_header_init + + subroutine ccl_data_header_validate(header, ierr) + type(CCL_Data_Block_Header_Type), intent(in) :: header + integer, intent(out) :: ierr + + integer(c_int32_t) :: expected_stride + integer(c_int64_t) :: expected_payload + integer(c_int64_t) :: max_payload + + ierr = CCL_SUCCESS + if (header%magic /= CCL_DATA_MAGIC) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%version_major /= CCL_DATA_VERSION_MAJOR) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%layout_id /= CCL_LAYOUT_AOR_V1) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (.not. ccl_data_role_is_valid(header%block_role)) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (.not. ccl_data_state_is_valid(header%state)) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%header_bytes /= ccl_data_header_size()) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%item_count < 0_c_int32_t .or. header%max_item_count < 0_c_int32_t) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%item_count > header%max_item_count) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + + expected_stride = ccl_data_record_size_for_role(header%block_role) + if (header%item_stride /= expected_stride) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + + expected_payload = int(header%item_count, c_int64_t) * int(header%item_stride, c_int64_t) + max_payload = int(header%max_item_count, c_int64_t) * int(header%item_stride, c_int64_t) + if (header%payload_bytes /= expected_payload) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + if (header%payload_bytes < 0_c_int64_t .or. header%payload_bytes > max_payload) then + ierr = CCL_ERROR_IPC_BAD_HEADER + end if + end subroutine ccl_data_header_validate + + subroutine ccl_data_header_set_count(header, item_count, ierr) + type(CCL_Data_Block_Header_Type), intent(inout) :: header + integer(c_int32_t), intent(in) :: item_count + integer, intent(out) :: ierr + + ierr = CCL_SUCCESS + if (item_count < 0_c_int32_t .or. item_count > header%max_item_count) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + + header%item_count = item_count + header%payload_bytes = int(item_count, c_int64_t) * int(header%item_stride, c_int64_t) + call ccl_data_header_validate(header, ierr) + end subroutine ccl_data_header_set_count + + subroutine ccl_data_header_set_state(header, state, ierr) + type(CCL_Data_Block_Header_Type), intent(inout) :: header + integer(c_int32_t), intent(in) :: state + integer, intent(out) :: ierr + + ierr = CCL_SUCCESS + if (.not. ccl_data_state_is_valid(state)) then + ierr = CCL_ERROR_IPC_BAD_HEADER + return + end if + + header%state = state + call ccl_data_header_validate(header, ierr) + end subroutine ccl_data_header_set_state + + subroutine ccl_data_header_pack(src, dst, ierr) + type(CCL_Data_Block_Header_Type), intent(in) :: src + type(CCL_Data_Block_Header_Type), intent(out) :: dst + integer, intent(out) :: ierr + + call ccl_data_header_validate(src, ierr) + if (ierr == CCL_SUCCESS) dst = src + end subroutine ccl_data_header_pack + + subroutine ccl_data_header_unpack(src, dst, ierr) + type(CCL_Data_Block_Header_Type), intent(in) :: src + type(CCL_Data_Block_Header_Type), intent(out) :: dst + integer, intent(out) :: ierr + + dst = src + call ccl_data_header_validate(dst, ierr) + end subroutine ccl_data_header_unpack + + logical function ccl_data_role_is_valid(block_role) result(valid) + integer(c_int32_t), intent(in) :: block_role + + select case (block_role) + case (CCL_ROLE_SPH_TO_CCL_PARTICLE, CCL_ROLE_SPH_TO_CCL_FACET, & + CCL_ROLE_FEM_TO_CCL_FACET, CCL_ROLE_CCL_TO_SPH_FORCE, & + CCL_ROLE_CCL_TO_FEM_LOAD, CCL_ROLE_CCL_DIAGNOSTIC, & + CCL_ROLE_CCL_COMMAND_SPH, CCL_ROLE_CCL_COMMAND_FEM) + valid = .true. + case default + valid = .false. + end select + end function ccl_data_role_is_valid + + logical function ccl_data_state_is_valid(state) result(valid) + integer(c_int32_t), intent(in) :: state + + select case (state) + case (CCL_BUFFER_EMPTY, CCL_BUFFER_WRITING, CCL_BUFFER_READY, CCL_BUFFER_READING, & + CCL_BUFFER_CONSUMED, CCL_BUFFER_ERROR) + valid = .true. + case default + valid = .false. + end select + end function ccl_data_state_is_valid + + logical function ccl_data_state_transition_is_valid(from_state, to_state) result(valid) + integer(c_int32_t), intent(in) :: from_state + integer(c_int32_t), intent(in) :: to_state + + valid = .false. + select case (from_state) + case (CCL_BUFFER_EMPTY) + valid = (to_state == CCL_BUFFER_WRITING .or. to_state == CCL_BUFFER_ERROR) + case (CCL_BUFFER_WRITING) + valid = (to_state == CCL_BUFFER_READY .or. to_state == CCL_BUFFER_ERROR) + case (CCL_BUFFER_READY) + valid = (to_state == CCL_BUFFER_READING .or. to_state == CCL_BUFFER_ERROR) + case (CCL_BUFFER_READING) + valid = (to_state == CCL_BUFFER_CONSUMED .or. to_state == CCL_BUFFER_ERROR) + case (CCL_BUFFER_CONSUMED, CCL_BUFFER_ERROR) + valid = (to_state == CCL_BUFFER_EMPTY) + case default + valid = .false. + end select + end function ccl_data_state_transition_is_valid + + integer(c_int32_t) function ccl_data_record_size_for_role(block_role) result(byte_size) + integer(c_int32_t), intent(in) :: block_role + + select case (block_role) + case (CCL_ROLE_SPH_TO_CCL_PARTICLE) + byte_size = ccl_sph_particle_record_size() + case (CCL_ROLE_SPH_TO_CCL_FACET) + byte_size = ccl_sph_facet_record_size() + case (CCL_ROLE_FEM_TO_CCL_FACET) + byte_size = ccl_fem_facet_record_size() + case (CCL_ROLE_CCL_TO_SPH_FORCE) + byte_size = ccl_sph_force_record_size() + case (CCL_ROLE_CCL_TO_FEM_LOAD) + byte_size = ccl_fem_load_record_size() + case (CCL_ROLE_CCL_DIAGNOSTIC) + byte_size = ccl_diagnostic_record_size() + case (CCL_ROLE_CCL_COMMAND_SPH, CCL_ROLE_CCL_COMMAND_FEM) + byte_size = 0_c_int32_t + case default + byte_size = -1_c_int32_t + end select + end function ccl_data_record_size_for_role + + integer(c_int64_t) function ccl_data_payload_capacity_bytes(header) result(byte_size) + type(CCL_Data_Block_Header_Type), intent(in) :: header + + byte_size = int(header%max_item_count, c_int64_t) * int(header%item_stride, c_int64_t) + end function ccl_data_payload_capacity_bytes + + integer(c_int32_t) function ccl_data_header_size() result(byte_size) + type(CCL_Data_Block_Header_Type) :: header + + byte_size = int(c_sizeof(header), c_int32_t) + end function ccl_data_header_size + + integer(c_int32_t) function ccl_sph_particle_record_size() result(byte_size) + type(CCL_SPH_Particle_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_sph_particle_record_size + + integer(c_int32_t) function ccl_sph_facet_record_size() result(byte_size) + type(CCL_SPH_Facet_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_sph_facet_record_size + + integer(c_int32_t) function ccl_fem_facet_record_size() result(byte_size) + type(CCL_FEM_Facet_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_fem_facet_record_size + + integer(c_int32_t) function ccl_sph_force_record_size() result(byte_size) + type(CCL_SPH_Force_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_sph_force_record_size + + integer(c_int32_t) function ccl_fem_load_record_size() result(byte_size) + type(CCL_FEM_Load_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_fem_load_record_size + + integer(c_int32_t) function ccl_diagnostic_record_size() result(byte_size) + type(CCL_Diagnostic_Record_Type) :: record + + byte_size = int(c_sizeof(record), c_int32_t) + end function ccl_diagnostic_record_size + +end module ccl_data_block diff --git a/tests/unit/test_data_block.f90 b/tests/unit/test_data_block.f90 new file mode 100644 index 0000000..e3ed433 --- /dev/null +++ b/tests/unit/test_data_block.f90 @@ -0,0 +1,155 @@ +program test_data_block + use iso_c_binding, only: c_int32_t, c_int64_t + use ccl_data_block, only: CCL_BUFFER_CONSUMED, CCL_BUFFER_EMPTY, CCL_BUFFER_ERROR, & + CCL_BUFFER_READY, CCL_BUFFER_READING, CCL_BUFFER_WRITING, CCL_DATA_MAGIC, & + CCL_Data_Block_Header_Type, CCL_LAYOUT_SOA_V2, CCL_ROLE_CCL_DIAGNOSTIC, & + CCL_ROLE_CCL_TO_FEM_LOAD, CCL_ROLE_CCL_TO_SPH_FORCE, CCL_ROLE_FEM_TO_CCL_FACET, & + CCL_ROLE_SPH_TO_CCL_PARTICLE, ccl_data_header_init, ccl_data_header_pack, & + ccl_data_header_set_count, ccl_data_header_set_state, ccl_data_header_size, & + ccl_data_header_unpack, ccl_data_header_validate, ccl_data_payload_capacity_bytes, & + ccl_data_record_size_for_role, ccl_data_role_is_valid, ccl_data_state_transition_is_valid, & + ccl_diagnostic_record_size, ccl_fem_facet_record_size, ccl_fem_load_record_size, & + ccl_sph_force_record_size, ccl_sph_particle_record_size + use ccl_errors, only: CCL_ERROR_IPC_BAD_HEADER, CCL_SUCCESS + implicit none + + integer :: ierr + integer(c_int32_t) :: particle_stride + integer(c_int32_t) :: fem_stride + integer(c_int32_t) :: force_stride + integer(c_int32_t) :: load_stride + type(CCL_Data_Block_Header_Type) :: header + type(CCL_Data_Block_Header_Type) :: packed + type(CCL_Data_Block_Header_Type) :: unpacked + + particle_stride = ccl_sph_particle_record_size() + fem_stride = ccl_fem_facet_record_size() + force_stride = ccl_sph_force_record_size() + load_stride = ccl_fem_load_record_size() + + call require_true(ccl_data_header_size() > 0_c_int32_t, "header has positive byte size") + call require_true(particle_stride > 0_c_int32_t, "particle record has positive byte size") + call require_true(fem_stride > 0_c_int32_t, "fem facet record has positive byte size") + call require_true(force_stride > 0_c_int32_t, "sph force record has positive byte size") + call require_true(load_stride > 0_c_int32_t, "fem load record has positive byte size") + call require_true(ccl_diagnostic_record_size() > 0_c_int32_t, "diagnostic record has positive byte size") + + call require_true(ccl_data_role_is_valid(CCL_ROLE_CCL_DIAGNOSTIC), "diagnostic role valid") + call require_equal(ccl_data_record_size_for_role(CCL_ROLE_SPH_TO_CCL_PARTICLE), particle_stride, & + "particle role stride") + call require_equal(ccl_data_record_size_for_role(CCL_ROLE_FEM_TO_CCL_FACET), fem_stride, & + "fem role stride") + + call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t) + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_SUCCESS, "fresh header validates") + call require_equal(header%magic, CCL_DATA_MAGIC, "magic initialized") + call require_equal(header%item_stride, particle_stride, "stride initialized") + call require_equal_int64(header%payload_bytes, 0_c_int64_t, "empty payload initialized") + call require_equal_int64(ccl_data_payload_capacity_bytes(header), int(4 * particle_stride, c_int64_t), & + "capacity bytes computed") + + call ccl_data_header_set_state(header, CCL_BUFFER_WRITING, ierr) + call require_equal(ierr, CCL_SUCCESS, "set writing state") + call ccl_data_header_set_count(header, 3_c_int32_t, ierr) + call require_equal(ierr, CCL_SUCCESS, "set fixed payload count") + call ccl_data_header_set_state(header, CCL_BUFFER_READY, ierr) + call require_equal(ierr, CCL_SUCCESS, "set ready state") + call require_equal_int64(header%payload_bytes, int(3 * particle_stride, c_int64_t), & + "payload bytes match count and stride") + + call ccl_data_header_pack(header, packed, ierr) + call require_equal(ierr, CCL_SUCCESS, "pack header") + call ccl_data_header_unpack(packed, unpacked, ierr) + call require_equal(ierr, CCL_SUCCESS, "unpack header") + call require_equal(unpacked%item_count, 3_c_int32_t, "unpacked count preserved") + call require_equal_int64(unpacked%payload_bytes, header%payload_bytes, "unpacked payload preserved") + + call ccl_data_header_set_count(header, 4_c_int32_t, ierr) + call require_equal(ierr, CCL_SUCCESS, "max capacity accepted") + call ccl_data_header_set_count(header, 5_c_int32_t, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "count over capacity rejected") + + call ccl_data_header_init(header, CCL_ROLE_CCL_TO_SPH_FORCE, 3_c_int32_t, 1_c_int32_t, 0_c_int32_t) + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_SUCCESS, "empty sph force block validates") + call require_equal(header%item_stride, force_stride, "empty sph force stride reserved") + + call ccl_data_header_init(header, CCL_ROLE_CCL_TO_FEM_LOAD, 3_c_int32_t, 2_c_int32_t, 0_c_int32_t) + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_SUCCESS, "empty fem load block validates") + call require_equal(header%item_stride, load_stride, "empty fem load stride reserved") + + call ccl_data_header_init(header, CCL_ROLE_FEM_TO_CCL_FACET, 2_c_int32_t, 3_c_int32_t, 1_c_int32_t) + call ccl_data_header_set_count(header, 1_c_int32_t, ierr) + call require_equal(ierr, CCL_SUCCESS, "fem facet payload accepted") + header%item_stride = header%item_stride + 8_c_int32_t + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad record stride rejected") + + call ccl_data_header_init(header, 999_c_int32_t, 1_c_int32_t, 3_c_int32_t, 0_c_int32_t) + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad role rejected") + + call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t) + header%layout_id = CCL_LAYOUT_SOA_V2 + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "reserved soa layout rejected in 0.1.x") + + call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t) + header%payload_bytes = 8_c_int64_t + call ccl_data_header_validate(header, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad payload bytes rejected") + + call ccl_data_header_init(header, CCL_ROLE_SPH_TO_CCL_PARTICLE, 1_c_int32_t, 3_c_int32_t, 4_c_int32_t) + call ccl_data_header_set_state(header, 999_c_int32_t, ierr) + call require_equal(ierr, CCL_ERROR_IPC_BAD_HEADER, "bad buffer state rejected") + + call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_EMPTY, CCL_BUFFER_WRITING), & + "empty to writing valid") + call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_WRITING, CCL_BUFFER_READY), & + "writing to ready valid") + call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_READY, CCL_BUFFER_READING), & + "ready to reading valid") + call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_READING, CCL_BUFFER_CONSUMED), & + "reading to consumed valid") + call require_true(ccl_data_state_transition_is_valid(CCL_BUFFER_ERROR, CCL_BUFFER_EMPTY), & + "error to empty valid") + call require_true(.not. ccl_data_state_transition_is_valid(CCL_BUFFER_READY, CCL_BUFFER_EMPTY), & + "ready to empty invalid") + +contains + + subroutine require_equal(actual, expected, message) + integer, intent(in) :: actual + integer, intent(in) :: expected + character(len=*), intent(in) :: message + + if (actual /= expected) then + print '(a,": expected ",i0," got ",i0)', trim(message), expected, actual + stop 1 + end if + end subroutine require_equal + + subroutine require_equal_int64(actual, expected, message) + integer(c_int64_t), intent(in) :: actual + integer(c_int64_t), intent(in) :: expected + character(len=*), intent(in) :: message + + if (actual /= expected) then + print '(a,": int64 mismatch")', trim(message) + stop 1 + end if + end subroutine require_equal_int64 + + subroutine require_true(condition, message) + logical, intent(in) :: condition + character(len=*), intent(in) :: message + + if (.not. condition) then + print '(a)', trim(message) + stop 1 + end if + end subroutine require_true + +end program test_data_block diff --git a/todo.md b/todo.md index 0dd6210..75624cb 100644 --- a/todo.md +++ b/todo.md @@ -154,31 +154,31 @@ 目标:实现 data block header、block role 和基础 AoR v1 payload。 -- [ ] T0141 定义 data block header。 +- [x] T0141 定义 data block header。 完成判据:包含 magic、version、role、layout、record size、count、capacity、state 和 step id。 -- [ ] T0142 定义 block role 枚举。 +- [x] T0142 定义 block role 枚举。 完成判据:支持 `SPH_TO_CCL_PARTICLE`、`FEM_TO_CCL_FACET`、`CCL_DIAGNOSTIC`、`CCL_COMMAND_SPH`、`CCL_COMMAND_FEM`。 -- [ ] T0143 定义 buffer state。 +- [x] T0143 定义 buffer state。 完成判据:支持 empty、writing、ready、reading、error 状态转换。 -- [ ] T0144 实现 header pack/unpack。 +- [x] T0144 实现 header pack/unpack。 完成判据:共享内存中 header 可由 CCLite 和 worker 双向读写。 -- [ ] T0145 实现 header 校验。 +- [x] T0145 实现 header 校验。 完成判据:非法 role、record size、count 超限和 payload bytes 超限可被拒绝。 -- [ ] T0146 定义 SPH particle AoR v1 record。 +- [x] T0146 定义 SPH particle AoR v1 record。 完成判据:字段覆盖 id、index、position、velocity、mass、density、h、radius、pressure、sound speed 和 flags。 -- [ ] T0147 定义 FEM facet AoR v1 record。 +- [x] T0147 定义 FEM facet AoR v1 record。 完成判据:字段覆盖 facet id、surface id、node ids、coords、velocity、normal、area、thickness、mass per area 和 flags。 -- [ ] T0148 定义空载荷 record 预留。 +- [x] T0148 定义空载荷 record 预留。 完成判据:0.1.x 可创建 `CCL_TO_SPH_FORCE` 和 `CCL_TO_FEM_LOAD` 空 block。 -- [ ] T0149 增加 data block 单元测试。 +- [x] T0149 增加 data block 单元测试。 完成判据:覆盖空 payload、固定 record、最大 capacity、bad header 和 bad count。 # 7. 0.1.5 Controller 空耦合