From e3e6989d4dc2790b1d565c699de4c97b3ce1c6d9 Mon Sep 17 00:00:00 2001 From: shenw Date: Mon, 22 Jun 2026 17:52:59 +0800 Subject: [PATCH] Release FemLite 0.1.0 acceptance baseline --- .gitattributes | 10 + .gitignore | 23 + CMakeLists.txt | 97 ++ docs/v1_acceptance_report.md | 109 ++ examples/c3d4_one_element.inp | 23 + examples/c3d8_one_element.inp | 25 + examples/generate_sets.inp | 23 + examples/s4_one_element.inp | 22 + src/app/fem_program.f90 | 101 ++ src/base/fem_api.f90 | 945 ++++++++++ src/base/fem_control.f90 | 35 + src/base/fem_error.f90 | 47 + src/base/fem_log.f90 | 66 + src/base/fem_precision.f90 | 24 + src/boundary/fem_boundary.f90 | 357 ++++ src/coupling/fem_coupling.f90 | 741 ++++++++ src/element/fem_element_internal.f90 | 991 +++++++++++ src/element/fem_element_mass.f90 | 314 ++++ src/input/fem_inp_reader.f90 | 1731 +++++++++++++++++++ src/material/fem_material_j2.f90 | 550 ++++++ src/mesh/fem_database.f90 | 1205 +++++++++++++ src/output/fem_output_csv.f90 | 344 ++++ tests/data/c3d4_boundary_amplitude.inp | 25 + tests/data/c3d4_cload_amplitude.inp | 19 + tests/data/c3d4_elastic_wave_pair.inp | 27 + tests/data/c3d4_j2_eqps_failure.inp | 19 + tests/data/c3d4_j2_ideal_shear.inp | 17 + tests/data/c3d4_j2_linear_shear.inp | 18 + tests/data/c3d4_j2_multilinear_shear.inp | 19 + tests/data/c3d4_jc_damage_shear.inp | 19 + tests/data/c3d4_jc_rate_shear.inp | 19 + tests/data/c3d8_elastic_wave_bar.inp | 38 + tests/data/c3d8_two_element_shared_face.inp | 24 + tests/data/invalid_boundary_dof.inp | 21 + tests/data/invalid_missing_density.inp | 17 + tests/data/invalid_unknown_cload_target.inp | 21 + tests/performance/generate_p1_c3d4.py | 127 ++ tests/performance/perf_p1.f90 | 107 ++ tests/unit/test_material_j2.f90 | 458 +++++ tests/unit/test_smoke.f90 | 1035 +++++++++++ 40 files changed, 9813 insertions(+) create mode 100644 .gitattributes create mode 100644 CMakeLists.txt create mode 100644 docs/v1_acceptance_report.md create mode 100644 examples/c3d4_one_element.inp create mode 100644 examples/c3d8_one_element.inp create mode 100644 examples/generate_sets.inp create mode 100644 examples/s4_one_element.inp create mode 100644 src/app/fem_program.f90 create mode 100644 src/base/fem_api.f90 create mode 100644 src/base/fem_control.f90 create mode 100644 src/base/fem_error.f90 create mode 100644 src/base/fem_log.f90 create mode 100644 src/base/fem_precision.f90 create mode 100644 src/boundary/fem_boundary.f90 create mode 100644 src/coupling/fem_coupling.f90 create mode 100644 src/element/fem_element_internal.f90 create mode 100644 src/element/fem_element_mass.f90 create mode 100644 src/input/fem_inp_reader.f90 create mode 100644 src/material/fem_material_j2.f90 create mode 100644 src/mesh/fem_database.f90 create mode 100644 src/output/fem_output_csv.f90 create mode 100644 tests/data/c3d4_boundary_amplitude.inp create mode 100644 tests/data/c3d4_cload_amplitude.inp create mode 100644 tests/data/c3d4_elastic_wave_pair.inp create mode 100644 tests/data/c3d4_j2_eqps_failure.inp create mode 100644 tests/data/c3d4_j2_ideal_shear.inp create mode 100644 tests/data/c3d4_j2_linear_shear.inp create mode 100644 tests/data/c3d4_j2_multilinear_shear.inp create mode 100644 tests/data/c3d4_jc_damage_shear.inp create mode 100644 tests/data/c3d4_jc_rate_shear.inp create mode 100644 tests/data/c3d8_elastic_wave_bar.inp create mode 100644 tests/data/c3d8_two_element_shared_face.inp create mode 100644 tests/data/invalid_boundary_dof.inp create mode 100644 tests/data/invalid_missing_density.inp create mode 100644 tests/data/invalid_unknown_cload_target.inp create mode 100644 tests/performance/generate_p1_c3d4.py create mode 100644 tests/performance/perf_p1.f90 create mode 100644 tests/unit/test_material_j2.f90 create mode 100644 tests/unit/test_smoke.f90 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2b83c5d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +* text=auto + +*.f90 text eol=lf +*.F90 text eol=lf +*.cmake text eol=lf +CMakeLists.txt text eol=lf +*.md text eol=lf +*.inp text eol=lf +*.txt text eol=lf +*.py text eol=lf diff --git a/.gitignore b/.gitignore index dc61912..1f7d10a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,26 @@ Python/venv/ # Python cache files __pycache__/ *.pyc + +# CMake and Fortran build outputs +build*/ +.build*/ +CMakeFiles/ +CMakeCache.txt +cmake_install.cmake +CTestTestfile.cmake +Testing/ +*.mod +*.smod +*.o +*.obj +*.a +*.lib +*.exe + +# Local run outputs +*.log +history.csv +nodes_*.csv +elements_*.csv +/output/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..cdbabac --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,97 @@ +cmake_minimum_required(VERSION 3.20) + +project(FemLite + VERSION 0.1.0 + LANGUAGES Fortran +) + +enable_testing() + +set(CMAKE_Fortran_STANDARD 95) +set(CMAKE_Fortran_STANDARD_REQUIRED ON) +set(CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/mod") + +function(femlite_apply_fortran_options target_name) + if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU") + target_compile_options(${target_name} PRIVATE + -std=f95 + -fall-intrinsics + -Wall + -Wextra + -Wimplicit-interface + $<$:-g> + $<$:-O0> + $<$:-fcheck=all> + $<$:-fbacktrace> + ) + elseif(CMAKE_Fortran_COMPILER_ID MATCHES "Intel") + target_compile_options(${target_name} PRIVATE + $<$:-g> + $<$:-O0> + ) + endif() +endfunction() + +add_library(femlite_core STATIC + src/base/fem_precision.f90 + src/base/fem_error.f90 + src/base/fem_log.f90 + src/base/fem_control.f90 + src/mesh/fem_database.f90 + src/input/fem_inp_reader.f90 + src/material/fem_material_j2.f90 + src/element/fem_element_mass.f90 + src/element/fem_element_internal.f90 + src/coupling/fem_coupling.f90 + src/boundary/fem_boundary.f90 + src/output/fem_output_csv.f90 + src/base/fem_api.f90 +) + +set_target_properties(femlite_core PROPERTIES + OUTPUT_NAME FemLite +) + +target_include_directories(femlite_core PUBLIC + "$" +) + +femlite_apply_fortran_options(femlite_core) + +add_executable(femlite + src/app/fem_program.f90 +) + +target_link_libraries(femlite PRIVATE femlite_core) +femlite_apply_fortran_options(femlite) + +add_executable(test_smoke + tests/unit/test_smoke.f90 +) + +target_link_libraries(test_smoke PRIVATE femlite_core) +femlite_apply_fortran_options(test_smoke) + +add_executable(test_material_j2 + tests/unit/test_material_j2.f90 +) + +target_link_libraries(test_material_j2 PRIVATE femlite_core) +femlite_apply_fortran_options(test_material_j2) + +add_executable(perf_p1 + tests/performance/perf_p1.f90 +) + +target_link_libraries(perf_p1 PRIVATE femlite_core) +femlite_apply_fortran_options(perf_p1) + +add_test(NAME smoke COMMAND test_smoke) +set_tests_properties(smoke PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + +add_test(NAME material_j2 COMMAND test_material_j2) +set_tests_properties(material_j2 PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) diff --git a/docs/v1_acceptance_report.md b/docs/v1_acceptance_report.md new file mode 100644 index 0000000..ebf28ff --- /dev/null +++ b/docs/v1_acceptance_report.md @@ -0,0 +1,109 @@ +# FemLite 0.1.0 第一版验收报告 + +日期:2026-06-22 + +## 1. 版本信息 + +- 软件名称:FemLite +- 软件版本:0.1.0 +- 交付形态:静态库、命令行程序、测试程序 +- 语言标准:Fortran 95 +- 默认平台:WSL Ubuntu + gfortran + CMake + +## 2. 验收环境 + +- 操作系统:Linux 6.6.87.2-microsoft-standard-WSL2 x86_64 GNU/Linux +- 编译器:GNU Fortran 13.3.0 +- CMake:3.28.3 +- 构建目录:build-wsl-debug + +## 3. 第一版范围 + +第一版目标为: + +- 显式瞬态求解。 +- 实体单元 C3D4、C3D8。 +- 第一版有限能力 S4 壳单元。 +- 线弹性、小应变 J2 弹塑性、Johnson-Cook 率效应、温度软化、绝热塑性温升、Johnson-Cook 损伤。 +- Abaqus inp 子集读取。 +- Dirichlet、CLOAD、S4 压力 DLOAD、SPH 节点力、SPH facet 压力和剪切力接口。 +- CSV 历史量、节点快照、单元状态快照输出。 + +## 4. 验收项结果 + +| 验收项 | 结果 | 覆盖说明 | +|---|---:|---| +| 单元 patch test | PASS | C3D4、C3D8、S4 基础应变/应力/内力回归 | +| 单轴拉伸 | PASS | 线弹性和 J2 单轴应力更新 | +| 弹塑性加载卸载 | PASS | J2 加载、卸载、理想/线性/多段硬化 | +| 弹性波传播 | PASS | C3D8 两单元杆、C3D4 两单元对的显式传播测试 | +| 板受压力脉冲 | PASS | S4 压力载荷、集中质量、恢复内力和显式响应 | +| 简化 SPH-FEM 边界力传递 | PASS | 节点力、facet 压力、facet 压力加剪切力 | + +## 5. 回归测试 + +执行命令: + +```sh +cmake --build build-wsl-debug -j2 +ctest --test-dir build-wsl-debug --output-on-failure +``` + +结果: + +```text +100% tests passed, 0 tests failed out of 2 +``` + +测试程序: + +- smoke +- material_j2 + +## 6. P1 性能测试 + +P1 性能目标: + +- 10000 节点 +- 10000 单元 +- 100 步 +- 不要求并行 + +执行命令: + +```sh +python3 tests/performance/generate_p1_c3d4.py build-wsl-debug/p1_10000_c3d4.inp +./build-wsl-debug/perf_p1 build-wsl-debug/p1_10000_c3d4.inp +``` + +结果: + +```text +FemLite P1 performance + version: 0.1.0 + nodes: 10000 + elements: 10000 + dofs: 29838 + steps: 100 + time: 1.00000E-04 + cpu_sec: 1.577621 + result: PASS +``` + +性能测试说明: + +- 单元类型:C3D4。 +- 输出:关闭全场输出,只测试读入、建模、质量计算和显式步进。 +- 临时输入文件位于 build-wsl-debug,不纳入 git。 + +## 7. 已知限制 + +- S4 为第一版有限能力实现,后续仍需完善壳塑性、hourglass 控制和更完整的壳状态输出。 +- 第一版不装配全局刚度矩阵,不支持隐式非线性求解。 +- 接触、MPC、Robin、restart、VTK 输出属于第二版或后续范围。 +- C3D10、C3D20、S3、S6、S8 属于第二版范围。 +- 当前 P1 性能测试为本地 WSL 单机结果,不代表跨平台性能基线。 + +## 8. 验收结论 + +FemLite 0.1.0 已达到第一版功能验收标准。建议将当前状态标记为第一版基线,并在后续版本中推进壳单元完善、隐式求解、接触和 VTK/restart 输出。 diff --git a/examples/c3d4_one_element.inp b/examples/c3d4_one_element.inp new file mode 100644 index 0000000..6981fd8 --- /dev/null +++ b/examples/c3d4_one_element.inp @@ -0,0 +1,23 @@ +*HEADING +FemLite C3D4 one element scaffold example +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*NSET, NSET=FIXED +1 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*BOUNDARY +FIXED, 1, 3, 0.0 +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/examples/c3d8_one_element.inp b/examples/c3d8_one_element.inp new file mode 100644 index 0000000..88a42ae --- /dev/null +++ b/examples/c3d8_one_element.inp @@ -0,0 +1,25 @@ +*HEADING +FemLite C3D8 one element scaffold example +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 1.0, 1.0, 0.0 +4, 0.0, 1.0, 0.0 +5, 0.0, 0.0, 1.0 +6, 1.0, 0.0, 1.0 +7, 1.0, 1.0, 1.0 +8, 0.0, 1.0, 1.0 +*ELEMENT, TYPE=C3D8, ELSET=EALL +1, 1, 2, 3, 4, 5, 6, 7, 8 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*CLOAD +7, 3, -1.0E3 +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/examples/generate_sets.inp b/examples/generate_sets.inp new file mode 100644 index 0000000..48bc66b --- /dev/null +++ b/examples/generate_sets.inp @@ -0,0 +1,23 @@ +*HEADING +FemLite generated set scaffold example +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*NSET, NSET=ALLNODES, GENERATE +1, 4, 1 +*ELSET, ELSET=ALLELEMS, GENERATE +1, 1, 1 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/examples/s4_one_element.inp b/examples/s4_one_element.inp new file mode 100644 index 0000000..bf23d26 --- /dev/null +++ b/examples/s4_one_element.inp @@ -0,0 +1,22 @@ +*HEADING +FemLite S4 one element scaffold example +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 1.0, 1.0, 0.0 +4, 0.0, 1.0, 0.0 +*ELEMENT, TYPE=S4, ELSET=PLATE +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SHELL SECTION, ELSET=PLATE, MATERIAL=STEEL +0.01 +*DLOAD +PLATE, P, 1.0E6 +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/src/app/fem_program.f90 b/src/app/fem_program.f90 new file mode 100644 index 0000000..3f74b22 --- /dev/null +++ b/src/app/fem_program.f90 @@ -0,0 +1,101 @@ +program femlite + use fem_precision + use fem_api + implicit none + + integer :: ierr + integer :: major + integer :: minor + integer :: patch + integer :: argc + integer :: num_nodes + integer :: num_elems + integer :: num_dof + integer :: num_nsets + integer :: num_elsets + integer :: num_materials + integer :: num_sections + integer :: num_nset_items + integer :: num_elset_items + integer :: num_bc + integer :: num_cload + integer :: num_dload + integer :: step + real(rk) :: time + character(len=256) :: filename + character(len=256) :: arg + + intrinsic iargc + intrinsic getarg + intrinsic exit + + call fem_initialize(ierr) + call exit_if_error(ierr) + + call fem_get_version(major, minor, patch, ierr) + call exit_if_error(ierr) + + write(*,'(A,I1,A,I1,A,I1)') 'FemLite ', major, '.', minor, '.', patch + + argc = iargc() + if (argc .ge. 1) then + call getarg(1, filename) + call fem_read_inp(trim(filename), ierr) + call exit_if_error(ierr) + + call fem_build_model(ierr) + call exit_if_error(ierr) + + call fem_get_model_counts(num_nodes, num_elems, num_dof, num_nsets, & + num_elsets, num_materials, num_sections, & + num_nset_items, num_elset_items, ierr) + call exit_if_error(ierr) + call fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + call exit_if_error(ierr) + + write(*,'(A)') 'Model summary' + write(*,'(A,I8)') ' nodes: ', num_nodes + write(*,'(A,I8)') ' elements: ', num_elems + write(*,'(A,I8)') ' dofs: ', num_dof + write(*,'(A,I8)') ' nsets: ', num_nsets + write(*,'(A,I8)') ' elsets: ', num_elsets + write(*,'(A,I8)') ' materials:', num_materials + write(*,'(A,I8)') ' sections: ', num_sections + write(*,'(A,I8)') ' boundary: ', num_bc + write(*,'(A,I8)') ' cload: ', num_cload + write(*,'(A,I8)') ' dload: ', num_dload + + if (argc .ge. 2) then + call getarg(2, arg) + if (trim(arg) .eq. '--run') then + call fem_run_explicit(ierr) + call exit_if_error(ierr) + call fem_get_current_time(time, step, ierr) + call exit_if_error(ierr) + write(*,'(A)') 'Run completed' + write(*,'(A,I8)') ' steps: ', step + write(*,'(A,ES12.5)') ' time: ', time + end if + end if + end if + + call fem_finalize(ierr) + call exit_if_error(ierr) + +contains + + subroutine exit_if_error(ierr_in) + integer, intent(in) :: ierr_in + integer :: code + integer :: ierr_local + character(len=256) :: message + + if (ierr_in .eq. 0) return + + call fem_get_last_error(code, message, ierr_local) + write(*,'(A,I8)') 'FemLite error code: ', code + write(*,'(A,A)') 'FemLite error: ', trim(message) + call fem_finalize(ierr_local) + call exit(1) + end subroutine exit_if_error +end program femlite diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 new file mode 100644 index 0000000..2ec40ed --- /dev/null +++ b/src/base/fem_api.f90 @@ -0,0 +1,945 @@ +module fem_api + use fem_precision + use fem_error + use fem_log + use fem_control + use fem_database + use fem_inp_reader + use fem_element_mass + use fem_element_internal + use fem_coupling + use fem_boundary + use fem_output_csv + implicit none + + logical :: api_initialized = .false. + +contains + + subroutine fem_initialize(ierr) + integer, intent(out) :: ierr + integer :: log_ierr + + call error_clear(ierr) + call control_set_defaults() + call log_open('femlite.log', log_ierr) + if (log_ierr .ne. FEM_SUCCESS) then + call error_set(FEM_ERR_OUTPUT, 'failed to open femlite.log', ierr) + return + end if + + api_initialized = .true. + call log_info('FemLite initialized') + ierr = FEM_SUCCESS + end subroutine fem_initialize + + subroutine fem_finalize(ierr) + integer, intent(out) :: ierr + + call cpl_finalize_facets() + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call log_info('FemLite finalized') + call log_close() + api_initialized = .false. + ierr = FEM_SUCCESS + end subroutine fem_finalize + + subroutine fem_get_version(major, minor, patch, ierr) + integer, intent(out) :: major + integer, intent(out) :: minor + integer, intent(out) :: patch + integer, intent(out) :: ierr + + major = FEMLITE_VERSION_MAJOR + minor = FEMLITE_VERSION_MINOR + patch = FEMLITE_VERSION_PATCH + ierr = FEM_SUCCESS + end subroutine fem_get_version + + subroutine fem_read_inp(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + logical :: exists + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_read_inp called before fem_initialize', ierr) + return + end if + + inquire(file=filename, exist=exists) + if (.not. exists) then + call error_set(FEM_ERR_INPUT, 'input file does not exist: '//trim(filename), ierr) + return + end if + + call cpl_finalize_facets() + call output_csv_reset() + time_start = ZERO + time_end = ZERO + dt_user = ZERO + nstep_max = 0 + use_user_dt = .false. + call inp_read_file(filename, ierr) + if (ierr .ne. FEM_SUCCESS) return + + current_time = ZERO + current_step = 0 + end subroutine fem_read_inp + + subroutine fem_build_model(ierr) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_build_model called before fem_initialize', ierr) + return + end if + + call database_validate_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call cpl_build_facets(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call log_info('model build completed') + end subroutine fem_build_model + + subroutine fem_compute_lumped_mass(ierr) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_compute_lumped_mass called before fem_initialize', ierr) + return + end if + + call element_compute_lumped_mass(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call log_info('lumped mass computed') + end subroutine fem_compute_lumped_mass + + subroutine fem_compute_internal_force(ierr) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_compute_internal_force called before fem_initialize', ierr) + return + end if + + call element_compute_internal_force(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call log_info('internal force computed') + end subroutine fem_compute_internal_force + + subroutine fem_get_mass_summary(total_mass, rotational_inertia, ierr) + real(rk), intent(out) :: total_mass + real(rk), intent(out) :: rotational_inertia + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_mass_summary called before fem_initialize', ierr) + return + end if + + total_mass = ZERO + rotational_inertia = ZERO + if (.not. allocated(mass_diag)) then + call error_set(FEM_ERR_INTERNAL, 'mass vector is not allocated', ierr) + return + end if + + do node = 1, nnode + dof = dof_id(1,node) + if (dof .gt. 0) total_mass = total_mass + mass_diag(dof) + + do idir = 4, NDOF_NODE + dof = dof_id(idir,node) + if (dof .gt. 0) rotational_inertia = rotational_inertia + mass_diag(dof) + end do + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_mass_summary + + subroutine fem_clear_sph_forces(ierr) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_clear_sph_forces called before fem_initialize', ierr) + return + end if + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'SPH force vector is not allocated', ierr) + return + end if + + fsph = ZERO + ierr = FEM_SUCCESS + end subroutine fem_clear_sph_forces + + subroutine fem_apply_sph_nodal_force(node_id, force, ierr) + integer, intent(in) :: node_id + real(rk), intent(in) :: force(NDIM) + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_apply_sph_nodal_force called before fem_initialize', ierr) + return + end if + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'SPH force vector is not allocated', ierr) + return + end if + + node = database_find_node(node_id) + if (node .eq. 0) then + call error_set(FEM_ERR_COUPLING, 'SPH nodal force references unknown node', ierr) + return + end if + + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fsph(dof) = fsph(dof) + force(idir) + end do + + ierr = FEM_SUCCESS + end subroutine fem_apply_sph_nodal_force + + subroutine fem_apply_sph_nodal_forces(num_force_nodes, node_ids, force, ierr) + integer, intent(in) :: num_force_nodes + integer, intent(in) :: node_ids(num_force_nodes) + real(rk), intent(in) :: force(NDIM,num_force_nodes) + integer, intent(out) :: ierr + integer :: item + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_apply_sph_nodal_forces called before fem_initialize', ierr) + return + end if + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'SPH force vector is not allocated', ierr) + return + end if + if (num_force_nodes .lt. 0) then + call error_set(FEM_ERR_COUPLING, 'number of SPH force nodes is negative', ierr) + return + end if + + do item = 1, num_force_nodes + node = database_find_node(node_ids(item)) + if (node .eq. 0) then + call error_set(FEM_ERR_COUPLING, 'SPH nodal force references unknown node', ierr) + return + end if + end do + + do item = 1, num_force_nodes + node = database_find_node(node_ids(item)) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fsph(dof) = fsph(dof) + force(idir,item) + end do + end do + + ierr = FEM_SUCCESS + end subroutine fem_apply_sph_nodal_forces + + subroutine fem_get_sph_force_summary(total_force, ierr) + real(rk), intent(out) :: total_force(NDIM) + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_sph_force_summary called before fem_initialize', ierr) + return + end if + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'SPH force vector is not allocated', ierr) + return + end if + + total_force = ZERO + do node = 1, nnode + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) total_force(idir) = total_force(idir) + fsph(dof) + end do + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_sph_force_summary + + subroutine fem_get_boundary_facet_count(num_facets, max_face_nodes, ierr) + integer, intent(out) :: num_facets + integer, intent(out) :: max_face_nodes + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_boundary_facet_count called before fem_initialize', ierr) + return + end if + + call cpl_get_facet_count(num_facets, max_face_nodes, ierr) + end subroutine fem_get_boundary_facet_count + + subroutine fem_get_boundary_facets(nfacet_in, max_face_nodes_in, & + facet_id, elem_id, face_id, facet_nnode, & + facet_conn, x, vnod, normal, area, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode(nfacet_in) + integer, intent(out) :: facet_conn(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_boundary_facets called before fem_initialize', ierr) + return + end if + + call cpl_get_facets(nfacet_in, max_face_nodes_in, & + facet_id, elem_id, face_id, facet_nnode, & + facet_conn, x, vnod, normal, area, ierr) + end subroutine fem_get_boundary_facets + + subroutine fem_apply_sph_facet_pressure(nfacet_in, facet_id, pressure, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: facet_id(nfacet_in) + real(rk), intent(in) :: pressure(nfacet_in) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_apply_sph_facet_pressure called before fem_initialize', ierr) + return + end if + + call cpl_apply_facet_pressure(nfacet_in, facet_id, pressure, ierr) + end subroutine fem_apply_sph_facet_pressure + + subroutine fem_apply_sph_facet_tractions(nfacet_in, facet_id, pressure, shear, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: facet_id(nfacet_in) + real(rk), intent(in) :: pressure(nfacet_in) + real(rk), intent(in) :: shear(NDIM,nfacet_in) + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_apply_sph_facet_tractions called before fem_initialize', ierr) + return + end if + + call cpl_apply_facet_tractions(nfacet_in, facet_id, pressure, shear, ierr) + end subroutine fem_apply_sph_facet_tractions + + subroutine fem_get_node_kinematics(node_id, displacement, velocity, acceleration, ierr) + integer, intent(in) :: node_id + real(rk), intent(out) :: displacement(NDIM) + real(rk), intent(out) :: velocity(NDIM) + real(rk), intent(out) :: acceleration(NDIM) + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_node_kinematics called before fem_initialize', ierr) + return + end if + + node = database_find_node(node_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'node kinematics query references unknown node', ierr) + return + end if + + displacement = ZERO + velocity = ZERO + acceleration = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) then + displacement(idir) = u(dof) + velocity(idir) = v(dof) + acceleration(idir) = a(dof) + end if + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_node_kinematics + + subroutine fem_set_node_displacement(node_id, displacement, ierr) + integer, intent(in) :: node_id + real(rk), intent(in) :: displacement(NDIM) + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_set_node_displacement called before fem_initialize', ierr) + return + end if + + node = database_find_node(node_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'node displacement setter references unknown node', ierr) + return + end if + + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) u(dof) = displacement(idir) + end do + + ierr = FEM_SUCCESS + end subroutine fem_set_node_displacement + + subroutine fem_get_node_internal_force(node_id, force, ierr) + integer, intent(in) :: node_id + real(rk), intent(out) :: force(NDIM) + integer, intent(out) :: ierr + integer :: node + integer :: idir + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_node_internal_force called before fem_initialize', ierr) + return + end if + if (.not. allocated(fint)) then + call error_set(FEM_ERR_INTERNAL, 'internal force vector is not allocated', ierr) + return + end if + + node = database_find_node(node_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'internal force query references unknown node', ierr) + return + end if + + force = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) force(idir) = fint(dof) + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_node_internal_force + + subroutine fem_get_element_average_state(nelem_in, elem_id, stress_out, & + strain_out, plastic_strain, eqps, & + damage, ierr) + integer, intent(in) :: nelem_in + integer, intent(in) :: elem_id(nelem_in) + real(rk), intent(out) :: stress_out(NSTRESS,nelem_in) + real(rk), intent(out) :: strain_out(NSTRESS,nelem_in) + real(rk), intent(out) :: plastic_strain(NSTRESS,nelem_in) + real(rk), intent(out) :: eqps(nelem_in) + real(rk), intent(out) :: damage(nelem_in) + integer, intent(out) :: ierr + integer :: item + integer :: elem + integer :: gp + integer :: gp_start + integer :: gp_count + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_element_average_state called before fem_initialize', ierr) + return + end if + if (.not. allocated(elem_gp_start) .or. .not. allocated(stress) .or. & + .not. allocated(strain)) then + call error_set(FEM_ERR_INTERNAL, 'element state is not available', ierr) + return + end if + + stress_out = ZERO + strain_out = ZERO + plastic_strain = ZERO + eqps = ZERO + damage = ZERO + + do item = 1, nelem_in + elem = database_find_elem(elem_id(item)) + if (elem .eq. 0) then + call error_set(FEM_ERR_INPUT, 'element average state references unknown element', ierr) + return + end if + gp_start = elem_gp_start(elem) + gp_count = elem_ngp(elem) + if (gp_start .le. 0 .or. gp_count .le. 0) then + call error_set(FEM_ERR_OUTPUT, 'element state is not available for requested element', ierr) + return + end if + + do gp = gp_start, gp_start + gp_count - 1 + stress_out(:,item) = stress_out(:,item) + stress(:,gp) + strain_out(:,item) = strain_out(:,item) + strain(:,gp) + plastic_strain(:,item) = plastic_strain(:,item) + statev(5:10,gp) + eqps(item) = eqps(item) + statev(1,gp) + damage(item) = damage(item) + statev(2,gp) + end do + + stress_out(:,item) = stress_out(:,item) / real(gp_count, rk) + strain_out(:,item) = strain_out(:,item) / real(gp_count, rk) + plastic_strain(:,item) = plastic_strain(:,item) / real(gp_count, rk) + eqps(item) = eqps(item) / real(gp_count, rk) + damage(item) = damage(item) / real(gp_count, rk) + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_element_average_state + + subroutine fem_get_element_status(nelem_in, elem_id, status, ierr) + integer, intent(in) :: nelem_in + integer, intent(in) :: elem_id(nelem_in) + integer, intent(out) :: status(nelem_in) + integer, intent(out) :: ierr + integer :: item + integer :: elem + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_get_element_status called before fem_initialize', ierr) + return + end if + if (.not. allocated(elem_status)) then + call error_set(FEM_ERR_INTERNAL, 'element status array is not allocated', ierr) + return + end if + + status = 0 + do item = 1, nelem_in + elem = database_find_elem(elem_id(item)) + if (elem .eq. 0) then + call error_set(FEM_ERR_INPUT, 'element status query references unknown element', ierr) + return + end if + status(item) = elem_status(elem) + end do + + ierr = FEM_SUCCESS + end subroutine fem_get_element_status + + subroutine fem_set_output_directory(path, ierr) + character(len=*), intent(in) :: path + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_set_output_directory called before fem_initialize', ierr) + return + end if + + call output_csv_set_directory(path, ierr) + end subroutine fem_set_output_directory + + subroutine fem_set_output_interval(interval, ierr) + integer, intent(in) :: interval + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_set_output_interval called before fem_initialize', ierr) + return + end if + + call output_csv_set_interval(interval, ierr) + end subroutine fem_set_output_interval + + subroutine fem_write_output(step, time, ierr) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_write_output called before fem_initialize', ierr) + return + end if + + call output_csv_write(step, time, ierr) + end subroutine fem_write_output + + subroutine fem_estimate_stable_dt(dtcrit, ierr) + real(rk), intent(out) :: dtcrit + integer, intent(out) :: ierr + integer :: elem + integer :: mat + real(rk) :: hchar + real(rk) :: wave_speed + real(rk) :: elem_dt + logical :: has_active + + dtcrit = ZERO + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_estimate_stable_dt called before fem_initialize', ierr) + return + end if + if (nelem .le. 0) then + call error_set(FEM_ERR_MESH, 'stable time step requires at least one element', ierr) + return + end if + + elem_dt = huge(ONE) + has_active = .false. + do elem = 1, nelem + if (elem_status(elem) .ne. 0) cycle + has_active = .true. + + mat = elem_mat(elem) + call estimate_material_wave_speed(mat, wave_speed, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call estimate_element_hchar(elem, hchar, ierr) + if (ierr .ne. FEM_SUCCESS) return + + elem_dt = min(elem_dt, hchar / wave_speed) + end do + + if (.not. has_active) then + dtcrit = ZERO + else + dtcrit = stable_dt_safety * elem_dt + end if + dt_stable = dtcrit + ierr = FEM_SUCCESS + end subroutine fem_estimate_stable_dt + + subroutine estimate_material_wave_speed(mat, wave_speed, ierr) + integer, intent(in) :: mat + real(rk), intent(out) :: wave_speed + integer, intent(out) :: ierr + real(rk) :: lambda + real(rk) :: mu + + wave_speed = ZERO + if (mat .lt. 1 .or. mat .gt. n_material) then + call error_set(FEM_ERR_MATERIAL, 'stable time step found invalid material index', ierr) + return + end if + if (mat_density(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'stable time step requires positive density', ierr) + return + end if + if (mat_e(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'stable time step requires positive elastic modulus', ierr) + return + end if + if (mat_nu(mat) .le. -ONE .or. mat_nu(mat) .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'stable time step found invalid Poisson ratio', ierr) + return + end if + + lambda = mat_e(mat) * mat_nu(mat) / & + ((ONE + mat_nu(mat)) * (ONE - 2.0_rk * mat_nu(mat))) + mu = mat_e(mat) / (2.0_rk * (ONE + mat_nu(mat))) + wave_speed = sqrt((lambda + 2.0_rk * mu) / mat_density(mat)) + ierr = FEM_SUCCESS + end subroutine estimate_material_wave_speed + + subroutine estimate_element_hchar(elem, hchar, ierr) + integer, intent(in) :: elem + real(rk), intent(out) :: hchar + integer, intent(out) :: ierr + character(len=ELEM_TYPE_LEN) :: etype + + hchar = huge(ONE) + etype = adjustl(elem_type(elem)) + + select case (trim(etype)) + case ('C3D4') + call update_min_edge(elem, 1, 2, hchar) + call update_min_edge(elem, 1, 3, hchar) + call update_min_edge(elem, 1, 4, hchar) + call update_min_edge(elem, 2, 3, hchar) + call update_min_edge(elem, 2, 4, hchar) + call update_min_edge(elem, 3, 4, hchar) + case ('C3D8') + call update_min_edge(elem, 1, 2, hchar) + call update_min_edge(elem, 2, 3, hchar) + call update_min_edge(elem, 3, 4, hchar) + call update_min_edge(elem, 4, 1, hchar) + call update_min_edge(elem, 5, 6, hchar) + call update_min_edge(elem, 6, 7, hchar) + call update_min_edge(elem, 7, 8, hchar) + call update_min_edge(elem, 8, 5, hchar) + call update_min_edge(elem, 1, 5, hchar) + call update_min_edge(elem, 2, 6, hchar) + call update_min_edge(elem, 3, 7, hchar) + call update_min_edge(elem, 4, 8, hchar) + case ('S4') + call update_min_edge(elem, 1, 2, hchar) + call update_min_edge(elem, 2, 3, hchar) + call update_min_edge(elem, 3, 4, hchar) + call update_min_edge(elem, 4, 1, hchar) + case default + call error_set(FEM_ERR_MESH, 'unsupported element type for stable time step', ierr) + return + end select + + if (hchar .le. ZERO .or. hchar .gt. 0.5_rk * huge(ONE)) then + call error_set(FEM_ERR_MESH, 'element has non-positive characteristic length', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine estimate_element_hchar + + subroutine update_min_edge(elem, local_a, local_b, hchar) + integer, intent(in) :: elem + integer, intent(in) :: local_a + integer, intent(in) :: local_b + real(rk), intent(inout) :: hchar + real(rk) :: xa(NDIM) + real(rk) :: xb(NDIM) + real(rk) :: edge_length + integer :: node_a + integer :: node_b + + node_a = elem_conn(local_a, elem) + node_b = elem_conn(local_b, elem) + call current_node_position(node_a, xa) + call current_node_position(node_b, xb) + edge_length = sqrt(sum((xb - xa) * (xb - xa))) + hchar = min(hchar, edge_length) + end subroutine update_min_edge + + subroutine current_node_position(node, xyz) + integer, intent(in) :: node + real(rk), intent(out) :: xyz(NDIM) + integer :: idir + integer :: dof + + xyz = coord0(:,node) + if (.not. allocated(u)) return + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) xyz(idir) = xyz(idir) + u(dof) + end do + end subroutine current_node_position + + subroutine fem_advance_explicit(dt, ierr) + real(rk), intent(in) :: dt + integer, intent(out) :: ierr + real(rk) :: rhs + integer :: dof + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_advance_explicit called before fem_initialize', ierr) + return + end if + if (dt .le. ZERO) then + call error_set(FEM_ERR_SOLVER, 'explicit time step must be positive', ierr) + return + end if + if (.not. allocated(mass_diag)) then + call error_set(FEM_ERR_INTERNAL, 'mass vector is not allocated', ierr) + return + end if + + do dof = 1, ndof + if (mass_diag(dof) .le. ZERO) then + call error_set(FEM_ERR_SOLVER, 'non-positive lumped mass in explicit step', ierr) + return + end if + end do + + call boundary_apply_dirichlet(current_time, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call element_compute_internal_force(ierr, dt) + if (ierr .ne. FEM_SUCCESS) return + + call boundary_apply_prescribed_loads(current_time, ierr) + if (ierr .ne. FEM_SUCCESS) return + + do dof = 1, ndof + rhs = fext(dof) + fsph(dof) + fcontact(dof) - fint(dof) + a(dof) = rhs / mass_diag(dof) + + if (current_step .eq. 0) then + vhalf(dof) = v(dof) + HALF * dt * a(dof) + else + vhalf(dof) = vhalf(dof) + dt * a(dof) + end if + + u(dof) = u(dof) + dt * vhalf(dof) + v(dof) = vhalf(dof) + HALF * dt * a(dof) + end do + + call boundary_apply_dirichlet(current_time + dt, ierr) + if (ierr .ne. FEM_SUCCESS) return + + current_step = current_step + 1 + current_time = current_time + dt + ierr = FEM_SUCCESS + end subroutine fem_advance_explicit + + subroutine fem_run_explicit(ierr) + integer, intent(out) :: ierr + real(rk) :: dt + real(rk) :: dt_step + real(rk) :: remaining + real(rk) :: time_tol + integer :: run_steps + integer :: step_limit + + if (.not. api_initialized) then + call error_set(FEM_ERR_INTERNAL, 'fem_run_explicit called before fem_initialize', ierr) + return + end if + if (time_end .le. current_time) then + call error_set(FEM_ERR_SOLVER, 'explicit run requires a positive end time', ierr) + return + end if + + call fem_compute_lumped_mass(ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (use_user_dt) then + dt = dt_user + else + call fem_estimate_stable_dt(dt, ierr) + if (ierr .ne. FEM_SUCCESS) return + end if + if (dt .le. ZERO) then + call error_set(FEM_ERR_SOLVER, 'explicit run requires a positive time step', ierr) + return + end if + + call boundary_apply_dirichlet(current_time, ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (output_interval .gt. 0) then + call fem_write_output(current_step, current_time, ierr) + if (ierr .ne. FEM_SUCCESS) return + end if + + if (nstep_max .gt. 0) then + step_limit = nstep_max + else + step_limit = huge(1) + end if + run_steps = 0 + time_tol = max(1.0e-12_rk * max(ONE, abs(time_end)), 1.0e-30_rk) + + do + remaining = time_end - current_time + if (remaining .le. time_tol) exit + if (run_steps .ge. step_limit) exit + + dt_step = min(dt, remaining) + call fem_advance_explicit(dt_step, ierr) + if (ierr .ne. FEM_SUCCESS) return + run_steps = run_steps + 1 + + if (output_interval .gt. 0) then + if (mod(current_step, output_interval) .eq. 0 .or. & + time_end - current_time .le. time_tol) then + call fem_write_output(current_step, current_time, ierr) + if (ierr .ne. FEM_SUCCESS) return + end if + end if + end do + + if (time_end - current_time .gt. time_tol) then + call error_set(FEM_ERR_SOLVER, 'explicit run stopped before end time', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine fem_run_explicit + + subroutine fem_get_current_time(time, step, ierr) + real(rk), intent(out) :: time + integer, intent(out) :: step + integer, intent(out) :: ierr + + time = current_time + step = current_step + ierr = FEM_SUCCESS + end subroutine fem_get_current_time + + subroutine fem_get_time_control(dt, end_time, max_steps, user_dt, ierr) + real(rk), intent(out) :: dt + real(rk), intent(out) :: end_time + integer, intent(out) :: max_steps + logical, intent(out) :: user_dt + integer, intent(out) :: ierr + + dt = dt_user + end_time = time_end + max_steps = nstep_max + user_dt = use_user_dt + ierr = FEM_SUCCESS + end subroutine fem_get_time_control + + subroutine fem_get_model_size(num_nodes, num_elems, num_dof, ierr) + integer, intent(out) :: num_nodes + integer, intent(out) :: num_elems + integer, intent(out) :: num_dof + integer, intent(out) :: ierr + + num_nodes = nnode + num_elems = nelem + num_dof = ndof + ierr = FEM_SUCCESS + end subroutine fem_get_model_size + + subroutine fem_get_model_counts(num_nodes, num_elems, num_dof, num_nsets, & + num_elsets, num_materials, num_sections, & + num_nset_items, num_elset_items, ierr) + integer, intent(out) :: num_nodes + integer, intent(out) :: num_elems + integer, intent(out) :: num_dof + integer, intent(out) :: num_nsets + integer, intent(out) :: num_elsets + integer, intent(out) :: num_materials + integer, intent(out) :: num_sections + integer, intent(out) :: num_nset_items + integer, intent(out) :: num_elset_items + integer, intent(out) :: ierr + + num_nodes = nnode + num_elems = nelem + num_dof = ndof + num_nsets = n_nset + num_elsets = n_elset + num_materials = n_material + num_sections = n_section + num_nset_items = 0 + num_elset_items = 0 + if (allocated(nset_count)) num_nset_items = sum(nset_count) + if (allocated(elset_count)) num_elset_items = sum(elset_count) + ierr = FEM_SUCCESS + end subroutine fem_get_model_counts + + subroutine fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + integer, intent(out) :: num_bc + integer, intent(out) :: num_cload + integer, intent(out) :: num_dload + integer, intent(out) :: ierr + + num_bc = nbc + num_cload = ncload + num_dload = ndload + ierr = FEM_SUCCESS + end subroutine fem_get_load_counts + +end module fem_api diff --git a/src/base/fem_control.f90 b/src/base/fem_control.f90 new file mode 100644 index 0000000..47a270b --- /dev/null +++ b/src/base/fem_control.f90 @@ -0,0 +1,35 @@ +module fem_control + use fem_precision + implicit none + + real(rk) :: time_start = ZERO + real(rk) :: time_end = ZERO + real(rk) :: current_time = ZERO + real(rk) :: dt_user = ZERO + real(rk) :: dt_stable = ZERO + real(rk) :: stable_dt_safety = 0.8_rk + integer :: current_step = 0 + integer :: nstep_max = 0 + integer :: output_interval = 1 + logical :: use_sph_coupling = .false. + logical :: use_user_dt = .false. + logical :: large_deformation = .true. + +contains + + subroutine control_set_defaults() + time_start = ZERO + time_end = ZERO + current_time = ZERO + dt_user = ZERO + dt_stable = ZERO + stable_dt_safety = 0.8_rk + current_step = 0 + nstep_max = 0 + output_interval = 1 + use_sph_coupling = .false. + use_user_dt = .false. + large_deformation = .true. + end subroutine control_set_defaults + +end module fem_control diff --git a/src/base/fem_error.f90 b/src/base/fem_error.f90 new file mode 100644 index 0000000..23251ce --- /dev/null +++ b/src/base/fem_error.f90 @@ -0,0 +1,47 @@ +module fem_error + use fem_precision + implicit none + + integer, parameter :: FEM_SUCCESS = 0 + integer, parameter :: FEM_ERR_INPUT = 1000 + integer, parameter :: FEM_ERR_MESH = 2000 + integer, parameter :: FEM_ERR_MATERIAL = 3000 + integer, parameter :: FEM_ERR_SOLVER = 4000 + integer, parameter :: FEM_ERR_COUPLING = 5000 + integer, parameter :: FEM_ERR_OUTPUT = 6000 + integer, parameter :: FEM_ERR_INTERNAL = 9000 + + integer :: last_error_code = FEM_SUCCESS + character(len=256) :: last_error_message = 'success' + +contains + + subroutine error_clear(ierr) + integer, intent(out) :: ierr + + last_error_code = FEM_SUCCESS + last_error_message = 'success' + ierr = FEM_SUCCESS + end subroutine error_clear + + subroutine error_set(code, message, ierr) + integer, intent(in) :: code + character(len=*), intent(in) :: message + integer, intent(out) :: ierr + + last_error_code = code + last_error_message = message + ierr = code + end subroutine error_set + + subroutine fem_get_last_error(code, message, ierr) + integer, intent(out) :: code + character(len=*), intent(out) :: message + integer, intent(out) :: ierr + + code = last_error_code + message = last_error_message + ierr = FEM_SUCCESS + end subroutine fem_get_last_error + +end module fem_error diff --git a/src/base/fem_log.f90 b/src/base/fem_log.f90 new file mode 100644 index 0000000..0b0e62f --- /dev/null +++ b/src/base/fem_log.f90 @@ -0,0 +1,66 @@ +module fem_log + use fem_precision + use fem_error + implicit none + + integer, parameter :: LOG_UNIT = 99 + logical :: log_is_open = .false. + +contains + + subroutine log_open(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + integer :: ios + + if (log_is_open) then + close(LOG_UNIT) + log_is_open = .false. + end if + + open(unit=LOG_UNIT, file=filename, status='replace', action='write', iostat=ios) + if (ios .ne. 0) then + ierr = FEM_ERR_OUTPUT + return + end if + + log_is_open = .true. + ierr = FEM_SUCCESS + end subroutine log_open + + subroutine log_close() + if (log_is_open) then + close(LOG_UNIT) + log_is_open = .false. + end if + end subroutine log_close + + subroutine log_info(message) + character(len=*), intent(in) :: message + + call log_write('INFO', message) + end subroutine log_info + + subroutine log_warning(message) + character(len=*), intent(in) :: message + + call log_write('WARNING', message) + end subroutine log_warning + + subroutine log_error(message) + character(len=*), intent(in) :: message + + call log_write('ERROR', message) + end subroutine log_error + + subroutine log_write(level, message) + character(len=*), intent(in) :: level + character(len=*), intent(in) :: message + + write(*,'(A,A,A)') '[', trim(level), '] '//trim(message) + if (log_is_open) then + write(LOG_UNIT,'(A,A,A)') '[', trim(level), '] '//trim(message) + end if + end subroutine log_write + +end module fem_log diff --git a/src/base/fem_precision.f90 b/src/base/fem_precision.f90 new file mode 100644 index 0000000..ddb62cd --- /dev/null +++ b/src/base/fem_precision.f90 @@ -0,0 +1,24 @@ +module fem_precision + implicit none + + integer, parameter :: rk = selected_real_kind(12, 100) + integer, parameter :: ik = selected_int_kind(9) + + integer, parameter :: NDIM = 3 + integer, parameter :: NDOF_NODE = 6 + integer, parameter :: NSTRESS = 6 + integer, parameter :: MAX_NEN = 20 + integer, parameter :: MAX_FACE_NODES = 8 + integer, parameter :: NAME_LEN = 80 + integer, parameter :: ELEM_TYPE_LEN = 16 + integer, parameter :: MAT_MODEL_LEN = 32 + integer, parameter :: NSTATEV_MAX = 32 + + integer, parameter :: FEMLITE_VERSION_MAJOR = 0 + integer, parameter :: FEMLITE_VERSION_MINOR = 1 + integer, parameter :: FEMLITE_VERSION_PATCH = 0 + + real(rk), parameter :: ZERO = 0.0_rk + real(rk), parameter :: ONE = 1.0_rk + real(rk), parameter :: HALF = 0.5_rk +end module fem_precision diff --git a/src/boundary/fem_boundary.f90 b/src/boundary/fem_boundary.f90 new file mode 100644 index 0000000..d0d7222 --- /dev/null +++ b/src/boundary/fem_boundary.f90 @@ -0,0 +1,357 @@ +module fem_boundary + use fem_precision + use fem_error + use fem_database + implicit none + + real(rk) :: boundary_last_load_time = ZERO + +contains + + subroutine boundary_apply_prescribed_loads(time, ierr) + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: item + + if (.not. allocated(fext)) then + call error_set(FEM_ERR_INTERNAL, 'external force vector is not allocated', ierr) + return + end if + boundary_last_load_time = time + + fext = ZERO + + do item = 1, ncload + call apply_cload_item(item, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + do item = 1, ndload + call apply_dload_item(item, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine boundary_apply_prescribed_loads + + subroutine boundary_apply_dirichlet(time, ierr) + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: item + + do item = 1, nbc + call apply_boundary_item(item, time, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine boundary_apply_dirichlet + + subroutine apply_cload_item(item, ierr) + integer, intent(in) :: item + integer, intent(out) :: ierr + real(rk) :: scale + + scale = database_amplitude_value(cload_amp(item), boundary_last_load_time) + call apply_nodal_load_target(cload_target(item), cload_dof(item), & + scale * cload_value(item), ierr) + end subroutine apply_cload_item + + subroutine apply_boundary_item(item, time, ierr) + integer, intent(in) :: item + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: dof_num + real(rk) :: scale + + scale = database_amplitude_value(bc_amp(item), time) + do dof_num = bc_dof_start(item), bc_dof_end(item) + call apply_dirichlet_target(bc_target(item), dof_num, & + scale * bc_value(item), ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_boundary_item + + subroutine apply_dload_item(item, ierr) + integer, intent(in) :: item + integer, intent(out) :: ierr + integer :: elem + integer :: set_id + integer :: pos + integer :: elem_internal + integer :: external_id + real(rk) :: scale + + if (trim(dload_label(item)) .ne. 'P') then + call error_set(FEM_ERR_INPUT, 'only DLOAD label P is implemented', ierr) + return + end if + + scale = database_amplitude_value(dload_amp(item), boundary_last_load_time) + + read(dload_target(item),*,iostat=pos) external_id + if (pos .eq. 0) then + elem = database_find_elem(external_id) + if (elem .eq. 0) then + call error_set(FEM_ERR_INPUT, 'DLOAD references unknown element', ierr) + return + end if + call apply_pressure_to_element(elem, scale * dload_value(item), ierr) + return + end if + + set_id = database_find_elset(dload_target(item)) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'DLOAD references unknown element set', ierr) + return + end if + + do pos = elset_start(set_id), elset_start(set_id) + elset_count(set_id) - 1 + elem_internal = database_find_elem(elset_items(pos)) + if (elem_internal .eq. 0) then + call error_set(FEM_ERR_INPUT, 'DLOAD element set contains unknown element', ierr) + return + end if + call apply_pressure_to_element(elem_internal, scale * dload_value(item), ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_dload_item + + subroutine apply_nodal_load_target(target, dof_num, value, ierr) + character(len=*), intent(in) :: target + integer, intent(in) :: dof_num + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: node + integer :: set_id + integer :: pos + integer :: external_id + integer :: ios + + read(target,*,iostat=ios) external_id + if (ios .eq. 0) then + node = database_find_node(external_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'CLOAD references unknown node', ierr) + return + end if + call add_nodal_force(node, dof_num, value, ierr) + return + end if + + set_id = database_find_nset(target) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'CLOAD references unknown node set', ierr) + return + end if + + do pos = nset_start(set_id), nset_start(set_id) + nset_count(set_id) - 1 + node = database_find_node(nset_items(pos)) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'CLOAD node set contains unknown node', ierr) + return + end if + call add_nodal_force(node, dof_num, value, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_nodal_load_target + + subroutine apply_dirichlet_target(target, dof_num, value, ierr) + character(len=*), intent(in) :: target + integer, intent(in) :: dof_num + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: node + integer :: set_id + integer :: pos + integer :: external_id + integer :: ios + + read(target,*,iostat=ios) external_id + if (ios .eq. 0) then + node = database_find_node(external_id) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'BOUNDARY references unknown node', ierr) + return + end if + call set_dirichlet_node(node, dof_num, value, ierr) + return + end if + + set_id = database_find_nset(target) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'BOUNDARY references unknown node set', ierr) + return + end if + + do pos = nset_start(set_id), nset_start(set_id) + nset_count(set_id) - 1 + node = database_find_node(nset_items(pos)) + if (node .eq. 0) then + call error_set(FEM_ERR_INPUT, 'BOUNDARY node set contains unknown node', ierr) + return + end if + call set_dirichlet_node(node, dof_num, value, ierr) + if (ierr .ne. FEM_SUCCESS) return + end do + + ierr = FEM_SUCCESS + end subroutine apply_dirichlet_target + + subroutine add_nodal_force(node, dof_num, value, ierr) + integer, intent(in) :: node + integer, intent(in) :: dof_num + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: dof + + if (dof_num .lt. 1 .or. dof_num .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'nodal load dof is outside 1..6', ierr) + return + end if + + dof = dof_id(dof_num,node) + if (dof .gt. 0) fext(dof) = fext(dof) + value + ierr = FEM_SUCCESS + end subroutine add_nodal_force + + subroutine set_dirichlet_node(node, dof_num, value, ierr) + integer, intent(in) :: node + integer, intent(in) :: dof_num + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: dof + + if (dof_num .lt. 1 .or. dof_num .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'boundary dof is outside 1..6', ierr) + return + end if + + dof = dof_id(dof_num,node) + if (dof .gt. 0) then + u(dof) = value + v(dof) = ZERO + a(dof) = ZERO + vhalf(dof) = ZERO + end if + ierr = FEM_SUCCESS + end subroutine set_dirichlet_node + + subroutine apply_pressure_to_element(elem, pressure, ierr) + integer, intent(in) :: elem + real(rk), intent(in) :: pressure + integer, intent(out) :: ierr + real(rk) :: normal(NDIM) + real(rk) :: area + real(rk) :: nodal_force(NDIM) + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + if (elem_status(elem) .ne. 0) then + ierr = FEM_SUCCESS + return + end if + + if (trim(adjustl(elem_type(elem))) .ne. 'S4') then + call error_set(FEM_ERR_INPUT, 'DLOAD P is implemented for S4 only', ierr) + return + end if + + call shell4_normal_area(elem, normal, area, ierr) + if (ierr .ne. FEM_SUCCESS) return + + nodal_force = -pressure * normal * area / real(elem_nnode(elem), rk) + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node,elem) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fext(dof) = fext(dof) + nodal_force(idir) + end do + end do + + ierr = FEM_SUCCESS + end subroutine apply_pressure_to_element + + subroutine shell4_normal_area(elem, normal, area, ierr) + integer, intent(in) :: elem + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + real(rk) :: coords(NDIM,4) + real(rk) :: n1(NDIM) + real(rk) :: n2(NDIM) + real(rk) :: a1 + real(rk) :: a2 + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + do local_node = 1, 4 + node = elem_conn(local_node,elem) + coords(:,local_node) = coord0(:,node) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) coords(idir,local_node) = coords(idir,local_node) + u(dof) + end do + end do + + call triangle_normal_area(coords(:,1), coords(:,2), coords(:,3), n1, a1, ierr) + if (ierr .ne. FEM_SUCCESS) return + call triangle_normal_area(coords(:,1), coords(:,3), coords(:,4), n2, a2, ierr) + if (ierr .ne. FEM_SUCCESS) return + + area = a1 + a2 + normal = n1 * a1 + n2 * a2 + if (area .le. ZERO .or. sqrt(sum(normal * normal)) .le. ZERO) then + call error_set(FEM_ERR_MESH, 'S4 pressure face has non-positive area', ierr) + return + end if + normal = normal / sqrt(sum(normal * normal)) + ierr = FEM_SUCCESS + end subroutine shell4_normal_area + + subroutine triangle_normal_area(a, b, c, normal, area, ierr) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(in) :: c(NDIM) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + real(rk) :: ab(NDIM) + real(rk) :: ac(NDIM) + real(rk) :: cr(NDIM) + real(rk) :: norm_cr + + ab = b - a + ac = c - a + call cross3(ab, ac, cr) + norm_cr = sqrt(sum(cr * cr)) + if (norm_cr .le. ZERO) then + call error_set(FEM_ERR_MESH, 'pressure face has zero area triangle', ierr) + return + end if + + area = HALF * norm_cr + normal = cr / norm_cr + ierr = FEM_SUCCESS + end subroutine triangle_normal_area + + subroutine cross3(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(out) :: c(NDIM) + + c(1) = a(2) * b(3) - a(3) * b(2) + c(2) = a(3) * b(1) - a(1) * b(3) + c(3) = a(1) * b(2) - a(2) * b(1) + end subroutine cross3 + +end module fem_boundary diff --git a/src/coupling/fem_coupling.f90 b/src/coupling/fem_coupling.f90 new file mode 100644 index 0000000..719089a --- /dev/null +++ b/src/coupling/fem_coupling.f90 @@ -0,0 +1,741 @@ +module fem_coupling + use fem_precision + use fem_error + use fem_database + implicit none + + private + + integer :: nfacet = 0 + integer, allocatable :: facet_elem(:) + integer, allocatable :: facet_face(:) + integer, allocatable :: facet_nnode(:) + integer, allocatable :: facet_conn(:,:) + + public :: cpl_build_facets + public :: cpl_finalize_facets + public :: cpl_get_facet_count + public :: cpl_get_facets + public :: cpl_apply_facet_pressure + public :: cpl_apply_facet_tractions + +contains + + subroutine cpl_build_facets(ierr) + integer, intent(out) :: ierr + integer :: max_faces + integer :: table_size + integer :: stat + integer :: elem + integer :: face_count + integer :: out_count + integer :: local_nodes(MAX_FACE_NODES) + integer, allocatable :: hash(:) + integer, allocatable :: cand_elem(:) + integer, allocatable :: cand_face(:) + integer, allocatable :: cand_nnode(:) + integer, allocatable :: cand_count(:) + integer, allocatable :: cand_conn(:,:) + integer, allocatable :: cand_key(:,:) + + call cpl_finalize_facets() + + call count_candidate_faces(max_faces, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (max_faces .le. 0) then + nfacet = 0 + ierr = FEM_SUCCESS + return + end if + + table_size = max(17, 3 * max_faces + 1) + allocate(hash(table_size), cand_elem(max_faces), cand_face(max_faces), & + cand_nnode(max_faces), cand_count(max_faces), & + cand_conn(MAX_FACE_NODES,max_faces), & + cand_key(MAX_FACE_NODES,max_faces), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate facet candidates', ierr) + return + end if + + hash = 0 + cand_elem = 0 + cand_face = 0 + cand_nnode = 0 + cand_count = 0 + cand_conn = 0 + cand_key = 0 + face_count = 0 + + do elem = 1, nelem + if (elem_status(elem) .ne. 0) cycle + + select case (trim(adjustl(elem_type(elem)))) + case ('C3D4') + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 2 + local_nodes(3) = 3 + call add_solid_face(elem, 1, 3, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 2 + local_nodes(3) = 4 + call add_solid_face(elem, 2, 3, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 3 + local_nodes(3) = 4 + call add_solid_face(elem, 3, 3, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 2 + local_nodes(2) = 3 + local_nodes(3) = 4 + call add_solid_face(elem, 4, 3, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + case ('C3D8') + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 2 + local_nodes(3) = 3 + local_nodes(4) = 4 + call add_solid_face(elem, 1, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 5 + local_nodes(2) = 6 + local_nodes(3) = 7 + local_nodes(4) = 8 + call add_solid_face(elem, 2, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 2 + local_nodes(3) = 6 + local_nodes(4) = 5 + call add_solid_face(elem, 3, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 2 + local_nodes(2) = 3 + local_nodes(3) = 7 + local_nodes(4) = 6 + call add_solid_face(elem, 4, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 3 + local_nodes(2) = 4 + local_nodes(3) = 8 + local_nodes(4) = 7 + call add_solid_face(elem, 5, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + local_nodes = 0 + local_nodes(1) = 4 + local_nodes(2) = 1 + local_nodes(3) = 5 + local_nodes(4) = 8 + call add_solid_face(elem, 6, 4, local_nodes, face_count, hash, & + table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + case ('S4') + local_nodes = 0 + local_nodes(1) = 1 + local_nodes(2) = 2 + local_nodes(3) = 3 + local_nodes(4) = 4 + call add_shell_face(elem, 1, 4, local_nodes, face_count, cand_elem, & + cand_face, cand_nnode, cand_count, cand_conn, & + cand_key, ierr) + if (ierr .ne. FEM_SUCCESS) return + + case default + call error_set(FEM_ERR_MESH, 'unsupported element type for boundary facets', ierr) + return + end select + end do + + out_count = 0 + do elem = 1, face_count + if (cand_count(elem) .eq. 1) out_count = out_count + 1 + end do + + nfacet = out_count + if (nfacet .gt. 0) then + allocate(facet_elem(nfacet), facet_face(nfacet), facet_nnode(nfacet), & + facet_conn(MAX_FACE_NODES,nfacet), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate boundary facets', ierr) + return + end if + + out_count = 0 + do elem = 1, face_count + if (cand_count(elem) .eq. 1) then + out_count = out_count + 1 + facet_elem(out_count) = cand_elem(elem) + facet_face(out_count) = cand_face(elem) + facet_nnode(out_count) = cand_nnode(elem) + facet_conn(:,out_count) = cand_conn(:,elem) + end if + end do + end if + + deallocate(hash, cand_elem, cand_face, cand_nnode, cand_count, & + cand_conn, cand_key) + ierr = FEM_SUCCESS + end subroutine cpl_build_facets + + subroutine cpl_finalize_facets() + if (allocated(facet_elem)) deallocate(facet_elem) + if (allocated(facet_face)) deallocate(facet_face) + if (allocated(facet_nnode)) deallocate(facet_nnode) + if (allocated(facet_conn)) deallocate(facet_conn) + nfacet = 0 + end subroutine cpl_finalize_facets + + subroutine cpl_get_facet_count(facet_count, max_face_nodes, ierr) + integer, intent(out) :: facet_count + integer, intent(out) :: max_face_nodes + integer, intent(out) :: ierr + integer :: idx + + facet_count = 0 + max_face_nodes = 0 + do idx = 1, nfacet + if (elem_status(facet_elem(idx)) .eq. 0) then + facet_count = facet_count + 1 + if (facet_nnode(idx) .gt. max_face_nodes) max_face_nodes = facet_nnode(idx) + end if + end do + ierr = FEM_SUCCESS + end subroutine cpl_get_facet_count + + subroutine cpl_get_facets(nfacet_in, max_face_nodes_in, & + facet_id, elem_id, face_id, & + facet_nnode_out, facet_conn_out, & + x, vnod, normal, area, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: max_face_nodes_in + integer, intent(out) :: facet_id(nfacet_in) + integer, intent(out) :: elem_id(nfacet_in) + integer, intent(out) :: face_id(nfacet_in) + integer, intent(out) :: facet_nnode_out(nfacet_in) + integer, intent(out) :: facet_conn_out(max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: x(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: vnod(NDIM,max_face_nodes_in,nfacet_in) + real(rk), intent(out) :: normal(NDIM,nfacet_in) + real(rk), intent(out) :: area(nfacet_in) + integer, intent(out) :: ierr + integer :: idx + integer :: active_count + integer :: out_idx + integer :: local_node + integer :: node + integer :: max_nodes + real(rk) :: coords(NDIM,MAX_FACE_NODES) + real(rk) :: velocities(NDIM,MAX_FACE_NODES) + real(rk) :: nrm(NDIM) + real(rk) :: ar + + call cpl_get_facet_count(active_count, max_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (nfacet_in .lt. active_count) then + call error_set(FEM_ERR_COUPLING, 'facet output array is too small', ierr) + return + end if + if (max_face_nodes_in .lt. max_nodes) then + call error_set(FEM_ERR_COUPLING, 'facet node dimension is too small', ierr) + return + end if + + facet_id = 0 + elem_id = 0 + face_id = 0 + facet_nnode_out = 0 + facet_conn_out = 0 + x = ZERO + vnod = ZERO + normal = ZERO + area = ZERO + + out_idx = 0 + do idx = 1, nfacet + if (elem_status(facet_elem(idx)) .ne. 0) cycle + out_idx = out_idx + 1 + call facet_current_geometry(idx, coords, velocities, nrm, ar, ierr) + if (ierr .ne. FEM_SUCCESS) return + + facet_id(out_idx) = idx + elem_id(out_idx) = elem_id_ext(facet_elem(idx)) + face_id(out_idx) = facet_face(idx) + facet_nnode_out(out_idx) = facet_nnode(idx) + normal(:,out_idx) = nrm + area(out_idx) = ar + + do local_node = 1, facet_nnode(idx) + node = facet_conn(local_node,idx) + facet_conn_out(local_node,out_idx) = node_id_ext(node) + x(:,local_node,out_idx) = coords(:,local_node) + vnod(:,local_node,out_idx) = velocities(:,local_node) + end do + end do + + ierr = FEM_SUCCESS + end subroutine cpl_get_facets + + subroutine cpl_apply_facet_pressure(nfacet_in, facet_id, pressure, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: facet_id(nfacet_in) + real(rk), intent(in) :: pressure(nfacet_in) + integer, intent(out) :: ierr + real(rk), allocatable :: shear(:,:) + integer :: stat + + allocate(shear(NDIM,nfacet_in), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate zero shear array', ierr) + return + end if + shear = ZERO + call cpl_apply_facet_tractions(nfacet_in, facet_id, pressure, shear, ierr) + deallocate(shear) + end subroutine cpl_apply_facet_pressure + + subroutine cpl_apply_facet_tractions(nfacet_in, facet_id, pressure, shear, ierr) + integer, intent(in) :: nfacet_in + integer, intent(in) :: facet_id(nfacet_in) + real(rk), intent(in) :: pressure(nfacet_in) + real(rk), intent(in) :: shear(NDIM,nfacet_in) + integer, intent(out) :: ierr + integer :: item + integer :: idx + integer :: local_node + integer :: node + integer :: idir + integer :: dof + real(rk) :: coords(NDIM,MAX_FACE_NODES) + real(rk) :: velocities(NDIM,MAX_FACE_NODES) + real(rk) :: nrm(NDIM) + real(rk) :: ar + real(rk) :: traction(NDIM) + real(rk) :: nodal_force(NDIM) + + if (.not. allocated(fsph)) then + call error_set(FEM_ERR_INTERNAL, 'SPH force vector is not allocated', ierr) + return + end if + if (nfacet_in .lt. 0) then + call error_set(FEM_ERR_COUPLING, 'number of SPH facets is negative', ierr) + return + end if + + do item = 1, nfacet_in + idx = find_facet(facet_id(item)) + if (idx .eq. 0) then + call error_set(FEM_ERR_COUPLING, 'SPH facet load references unknown facet', ierr) + return + end if + end do + + do item = 1, nfacet_in + idx = find_facet(facet_id(item)) + if (elem_status(facet_elem(idx)) .ne. 0) cycle + + call facet_current_geometry(idx, coords, velocities, nrm, ar, ierr) + if (ierr .ne. FEM_SUCCESS) return + + traction = -pressure(item) * nrm + shear(:,item) + nodal_force = traction * ar / real(facet_nnode(idx), rk) + + do local_node = 1, facet_nnode(idx) + node = facet_conn(local_node,idx) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fsph(dof) = fsph(dof) + nodal_force(idir) + end do + end do + end do + + ierr = FEM_SUCCESS + end subroutine cpl_apply_facet_tractions + + subroutine count_candidate_faces(max_faces, ierr) + integer, intent(out) :: max_faces + integer, intent(out) :: ierr + integer :: elem + + max_faces = 0 + do elem = 1, nelem + if (elem_status(elem) .ne. 0) cycle + select case (trim(adjustl(elem_type(elem)))) + case ('C3D4') + max_faces = max_faces + 4 + case ('C3D8') + max_faces = max_faces + 6 + case ('S4') + max_faces = max_faces + 1 + case default + call error_set(FEM_ERR_MESH, 'unsupported element type for boundary facets', ierr) + return + end select + end do + + ierr = FEM_SUCCESS + end subroutine count_candidate_faces + + subroutine add_solid_face(elem, face, num_face_nodes, local_nodes, face_count, & + hash, table_size, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + integer, intent(in) :: elem + integer, intent(in) :: face + integer, intent(in) :: num_face_nodes + integer, intent(in) :: local_nodes(MAX_FACE_NODES) + integer, intent(inout) :: face_count + integer, intent(in) :: table_size + integer, intent(inout) :: hash(:) + integer, intent(inout) :: cand_elem(:) + integer, intent(inout) :: cand_face(:) + integer, intent(inout) :: cand_nnode(:) + integer, intent(inout) :: cand_count(:) + integer, intent(inout) :: cand_conn(:,:) + integer, intent(inout) :: cand_key(:,:) + integer, intent(out) :: ierr + integer :: nodes(MAX_FACE_NODES) + integer :: key(MAX_FACE_NODES) + integer :: h + integer :: probe + integer :: slot + integer :: cand + integer :: local_node + + nodes = 0 + key = 0 + do local_node = 1, num_face_nodes + nodes(local_node) = elem_conn(local_nodes(local_node), elem) + key(local_node) = nodes(local_node) + end do + + call orient_solid_face(elem, num_face_nodes, nodes, ierr) + if (ierr .ne. FEM_SUCCESS) return + call sort_face_key(key, num_face_nodes) + + h = face_hash(key, num_face_nodes, table_size) + do probe = 1, table_size + slot = 1 + mod(h + probe - 2, table_size) + cand = hash(slot) + if (cand .eq. 0) then + face_count = face_count + 1 + cand_elem(face_count) = elem + cand_face(face_count) = face + cand_nnode(face_count) = num_face_nodes + cand_count(face_count) = 1 + cand_conn(:,face_count) = nodes + cand_key(:,face_count) = key + hash(slot) = face_count + ierr = FEM_SUCCESS + return + end if + + if (same_face_key(cand_key(:,cand), key, num_face_nodes, cand_nnode(cand))) then + cand_count(cand) = cand_count(cand) + 1 + ierr = FEM_SUCCESS + return + end if + end do + + call error_set(FEM_ERR_INTERNAL, 'facet hash table is full', ierr) + end subroutine add_solid_face + + subroutine add_shell_face(elem, face, num_face_nodes, local_nodes, face_count, & + cand_elem, cand_face, cand_nnode, cand_count, & + cand_conn, cand_key, ierr) + integer, intent(in) :: elem + integer, intent(in) :: face + integer, intent(in) :: num_face_nodes + integer, intent(in) :: local_nodes(MAX_FACE_NODES) + integer, intent(inout) :: face_count + integer, intent(inout) :: cand_elem(:) + integer, intent(inout) :: cand_face(:) + integer, intent(inout) :: cand_nnode(:) + integer, intent(inout) :: cand_count(:) + integer, intent(inout) :: cand_conn(:,:) + integer, intent(inout) :: cand_key(:,:) + integer, intent(out) :: ierr + integer :: local_node + + face_count = face_count + 1 + cand_elem(face_count) = elem + cand_face(face_count) = face + cand_nnode(face_count) = num_face_nodes + cand_count(face_count) = 1 + cand_conn(:,face_count) = 0 + cand_key(:,face_count) = 0 + do local_node = 1, num_face_nodes + cand_conn(local_node,face_count) = elem_conn(local_nodes(local_node),elem) + cand_key(local_node,face_count) = cand_conn(local_node,face_count) + end do + call sort_face_key(cand_key(:,face_count), num_face_nodes) + + ierr = FEM_SUCCESS + end subroutine add_shell_face + + subroutine orient_solid_face(elem, num_face_nodes, nodes, ierr) + integer, intent(in) :: elem + integer, intent(in) :: num_face_nodes + integer, intent(inout) :: nodes(MAX_FACE_NODES) + integer, intent(out) :: ierr + real(rk) :: face_center(NDIM) + real(rk) :: elem_center(NDIM) + real(rk) :: nrm(NDIM) + real(rk) :: ar + integer :: local_node + integer :: node + + elem_center = ZERO + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node,elem) + elem_center = elem_center + coord0(:,node) + end do + elem_center = elem_center / real(elem_nnode(elem), rk) + + face_center = ZERO + do local_node = 1, num_face_nodes + face_center = face_center + coord0(:,nodes(local_node)) + end do + face_center = face_center / real(num_face_nodes, rk) + + call face_geometry_from_nodes(num_face_nodes, nodes, coord0, nrm, ar, ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (dot_product(nrm, face_center - elem_center) .lt. ZERO) then + call reverse_face_nodes(num_face_nodes, nodes) + end if + + ierr = FEM_SUCCESS + end subroutine orient_solid_face + + subroutine facet_current_geometry(idx, coords, velocities, normal, area, ierr) + integer, intent(in) :: idx + real(rk), intent(out) :: coords(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: velocities(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + coords = ZERO + velocities = ZERO + do local_node = 1, facet_nnode(idx) + node = facet_conn(local_node,idx) + coords(:,local_node) = coord0(:,node) + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) then + coords(idir,local_node) = coords(idir,local_node) + u(dof) + velocities(idir,local_node) = v(dof) + end if + end do + end do + + call face_geometry(facet_nnode(idx), coords, normal, area, ierr) + end subroutine facet_current_geometry + + subroutine face_geometry_from_nodes(num_face_nodes, nodes, coordinates, normal, area, ierr) + integer, intent(in) :: num_face_nodes + integer, intent(in) :: nodes(MAX_FACE_NODES) + real(rk), intent(in) :: coordinates(:,:) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + real(rk) :: coords(NDIM,MAX_FACE_NODES) + integer :: local_node + + coords = ZERO + do local_node = 1, num_face_nodes + coords(:,local_node) = coordinates(:,nodes(local_node)) + end do + + call face_geometry(num_face_nodes, coords, normal, area, ierr) + end subroutine face_geometry_from_nodes + + subroutine face_geometry(num_face_nodes, coords, normal, area, ierr) + integer, intent(in) :: num_face_nodes + real(rk), intent(in) :: coords(NDIM,MAX_FACE_NODES) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + real(rk) :: tri_normal(NDIM) + real(rk) :: total_normal(NDIM) + real(rk) :: tri_area + + if (num_face_nodes .eq. 3) then + call triangle_normal_area(coords(:,1), coords(:,2), coords(:,3), & + normal, area, ierr) + return + else if (num_face_nodes .eq. 4) then + call triangle_normal_area(coords(:,1), coords(:,2), coords(:,3), & + total_normal, area, ierr) + if (ierr .ne. FEM_SUCCESS) return + call triangle_normal_area(coords(:,1), coords(:,3), coords(:,4), & + tri_normal, tri_area, ierr) + if (ierr .ne. FEM_SUCCESS) return + total_normal = total_normal * area + tri_normal * tri_area + area = area + tri_area + if (area .le. ZERO) then + call error_set(FEM_ERR_MESH, 'facet has non-positive area', ierr) + return + end if + normal = total_normal / sqrt(sum(total_normal * total_normal)) + else + call error_set(FEM_ERR_MESH, 'unsupported number of facet nodes', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine face_geometry + + subroutine triangle_normal_area(a, b, c, normal, area, ierr) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(in) :: c(NDIM) + real(rk), intent(out) :: normal(NDIM) + real(rk), intent(out) :: area + integer, intent(out) :: ierr + real(rk) :: ab(NDIM) + real(rk) :: ac(NDIM) + real(rk) :: cr(NDIM) + real(rk) :: norm_cr + + ab = b - a + ac = c - a + call cross3(ab, ac, cr) + norm_cr = sqrt(sum(cr * cr)) + if (norm_cr .le. ZERO) then + call error_set(FEM_ERR_MESH, 'facet has zero area triangle', ierr) + return + end if + + area = HALF * norm_cr + normal = cr / norm_cr + ierr = FEM_SUCCESS + end subroutine triangle_normal_area + + subroutine cross3(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(out) :: c(NDIM) + + c(1) = a(2) * b(3) - a(3) * b(2) + c(2) = a(3) * b(1) - a(1) * b(3) + c(3) = a(1) * b(2) - a(2) * b(1) + end subroutine cross3 + + subroutine reverse_face_nodes(num_face_nodes, nodes) + integer, intent(in) :: num_face_nodes + integer, intent(inout) :: nodes(MAX_FACE_NODES) + integer :: tmp(MAX_FACE_NODES) + integer :: local_node + + tmp = nodes + do local_node = 1, num_face_nodes + nodes(local_node) = tmp(num_face_nodes - local_node + 1) + end do + end subroutine reverse_face_nodes + + subroutine sort_face_key(key, num_face_nodes) + integer, intent(inout) :: key(MAX_FACE_NODES) + integer, intent(in) :: num_face_nodes + integer :: i + integer :: j + integer :: value + + do i = 2, num_face_nodes + value = key(i) + j = i - 1 + do while (j .ge. 1) + if (key(j) .le. value) exit + key(j + 1) = key(j) + j = j - 1 + end do + key(j + 1) = value + end do + do i = num_face_nodes + 1, MAX_FACE_NODES + key(i) = 0 + end do + end subroutine sort_face_key + + integer function face_hash(key, num_face_nodes, table_size) + integer, intent(in) :: key(MAX_FACE_NODES) + integer, intent(in) :: num_face_nodes + integer, intent(in) :: table_size + integer :: i + integer :: h + + h = 0 + do i = 1, num_face_nodes + h = mod(131 * h + 17 * key(i) + i, table_size) + end do + face_hash = h + 1 + end function face_hash + + logical function same_face_key(candidate_key, key, num_face_nodes, candidate_nnode) + integer, intent(in) :: candidate_key(MAX_FACE_NODES) + integer, intent(in) :: key(MAX_FACE_NODES) + integer, intent(in) :: num_face_nodes + integer, intent(in) :: candidate_nnode + integer :: i + + same_face_key = .false. + if (candidate_nnode .ne. num_face_nodes) return + do i = 1, MAX_FACE_NODES + if (candidate_key(i) .ne. key(i)) return + end do + same_face_key = .true. + end function same_face_key + + integer function find_facet(facet_id) + integer, intent(in) :: facet_id + + if (facet_id .ge. 1 .and. facet_id .le. nfacet) then + find_facet = facet_id + else + find_facet = 0 + end if + end function find_facet + +end module fem_coupling diff --git a/src/element/fem_element_internal.f90 b/src/element/fem_element_internal.f90 new file mode 100644 index 0000000..f27e44d --- /dev/null +++ b/src/element/fem_element_internal.f90 @@ -0,0 +1,991 @@ +module fem_element_internal + use fem_precision + use fem_error + use fem_database + use fem_material_j2 + implicit none + +contains + + subroutine element_compute_internal_force(ierr, dt) + integer, intent(out) :: ierr + real(rk), intent(in), optional :: dt + integer :: elem + real(rk) :: dt_local + + if (.not. allocated(fint)) then + call error_set(FEM_ERR_INTERNAL, 'internal force vector is not allocated', ierr) + return + end if + + call ensure_element_state(ierr) + if (ierr .ne. FEM_SUCCESS) return + + dt_local = ZERO + if (present(dt)) dt_local = dt + + fint = ZERO + + do elem = 1, nelem + if (elem_status(elem) .ne. 0) cycle + + select case (trim(adjustl(elem_type(elem)))) + case ('C3D4') + call c3d4_internal_force(elem, dt_local, ierr) + if (ierr .ne. FEM_SUCCESS) return + case ('C3D8') + call c3d8_internal_force(elem, dt_local, ierr) + if (ierr .ne. FEM_SUCCESS) return + case ('S4') + call shell4_internal_force(elem, ierr) + if (ierr .ne. FEM_SUCCESS) return + case default + cycle + end select + end do + + ierr = FEM_SUCCESS + end subroutine element_compute_internal_force + + subroutine ensure_element_state(ierr) + integer, intent(out) :: ierr + integer :: elem + integer :: gp + integer :: stat + + if (allocated(elem_gp_start)) then + ierr = FEM_SUCCESS + return + end if + + allocate(elem_gp_start(nelem), elem_ngp(nelem), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state map', ierr) + return + end if + + elem_gp_start = 0 + elem_ngp = 0 + ngauss_total = 0 + + do elem = 1, nelem + select case (trim(adjustl(elem_type(elem)))) + case ('C3D4') + elem_ngp(elem) = 1 + case ('C3D8') + elem_ngp(elem) = 8 + case ('S4') + elem_ngp(elem) = 4 + case default + elem_ngp(elem) = 0 + end select + + if (elem_ngp(elem) .gt. 0) then + elem_gp_start(elem) = ngauss_total + 1 + ngauss_total = ngauss_total + elem_ngp(elem) + end if + end do + + if (ngauss_total .gt. 0) then + allocate(stress(NSTRESS,ngauss_total), strain(NSTRESS,ngauss_total), & + statev(NSTATEV_MAX,ngauss_total), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state arrays', ierr) + return + end if + stress = ZERO + strain = ZERO + statev = ZERO + end if + + do gp = 1, ngauss_total + statev(1,gp) = ZERO + end do + + ierr = FEM_SUCCESS + end subroutine ensure_element_state + + subroutine c3d4_internal_force(elem, dt, ierr) + integer, intent(in) :: elem + real(rk), intent(in) :: dt + integer, intent(out) :: ierr + real(rk) :: dndx(NDIM,4) + real(rk) :: eps(NSTRESS) + real(rk) :: deps(NSTRESS) + real(rk) :: sig(NSTRESS) + real(rk) :: state_old(NSTATEV_MAX) + real(rk) :: state_new(NSTATEV_MAX) + real(rk) :: stress_old(NSTRESS) + real(rk) :: spin_current(3) + real(rk) :: spin_inc(3) + real(rk) :: volume + integer :: local_node + integer :: node + integer :: dof + integer :: gp + + call c3d4_shape_gradients(elem, dndx, volume, ierr) + if (ierr .ne. FEM_SUCCESS) return + + eps = ZERO + do local_node = 1, 4 + node = elem_conn(local_node, elem) + eps(1) = eps(1) + dndx(1,local_node) * get_dof_value(u, 1, node) + eps(2) = eps(2) + dndx(2,local_node) * get_dof_value(u, 2, node) + eps(3) = eps(3) + dndx(3,local_node) * get_dof_value(u, 3, node) + eps(4) = eps(4) + dndx(2,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 2, node) + eps(5) = eps(5) + dndx(3,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 3, node) + eps(6) = eps(6) + dndx(3,local_node) * get_dof_value(u, 2, node) + & + dndx(2,local_node) * get_dof_value(u, 3, node) + end do + + gp = elem_gp_start(elem) + deps = eps + stress_old = ZERO + state_old = ZERO + if (gp .gt. 0) then + deps = eps - strain(:,gp) + stress_old = stress(:,gp) + state_old = statev(:,gp) + end if + call solid_current_spin(elem, 4, dndx, spin_current) + spin_inc = spin_current - state_old(14:16) + + call material_update_small_strain_dt(elem_mat(elem), dt, deps, state_old, & + stress_old, sig, state_new, ierr, spin_inc) + if (ierr .ne. FEM_SUCCESS) return + state_new(14:16) = spin_current + + if (gp .gt. 0) then + strain(:,gp) = eps + stress(:,gp) = sig + statev(:,gp) = state_new + if (state_new(4) .ge. HALF) then + elem_status(elem) = 1 + stress(:,gp) = ZERO + return + end if + end if + + do local_node = 1, 4 + node = elem_conn(local_node, elem) + + dof = dof_id(1,node) + if (dof .gt. 0) then + fint(dof) = fint(dof) + volume * & + (dndx(1,local_node) * sig(1) + dndx(2,local_node) * sig(4) + & + dndx(3,local_node) * sig(5)) + end if + + dof = dof_id(2,node) + if (dof .gt. 0) then + fint(dof) = fint(dof) + volume * & + (dndx(1,local_node) * sig(4) + dndx(2,local_node) * sig(2) + & + dndx(3,local_node) * sig(6)) + end if + + dof = dof_id(3,node) + if (dof .gt. 0) then + fint(dof) = fint(dof) + volume * & + (dndx(1,local_node) * sig(5) + dndx(2,local_node) * sig(6) + & + dndx(3,local_node) * sig(3)) + end if + end do + + ierr = FEM_SUCCESS + end subroutine c3d4_internal_force + + subroutine c3d8_internal_force(elem, dt, ierr) + integer, intent(in) :: elem + real(rk), intent(in) :: dt + integer, intent(out) :: ierr + real(rk) :: xi_node(8) + real(rk) :: eta_node(8) + real(rk) :: zeta_node(8) + real(rk) :: gp_loc(2) + real(rk) :: dndr(NDIM,8) + real(rk) :: dndx(NDIM,8) + real(rk) :: detj + real(rk) :: eps(NSTRESS) + real(rk) :: deps(NSTRESS) + real(rk) :: sig(NSTRESS) + real(rk) :: state_old(NSTATEV_MAX) + real(rk) :: state_new(NSTATEV_MAX) + real(rk) :: stress_old(NSTRESS) + real(rk) :: spin_current(3) + real(rk) :: spin_inc(3) + real(rk) :: local_force(NDIM,8) + real(rk) :: r + real(rk) :: s + real(rk) :: t + real(rk) :: weight + integer :: i + integer :: j + integer :: k + integer :: local_node + integer :: node + integer :: gp + integer :: gp_start + integer :: failed_count + integer :: dof + + if (elem_nnode(elem) .ne. 8) then + call error_set(FEM_ERR_MESH, 'C3D8 element must have 8 nodes', ierr) + return + end if + + call hex8_natural_coords(xi_node, eta_node, zeta_node) + gp_loc(1) = -ONE / sqrt(3.0_rk) + gp_loc(2) = ONE / sqrt(3.0_rk) + gp_start = elem_gp_start(elem) + gp = gp_start + failed_count = 0 + local_force = ZERO + + do i = 1, 2 + r = gp_loc(i) + do j = 1, 2 + s = gp_loc(j) + do k = 1, 2 + t = gp_loc(k) + weight = ONE + + call hex8_shape_gradients(elem, r, s, t, xi_node, eta_node, & + zeta_node, dndr, dndx, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return + + eps = ZERO + do local_node = 1, 8 + node = elem_conn(local_node, elem) + eps(1) = eps(1) + dndx(1,local_node) * get_dof_value(u, 1, node) + eps(2) = eps(2) + dndx(2,local_node) * get_dof_value(u, 2, node) + eps(3) = eps(3) + dndx(3,local_node) * get_dof_value(u, 3, node) + eps(4) = eps(4) + dndx(2,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 2, node) + eps(5) = eps(5) + dndx(3,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 3, node) + eps(6) = eps(6) + dndx(3,local_node) * get_dof_value(u, 2, node) + & + dndx(2,local_node) * get_dof_value(u, 3, node) + end do + + deps = eps + stress_old = ZERO + state_old = ZERO + if (gp .gt. 0) then + deps = eps - strain(:,gp) + stress_old = stress(:,gp) + state_old = statev(:,gp) + end if + call solid_current_spin(elem, 8, dndx, spin_current) + spin_inc = spin_current - state_old(14:16) + + call material_update_small_strain_dt(elem_mat(elem), dt, deps, & + state_old, stress_old, sig, & + state_new, ierr, spin_inc) + if (ierr .ne. FEM_SUCCESS) return + state_new(14:16) = spin_current + + if (gp .gt. 0) then + strain(:,gp) = eps + stress(:,gp) = sig + statev(:,gp) = state_new + if (state_new(4) .ge. HALF) failed_count = failed_count + 1 + end if + + do local_node = 1, 8 + node = elem_conn(local_node, elem) + + dof = dof_id(1,node) + if (dof .gt. 0) then + local_force(1,local_node) = local_force(1,local_node) + weight * detj * & + (dndx(1,local_node) * sig(1) + dndx(2,local_node) * sig(4) + & + dndx(3,local_node) * sig(5)) + end if + + dof = dof_id(2,node) + if (dof .gt. 0) then + local_force(2,local_node) = local_force(2,local_node) + weight * detj * & + (dndx(1,local_node) * sig(4) + dndx(2,local_node) * sig(2) + & + dndx(3,local_node) * sig(6)) + end if + + dof = dof_id(3,node) + if (dof .gt. 0) then + local_force(3,local_node) = local_force(3,local_node) + weight * detj * & + (dndx(1,local_node) * sig(5) + dndx(2,local_node) * sig(6) + & + dndx(3,local_node) * sig(3)) + end if + end do + + gp = gp + 1 + end do + end do + end do + + if (failed_count .ge. 4) then + elem_status(elem) = 1 + if (gp_start .gt. 0) stress(:,gp_start:gp_start+7) = ZERO + else + do local_node = 1, 8 + node = elem_conn(local_node, elem) + dof = dof_id(1,node) + if (dof .gt. 0) fint(dof) = fint(dof) + local_force(1,local_node) + dof = dof_id(2,node) + if (dof .gt. 0) fint(dof) = fint(dof) + local_force(2,local_node) + dof = dof_id(3,node) + if (dof .gt. 0) fint(dof) = fint(dof) + local_force(3,local_node) + end do + end if + + ierr = FEM_SUCCESS + end subroutine c3d8_internal_force + + subroutine shell4_internal_force(elem, ierr) + integer, intent(in) :: elem + integer, intent(out) :: ierr + real(rk) :: basis(NDIM,NDIM) + real(rk) :: xloc(2,4) + real(rk) :: qloc(5,4) + real(rk) :: floc(5,4) + real(rk) :: gp_loc(2) + real(rk) :: nshape(4) + real(rk) :: dndx(4) + real(rk) :: dndy(4) + real(rk) :: strain_mem(3) + real(rk) :: strain_ben(3) + real(rk) :: strain_shr(2) + real(rk) :: force_mem(3) + real(rk) :: force_ben(3) + real(rk) :: force_shr(2) + real(rk) :: young + real(rk) :: poisson + real(rk) :: thickness + real(rk) :: shear_mod + real(rk) :: coeff_mem + real(rk) :: coeff_ben + real(rk) :: coeff_shr + real(rk) :: detj + real(rk) :: weight + real(rk) :: r + real(rk) :: s + integer :: i + integer :: j + integer :: gp + integer :: gp_start + integer :: local_node + integer :: mat + integer :: sec + + if (elem_nnode(elem) .ne. 4) then + call error_set(FEM_ERR_MESH, 'S4 element must have 4 nodes', ierr) + return + end if + + mat = elem_mat(elem) + sec = elem_sec(elem) + if (mat .lt. 1 .or. mat .gt. n_material) then + call error_set(FEM_ERR_MATERIAL, 'S4 element has invalid material index', ierr) + return + end if + if (sec .lt. 1 .or. sec .gt. n_section) then + call error_set(FEM_ERR_INPUT, 'S4 element has invalid section index', ierr) + return + end if + + young = mat_e(mat) + poisson = mat_nu(mat) + thickness = section_thickness(sec) + if (young .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'S4 elastic modulus must be positive', ierr) + return + end if + if (poisson .le. -ONE .or. poisson .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'S4 Poisson ratio must be in (-1,0.5)', ierr) + return + end if + if (thickness .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'S4 section thickness must be positive', ierr) + return + end if + + call shell4_basis(elem, basis, xloc, ierr) + if (ierr .ne. FEM_SUCCESS) return + call shell4_local_dofs(elem, basis, qloc) + + shear_mod = young / (2.0_rk * (ONE + poisson)) + coeff_mem = young * thickness / (ONE - poisson * poisson) + coeff_ben = young * thickness**3 / & + (12.0_rk * (ONE - poisson * poisson)) + coeff_shr = (5.0_rk / 6.0_rk) * shear_mod * thickness + + gp_loc(1) = -ONE / sqrt(3.0_rk) + gp_loc(2) = ONE / sqrt(3.0_rk) + gp_start = elem_gp_start(elem) + gp = gp_start + floc = ZERO + + do i = 1, 2 + r = gp_loc(i) + do j = 1, 2 + s = gp_loc(j) + call shell4_shape_xy(xloc, r, s, nshape, dndx, dndy, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return + weight = detj + + call shell4_strains(nshape, dndx, dndy, qloc, strain_mem, & + strain_ben, strain_shr) + call shell4_resultants(coeff_mem, coeff_ben, coeff_shr, poisson, & + strain_mem, strain_ben, strain_shr, & + force_mem, force_ben, force_shr) + + do local_node = 1, 4 + floc(1,local_node) = floc(1,local_node) + weight * & + (dndx(local_node) * force_mem(1) + & + dndy(local_node) * force_mem(3)) + floc(2,local_node) = floc(2,local_node) + weight * & + (dndy(local_node) * force_mem(2) + & + dndx(local_node) * force_mem(3)) + floc(3,local_node) = floc(3,local_node) + weight * & + (dndx(local_node) * force_shr(1) + & + dndy(local_node) * force_shr(2)) + floc(4,local_node) = floc(4,local_node) + weight * & + (-dndy(local_node) * force_ben(2) - & + dndx(local_node) * force_ben(3) - & + nshape(local_node) * force_shr(2)) + floc(5,local_node) = floc(5,local_node) + weight * & + (dndx(local_node) * force_ben(1) + & + dndy(local_node) * force_ben(3) + & + nshape(local_node) * force_shr(1)) + end do + + if (gp .gt. 0) then + strain(:,gp) = ZERO + stress(:,gp) = ZERO + statev(:,gp) = ZERO + strain(1,gp) = strain_mem(1) + strain(2,gp) = strain_mem(2) + strain(4,gp) = strain_mem(3) + strain(5,gp) = strain_shr(1) + strain(6,gp) = strain_shr(2) + stress(1,gp) = force_mem(1) / thickness + stress(2,gp) = force_mem(2) / thickness + stress(4,gp) = force_mem(3) / thickness + stress(5,gp) = force_shr(1) / thickness + stress(6,gp) = force_shr(2) / thickness + statev(10,gp) = strain_ben(1) + statev(11,gp) = strain_ben(2) + statev(12,gp) = strain_ben(3) + end if + gp = gp + 1 + end do + end do + + call shell4_add_global(elem, basis, floc) + ierr = FEM_SUCCESS + end subroutine shell4_internal_force + + subroutine shell4_basis(elem, basis, xloc, ierr) + integer, intent(in) :: elem + real(rk), intent(out) :: basis(NDIM,NDIM) + real(rk), intent(out) :: xloc(2,4) + integer, intent(out) :: ierr + real(rk) :: origin(NDIM) + real(rk) :: x1(NDIM) + real(rk) :: x2(NDIM) + real(rk) :: x3(NDIM) + real(rk) :: x4(NDIM) + real(rk) :: v12(NDIM) + real(rk) :: v14(NDIM) + real(rk) :: normal(NDIM) + real(rk) :: tmp(NDIM) + real(rk) :: len1 + real(rk) :: lenn + integer :: local_node + integer :: node + + x1 = coord0(:, elem_conn(1,elem)) + x2 = coord0(:, elem_conn(2,elem)) + x3 = coord0(:, elem_conn(3,elem)) + x4 = coord0(:, elem_conn(4,elem)) + origin = x1 + v12 = x2 - x1 + v14 = x4 - x1 + + len1 = sqrt(sum(v12 * v12)) + if (len1 .le. ZERO) then + call error_set(FEM_ERR_MESH, 'S4 has coincident first edge nodes', ierr) + return + end if + basis(:,1) = v12 / len1 + + call cross3(v12, v14, normal) + lenn = sqrt(sum(normal * normal)) + if (lenn .le. ZERO) then + tmp = x3 - x1 + call cross3(v12, tmp, normal) + lenn = sqrt(sum(normal * normal)) + end if + if (lenn .le. ZERO) then + call error_set(FEM_ERR_MESH, 'S4 local basis is singular', ierr) + return + end if + basis(:,3) = normal / lenn + call cross3(basis(:,3), basis(:,1), basis(:,2)) + + do local_node = 1, 4 + node = elem_conn(local_node, elem) + tmp = coord0(:,node) - origin + xloc(1,local_node) = dot_product(tmp, basis(:,1)) + xloc(2,local_node) = dot_product(tmp, basis(:,2)) + end do + + ierr = FEM_SUCCESS + end subroutine shell4_basis + + subroutine shell4_local_dofs(elem, basis, qloc) + integer, intent(in) :: elem + real(rk), intent(in) :: basis(NDIM,NDIM) + real(rk), intent(out) :: qloc(5,4) + real(rk) :: disp(NDIM) + real(rk) :: rot(NDIM) + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + qloc = ZERO + do local_node = 1, 4 + node = elem_conn(local_node, elem) + disp = ZERO + rot = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) disp(idir) = u(dof) + dof = dof_id(idir + 3,node) + if (dof .gt. 0) rot(idir) = u(dof) + end do + qloc(1,local_node) = dot_product(disp, basis(:,1)) + qloc(2,local_node) = dot_product(disp, basis(:,2)) + qloc(3,local_node) = dot_product(disp, basis(:,3)) + qloc(4,local_node) = dot_product(rot, basis(:,1)) + qloc(5,local_node) = dot_product(rot, basis(:,2)) + end do + end subroutine shell4_local_dofs + + subroutine shell4_shape_xy(xloc, r, s, nshape, dndx, dndy, detj, ierr) + real(rk), intent(in) :: xloc(2,4) + real(rk), intent(in) :: r + real(rk), intent(in) :: s + real(rk), intent(out) :: nshape(4) + real(rk), intent(out) :: dndx(4) + real(rk), intent(out) :: dndy(4) + real(rk), intent(out) :: detj + integer, intent(out) :: ierr + real(rk) :: dndr(4) + real(rk) :: dnds(4) + real(rk) :: jac(2,2) + real(rk) :: invj(2,2) + integer :: local_node + + nshape(1) = 0.25_rk * (ONE - r) * (ONE - s) + nshape(2) = 0.25_rk * (ONE + r) * (ONE - s) + nshape(3) = 0.25_rk * (ONE + r) * (ONE + s) + nshape(4) = 0.25_rk * (ONE - r) * (ONE + s) + + dndr(1) = -0.25_rk * (ONE - s) + dndr(2) = 0.25_rk * (ONE - s) + dndr(3) = 0.25_rk * (ONE + s) + dndr(4) = -0.25_rk * (ONE + s) + + dnds(1) = -0.25_rk * (ONE - r) + dnds(2) = -0.25_rk * (ONE + r) + dnds(3) = 0.25_rk * (ONE + r) + dnds(4) = 0.25_rk * (ONE - r) + + jac = ZERO + do local_node = 1, 4 + jac(1,1) = jac(1,1) + xloc(1,local_node) * dndr(local_node) + jac(1,2) = jac(1,2) + xloc(1,local_node) * dnds(local_node) + jac(2,1) = jac(2,1) + xloc(2,local_node) * dndr(local_node) + jac(2,2) = jac(2,2) + xloc(2,local_node) * dnds(local_node) + end do + + detj = jac(1,1) * jac(2,2) - jac(1,2) * jac(2,1) + if (detj .le. ZERO) then + call error_set(FEM_ERR_MESH, 'S4 element has non-positive Jacobian', ierr) + return + end if + + invj(1,1) = jac(2,2) / detj + invj(1,2) = -jac(1,2) / detj + invj(2,1) = -jac(2,1) / detj + invj(2,2) = jac(1,1) / detj + + do local_node = 1, 4 + dndx(local_node) = dndr(local_node) * invj(1,1) + & + dnds(local_node) * invj(2,1) + dndy(local_node) = dndr(local_node) * invj(1,2) + & + dnds(local_node) * invj(2,2) + end do + + ierr = FEM_SUCCESS + end subroutine shell4_shape_xy + + subroutine shell4_strains(nshape, dndx, dndy, qloc, strain_mem, & + strain_ben, strain_shr) + real(rk), intent(in) :: nshape(4) + real(rk), intent(in) :: dndx(4) + real(rk), intent(in) :: dndy(4) + real(rk), intent(in) :: qloc(5,4) + real(rk), intent(out) :: strain_mem(3) + real(rk), intent(out) :: strain_ben(3) + real(rk), intent(out) :: strain_shr(2) + integer :: local_node + + strain_mem = ZERO + strain_ben = ZERO + strain_shr = ZERO + + do local_node = 1, 4 + strain_mem(1) = strain_mem(1) + dndx(local_node) * qloc(1,local_node) + strain_mem(2) = strain_mem(2) + dndy(local_node) * qloc(2,local_node) + strain_mem(3) = strain_mem(3) + dndy(local_node) * qloc(1,local_node) + & + dndx(local_node) * qloc(2,local_node) + + strain_ben(1) = strain_ben(1) + dndx(local_node) * qloc(5,local_node) + strain_ben(2) = strain_ben(2) - dndy(local_node) * qloc(4,local_node) + strain_ben(3) = strain_ben(3) - dndx(local_node) * qloc(4,local_node) + & + dndy(local_node) * qloc(5,local_node) + + strain_shr(1) = strain_shr(1) + dndx(local_node) * qloc(3,local_node) + & + nshape(local_node) * qloc(5,local_node) + strain_shr(2) = strain_shr(2) + dndy(local_node) * qloc(3,local_node) - & + nshape(local_node) * qloc(4,local_node) + end do + end subroutine shell4_strains + + subroutine shell4_resultants(coeff_mem, coeff_ben, coeff_shr, poisson, & + strain_mem, strain_ben, strain_shr, & + force_mem, force_ben, force_shr) + real(rk), intent(in) :: coeff_mem + real(rk), intent(in) :: coeff_ben + real(rk), intent(in) :: coeff_shr + real(rk), intent(in) :: poisson + real(rk), intent(in) :: strain_mem(3) + real(rk), intent(in) :: strain_ben(3) + real(rk), intent(in) :: strain_shr(2) + real(rk), intent(out) :: force_mem(3) + real(rk), intent(out) :: force_ben(3) + real(rk), intent(out) :: force_shr(2) + + force_mem(1) = coeff_mem * (strain_mem(1) + poisson * strain_mem(2)) + force_mem(2) = coeff_mem * (poisson * strain_mem(1) + strain_mem(2)) + force_mem(3) = coeff_mem * HALF * (ONE - poisson) * strain_mem(3) + + force_ben(1) = coeff_ben * (strain_ben(1) + poisson * strain_ben(2)) + force_ben(2) = coeff_ben * (poisson * strain_ben(1) + strain_ben(2)) + force_ben(3) = coeff_ben * HALF * (ONE - poisson) * strain_ben(3) + + force_shr(1) = coeff_shr * strain_shr(1) + force_shr(2) = coeff_shr * strain_shr(2) + end subroutine shell4_resultants + + subroutine shell4_add_global(elem, basis, floc) + integer, intent(in) :: elem + real(rk), intent(in) :: basis(NDIM,NDIM) + real(rk), intent(in) :: floc(5,4) + real(rk) :: fglob(NDIM) + real(rk) :: mglob(NDIM) + integer :: local_node + integer :: node + integer :: idir + integer :: dof + + do local_node = 1, 4 + node = elem_conn(local_node, elem) + fglob = floc(1,local_node) * basis(:,1) + & + floc(2,local_node) * basis(:,2) + & + floc(3,local_node) * basis(:,3) + mglob = floc(4,local_node) * basis(:,1) + & + floc(5,local_node) * basis(:,2) + + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) fint(dof) = fint(dof) + fglob(idir) + dof = dof_id(idir + 3,node) + if (dof .gt. 0) fint(dof) = fint(dof) + mglob(idir) + end do + end do + end subroutine shell4_add_global + + subroutine solid_current_spin(elem, nlocal, dndx, spin_current) + integer, intent(in) :: elem + integer, intent(in) :: nlocal + real(rk), intent(in) :: dndx(NDIM,nlocal) + real(rk), intent(out) :: spin_current(3) + real(rk) :: gradu(NDIM,NDIM) + integer :: local_node + integer :: node + integer :: idir + integer :: jdir + + gradu = ZERO + do local_node = 1, nlocal + node = elem_conn(local_node, elem) + do idir = 1, NDIM + do jdir = 1, NDIM + gradu(idir,jdir) = gradu(idir,jdir) + & + get_dof_value(u, idir, node) * dndx(jdir,local_node) + end do + end do + end do + + spin_current(1) = HALF * (gradu(1,2) - gradu(2,1)) + spin_current(2) = HALF * (gradu(1,3) - gradu(3,1)) + spin_current(3) = HALF * (gradu(2,3) - gradu(3,2)) + end subroutine solid_current_spin + + subroutine elastic_stress_3d(mat, eps, sig, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: eps(NSTRESS) + real(rk), intent(out) :: sig(NSTRESS) + integer, intent(out) :: ierr + real(rk) :: lambda + real(rk) :: mu + real(rk) :: trace_eps + real(rk) :: young + real(rk) :: poisson + + if (mat .lt. 1 .or. mat .gt. n_material) then + call error_set(FEM_ERR_MATERIAL, 'invalid material index for elastic stress', ierr) + return + end if + + young = mat_e(mat) + poisson = mat_nu(mat) + if (young .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'elastic modulus must be positive', ierr) + return + end if + if (poisson .le. -ONE .or. poisson .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'Poisson ratio must be in (-1,0.5)', ierr) + return + end if + + lambda = young * poisson / ((ONE + poisson) * (ONE - 2.0_rk * poisson)) + mu = young / (2.0_rk * (ONE + poisson)) + trace_eps = eps(1) + eps(2) + eps(3) + + sig(1) = lambda * trace_eps + 2.0_rk * mu * eps(1) + sig(2) = lambda * trace_eps + 2.0_rk * mu * eps(2) + sig(3) = lambda * trace_eps + 2.0_rk * mu * eps(3) + sig(4) = mu * eps(4) + sig(5) = mu * eps(5) + sig(6) = mu * eps(6) + + ierr = FEM_SUCCESS + end subroutine elastic_stress_3d + + subroutine c3d4_shape_gradients(elem, dndx, volume, ierr) + integer, intent(in) :: elem + real(rk), intent(out) :: dndx(NDIM,4) + real(rk), intent(out) :: volume + integer, intent(out) :: ierr + real(rk) :: x1(NDIM) + real(rk) :: x2(NDIM) + real(rk) :: x3(NDIM) + real(rk) :: x4(NDIM) + real(rk) :: jac(3,3) + real(rk) :: invj(3,3) + real(rk) :: detj + real(rk) :: dndr(3,4) + integer :: local_node + integer :: i + integer :: j + + if (elem_nnode(elem) .ne. 4) then + call error_set(FEM_ERR_MESH, 'C3D4 element must have 4 nodes', ierr) + return + end if + + x1 = coord0(:, elem_conn(1,elem)) + x2 = coord0(:, elem_conn(2,elem)) + x3 = coord0(:, elem_conn(3,elem)) + x4 = coord0(:, elem_conn(4,elem)) + + jac(:,1) = x2 - x1 + jac(:,2) = x3 - x1 + jac(:,3) = x4 - x1 + + call inverse3(jac, invj, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (detj .le. ZERO) then + call error_set(FEM_ERR_MESH, 'C3D4 element has non-positive Jacobian', ierr) + return + end if + + volume = detj / 6.0_rk + + dndr(:,1) = (/ -ONE, -ONE, -ONE /) + dndr(:,2) = (/ ONE, ZERO, ZERO /) + dndr(:,3) = (/ ZERO, ONE, ZERO /) + dndr(:,4) = (/ ZERO, ZERO, ONE /) + + dndx = ZERO + do local_node = 1, 4 + do i = 1, NDIM + do j = 1, NDIM + dndx(i,local_node) = dndx(i,local_node) + invj(j,i) * dndr(j,local_node) + end do + end do + end do + + ierr = FEM_SUCCESS + end subroutine c3d4_shape_gradients + + subroutine hex8_natural_coords(xi, eta, zeta) + real(rk), intent(out) :: xi(8) + real(rk), intent(out) :: eta(8) + real(rk), intent(out) :: zeta(8) + + xi(1) = -ONE + xi(2) = ONE + xi(3) = ONE + xi(4) = -ONE + xi(5) = -ONE + xi(6) = ONE + xi(7) = ONE + xi(8) = -ONE + + eta(1) = -ONE + eta(2) = -ONE + eta(3) = ONE + eta(4) = ONE + eta(5) = -ONE + eta(6) = -ONE + eta(7) = ONE + eta(8) = ONE + + zeta(1) = -ONE + zeta(2) = -ONE + zeta(3) = -ONE + zeta(4) = -ONE + zeta(5) = ONE + zeta(6) = ONE + zeta(7) = ONE + zeta(8) = ONE + end subroutine hex8_natural_coords + + subroutine hex8_shape_gradients(elem, r, s, t, xi_node, eta_node, & + zeta_node, dndr, dndx, detj, ierr) + integer, intent(in) :: elem + real(rk), intent(in) :: r + real(rk), intent(in) :: s + real(rk), intent(in) :: t + real(rk), intent(in) :: xi_node(8) + real(rk), intent(in) :: eta_node(8) + real(rk), intent(in) :: zeta_node(8) + real(rk), intent(out) :: dndr(NDIM,8) + real(rk), intent(out) :: dndx(NDIM,8) + real(rk), intent(out) :: detj + integer, intent(out) :: ierr + real(rk) :: jac(3,3) + real(rk) :: invj(3,3) + integer :: local_node + integer :: node + integer :: i + integer :: j + + do local_node = 1, 8 + dndr(1,local_node) = 0.125_rk * xi_node(local_node) * & + (ONE + s * eta_node(local_node)) * (ONE + t * zeta_node(local_node)) + dndr(2,local_node) = 0.125_rk * eta_node(local_node) * & + (ONE + r * xi_node(local_node)) * (ONE + t * zeta_node(local_node)) + dndr(3,local_node) = 0.125_rk * zeta_node(local_node) * & + (ONE + r * xi_node(local_node)) * (ONE + s * eta_node(local_node)) + end do + + jac = ZERO + do local_node = 1, 8 + node = elem_conn(local_node, elem) + jac(1,1) = jac(1,1) + coord0(1,node) * dndr(1,local_node) + jac(1,2) = jac(1,2) + coord0(1,node) * dndr(2,local_node) + jac(1,3) = jac(1,3) + coord0(1,node) * dndr(3,local_node) + jac(2,1) = jac(2,1) + coord0(2,node) * dndr(1,local_node) + jac(2,2) = jac(2,2) + coord0(2,node) * dndr(2,local_node) + jac(2,3) = jac(2,3) + coord0(2,node) * dndr(3,local_node) + jac(3,1) = jac(3,1) + coord0(3,node) * dndr(1,local_node) + jac(3,2) = jac(3,2) + coord0(3,node) * dndr(2,local_node) + jac(3,3) = jac(3,3) + coord0(3,node) * dndr(3,local_node) + end do + + call inverse3(jac, invj, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return + if (detj .le. ZERO) then + call error_set(FEM_ERR_MESH, 'C3D8 element has non-positive Jacobian', ierr) + return + end if + + dndx = ZERO + do local_node = 1, 8 + do i = 1, NDIM + do j = 1, NDIM + dndx(i,local_node) = dndx(i,local_node) + invj(j,i) * dndr(j,local_node) + end do + end do + end do + + ierr = FEM_SUCCESS + end subroutine hex8_shape_gradients + + subroutine inverse3(a, ainv, det, ierr) + real(rk), intent(in) :: a(3,3) + real(rk), intent(out) :: ainv(3,3) + real(rk), intent(out) :: det + integer, intent(out) :: ierr + + det = a(1,1) * (a(2,2) * a(3,3) - a(2,3) * a(3,2)) - & + a(1,2) * (a(2,1) * a(3,3) - a(2,3) * a(3,1)) + & + a(1,3) * (a(2,1) * a(3,2) - a(2,2) * a(3,1)) + if (abs(det) .le. ZERO) then + call error_set(FEM_ERR_MESH, 'singular 3x3 matrix', ierr) + return + end if + + ainv(1,1) = (a(2,2) * a(3,3) - a(2,3) * a(3,2)) / det + ainv(1,2) = -(a(1,2) * a(3,3) - a(1,3) * a(3,2)) / det + ainv(1,3) = (a(1,2) * a(2,3) - a(1,3) * a(2,2)) / det + ainv(2,1) = -(a(2,1) * a(3,3) - a(2,3) * a(3,1)) / det + ainv(2,2) = (a(1,1) * a(3,3) - a(1,3) * a(3,1)) / det + ainv(2,3) = -(a(1,1) * a(2,3) - a(1,3) * a(2,1)) / det + ainv(3,1) = (a(2,1) * a(3,2) - a(2,2) * a(3,1)) / det + ainv(3,2) = -(a(1,1) * a(3,2) - a(1,2) * a(3,1)) / det + ainv(3,3) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) / det + + ierr = FEM_SUCCESS + end subroutine inverse3 + + subroutine cross3(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(out) :: c(NDIM) + + c(1) = a(2) * b(3) - a(3) * b(2) + c(2) = a(3) * b(1) - a(1) * b(3) + c(3) = a(1) * b(2) - a(2) * b(1) + end subroutine cross3 + + real(rk) function get_dof_value(vector, dof_num, node) + real(rk), intent(in) :: vector(:) + integer, intent(in) :: dof_num + integer, intent(in) :: node + integer :: dof + + get_dof_value = ZERO + dof = dof_id(dof_num,node) + if (dof .gt. 0) get_dof_value = vector(dof) + end function get_dof_value + +end module fem_element_internal diff --git a/src/element/fem_element_mass.f90 b/src/element/fem_element_mass.f90 new file mode 100644 index 0000000..7309979 --- /dev/null +++ b/src/element/fem_element_mass.f90 @@ -0,0 +1,314 @@ +module fem_element_mass + use fem_precision + use fem_error + use fem_database + implicit none + +contains + + subroutine element_compute_lumped_mass(ierr) + integer, intent(out) :: ierr + integer :: elem + integer :: mat + integer :: sec + integer :: nlocal + real(rk) :: density + real(rk) :: volume + real(rk) :: area + real(rk) :: thickness + real(rk) :: elem_mass + real(rk) :: nodal_mass + real(rk) :: nodal_inertia + character(len=ELEM_TYPE_LEN) :: etype + + if (.not. allocated(mass_diag)) then + call error_set(FEM_ERR_INTERNAL, 'mass vector is not allocated', ierr) + return + end if + + mass_diag = ZERO + + do elem = 1, nelem + if (elem_status(elem) .ne. 0) cycle + + mat = elem_mat(elem) + sec = elem_sec(elem) + if (mat .lt. 1 .or. mat .gt. n_material) then + call error_set(FEM_ERR_MATERIAL, 'element has invalid material index', ierr) + return + end if + if (sec .lt. 1 .or. sec .gt. n_section) then + call error_set(FEM_ERR_INPUT, 'element has invalid section index', ierr) + return + end if + + density = mat_density(mat) + if (density .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'material density must be positive', ierr) + return + end if + + etype = adjustl(elem_type(elem)) + nlocal = elem_nnode(elem) + + select case (trim(etype)) + case ('C3D4') + if (nlocal .ne. 4) then + call error_set(FEM_ERR_MESH, 'C3D4 element must have 4 nodes', ierr) + return + end if + volume = tetra4_volume(elem) + if (volume .le. ZERO) then + call error_set(FEM_ERR_MESH, 'C3D4 element has non-positive volume', ierr) + return + end if + elem_mass = density * volume + nodal_mass = elem_mass / real(nlocal, rk) + call add_translational_mass(elem, nodal_mass) + + case ('C3D8') + if (nlocal .ne. 8) then + call error_set(FEM_ERR_MESH, 'C3D8 element must have 8 nodes', ierr) + return + end if + call hex8_volume(elem, volume, ierr) + if (ierr .ne. FEM_SUCCESS) return + elem_mass = density * volume + nodal_mass = elem_mass / real(nlocal, rk) + call add_translational_mass(elem, nodal_mass) + + case ('S4') + if (nlocal .ne. 4) then + call error_set(FEM_ERR_MESH, 'S4 element must have 4 nodes', ierr) + return + end if + thickness = section_thickness(sec) + if (thickness .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'S4 section thickness must be positive', ierr) + return + end if + area = shell4_area(elem) + if (area .le. ZERO) then + call error_set(FEM_ERR_MESH, 'S4 element has non-positive area', ierr) + return + end if + elem_mass = density * area * thickness + nodal_mass = elem_mass / real(nlocal, rk) + nodal_inertia = density * area * thickness**3 / & + (12.0_rk * real(nlocal, rk)) + call add_translational_mass(elem, nodal_mass) + call add_rotational_mass(elem, nodal_inertia) + + case default + call error_set(FEM_ERR_MESH, 'unsupported element type for lumped mass', ierr) + return + end select + end do + + ierr = FEM_SUCCESS + end subroutine element_compute_lumped_mass + + subroutine add_translational_mass(elem, nodal_mass) + integer, intent(in) :: elem + real(rk), intent(in) :: nodal_mass + integer :: local_node + integer :: node + integer :: dof + integer :: idir + + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node, elem) + do idir = 1, NDIM + dof = dof_id(idir, node) + if (dof .gt. 0) mass_diag(dof) = mass_diag(dof) + nodal_mass + end do + end do + end subroutine add_translational_mass + + subroutine add_rotational_mass(elem, nodal_inertia) + integer, intent(in) :: elem + real(rk), intent(in) :: nodal_inertia + integer :: local_node + integer :: node + integer :: dof + integer :: idir + + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node, elem) + do idir = 4, NDOF_NODE + dof = dof_id(idir, node) + if (dof .gt. 0) mass_diag(dof) = mass_diag(dof) + nodal_inertia + end do + end do + end subroutine add_rotational_mass + + real(rk) function tetra4_volume(elem) + integer, intent(in) :: elem + real(rk) :: a(NDIM) + real(rk) :: b(NDIM) + real(rk) :: c(NDIM) + real(rk) :: d(NDIM) + + a = coord0(:, elem_conn(1, elem)) + b = coord0(:, elem_conn(2, elem)) + c = coord0(:, elem_conn(3, elem)) + d = coord0(:, elem_conn(4, elem)) + tetra4_volume = abs(det3_columns(b - a, c - a, d - a)) / 6.0_rk + end function tetra4_volume + + subroutine hex8_volume(elem, volume, ierr) + integer, intent(in) :: elem + real(rk), intent(out) :: volume + integer, intent(out) :: ierr + real(rk) :: xi(8) + real(rk) :: eta(8) + real(rk) :: zeta(8) + real(rk) :: gp(2) + real(rk) :: dndr(3,8) + real(rk) :: jac(3,3) + real(rk) :: detj + real(rk) :: r + real(rk) :: s + real(rk) :: t + integer :: i + integer :: j + integer :: k + integer :: node + integer :: local_node + + xi(1) = -ONE + xi(2) = ONE + xi(3) = ONE + xi(4) = -ONE + xi(5) = -ONE + xi(6) = ONE + xi(7) = ONE + xi(8) = -ONE + + eta(1) = -ONE + eta(2) = -ONE + eta(3) = ONE + eta(4) = ONE + eta(5) = -ONE + eta(6) = -ONE + eta(7) = ONE + eta(8) = ONE + + zeta(1) = -ONE + zeta(2) = -ONE + zeta(3) = -ONE + zeta(4) = -ONE + zeta(5) = ONE + zeta(6) = ONE + zeta(7) = ONE + zeta(8) = ONE + + gp(1) = -ONE / sqrt(3.0_rk) + gp(2) = ONE / sqrt(3.0_rk) + volume = ZERO + + do i = 1, 2 + r = gp(i) + do j = 1, 2 + s = gp(j) + do k = 1, 2 + t = gp(k) + + do local_node = 1, 8 + dndr(1,local_node) = 0.125_rk * xi(local_node) * & + (ONE + s * eta(local_node)) * (ONE + t * zeta(local_node)) + dndr(2,local_node) = 0.125_rk * eta(local_node) * & + (ONE + r * xi(local_node)) * (ONE + t * zeta(local_node)) + dndr(3,local_node) = 0.125_rk * zeta(local_node) * & + (ONE + r * xi(local_node)) * (ONE + s * eta(local_node)) + end do + + jac = ZERO + do local_node = 1, 8 + node = elem_conn(local_node, elem) + jac(1,1) = jac(1,1) + coord0(1,node) * dndr(1,local_node) + jac(1,2) = jac(1,2) + coord0(1,node) * dndr(2,local_node) + jac(1,3) = jac(1,3) + coord0(1,node) * dndr(3,local_node) + jac(2,1) = jac(2,1) + coord0(2,node) * dndr(1,local_node) + jac(2,2) = jac(2,2) + coord0(2,node) * dndr(2,local_node) + jac(2,3) = jac(2,3) + coord0(2,node) * dndr(3,local_node) + jac(3,1) = jac(3,1) + coord0(3,node) * dndr(1,local_node) + jac(3,2) = jac(3,2) + coord0(3,node) * dndr(2,local_node) + jac(3,3) = jac(3,3) + coord0(3,node) * dndr(3,local_node) + end do + + detj = det3_matrix(jac) + if (detj .le. ZERO) then + call error_set(FEM_ERR_MESH, 'C3D8 element has non-positive Jacobian', ierr) + return + end if + volume = volume + detj + end do + end do + end do + + if (volume .le. ZERO) then + call error_set(FEM_ERR_MESH, 'C3D8 element has non-positive volume', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine hex8_volume + + real(rk) function shell4_area(elem) + integer, intent(in) :: elem + real(rk) :: a(NDIM) + real(rk) :: b(NDIM) + real(rk) :: c(NDIM) + real(rk) :: d(NDIM) + + a = coord0(:, elem_conn(1, elem)) + b = coord0(:, elem_conn(2, elem)) + c = coord0(:, elem_conn(3, elem)) + d = coord0(:, elem_conn(4, elem)) + shell4_area = triangle_area(a, b, c) + triangle_area(a, c, d) + end function shell4_area + + real(rk) function triangle_area(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(in) :: c(NDIM) + real(rk) :: ab(NDIM) + real(rk) :: ac(NDIM) + real(rk) :: cr(NDIM) + + ab = b - a + ac = c - a + call cross3(ab, ac, cr) + triangle_area = HALF * sqrt(sum(cr * cr)) + end function triangle_area + + subroutine cross3(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(out) :: c(NDIM) + + c(1) = a(2) * b(3) - a(3) * b(2) + c(2) = a(3) * b(1) - a(1) * b(3) + c(3) = a(1) * b(2) - a(2) * b(1) + end subroutine cross3 + + real(rk) function det3_columns(a, b, c) + real(rk), intent(in) :: a(NDIM) + real(rk), intent(in) :: b(NDIM) + real(rk), intent(in) :: c(NDIM) + + det3_columns = a(1) * (b(2) * c(3) - b(3) * c(2)) - & + b(1) * (a(2) * c(3) - a(3) * c(2)) + & + c(1) * (a(2) * b(3) - a(3) * b(2)) + end function det3_columns + + real(rk) function det3_matrix(a) + real(rk), intent(in) :: a(3,3) + + det3_matrix = a(1,1) * (a(2,2) * a(3,3) - a(2,3) * a(3,2)) - & + a(1,2) * (a(2,1) * a(3,3) - a(2,3) * a(3,1)) + & + a(1,3) * (a(2,1) * a(3,2) - a(2,2) * a(3,1)) + end function det3_matrix + +end module fem_element_mass diff --git a/src/input/fem_inp_reader.f90 b/src/input/fem_inp_reader.f90 new file mode 100644 index 0000000..1375e94 --- /dev/null +++ b/src/input/fem_inp_reader.f90 @@ -0,0 +1,1731 @@ +module fem_inp_reader + use fem_precision + use fem_error + use fem_log + use fem_control + use fem_database + implicit none + + integer, parameter :: LINE_LEN = 512 + integer, parameter :: MODE_NONE = 0 + integer, parameter :: MODE_NODE = 1 + integer, parameter :: MODE_ELEMENT = 2 + integer, parameter :: MODE_NSET = 3 + integer, parameter :: MODE_ELSET = 4 + integer, parameter :: MODE_DENSITY = 5 + integer, parameter :: MODE_ELASTIC = 6 + integer, parameter :: MODE_SHELL_SECTION = 7 + integer, parameter :: MODE_BOUNDARY = 8 + integer, parameter :: MODE_CLOAD = 9 + integer, parameter :: MODE_DLOAD = 10 + integer, parameter :: MODE_PLASTIC = 11 + integer, parameter :: MODE_DAMAGE_INIT = 12 + integer, parameter :: MODE_DYNAMIC = 13 + integer, parameter :: MODE_AMPLITUDE = 14 + integer, parameter :: MODE_JC_PLASTIC = 15 + integer, parameter :: MODE_RATE_DEPENDENT = 16 + integer, parameter :: MODE_SPECIFIC_HEAT = 17 + integer, parameter :: MODE_INELASTIC_HEAT_FRACTION = 18 + +contains + + subroutine inp_read_file(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + integer :: node_count + integer :: elem_count + integer :: nset_count_total + integer :: elset_count_total + integer :: nset_item_count + integer :: elset_item_count + integer :: material_count + integer :: plastic_item_count + integer :: section_count + integer :: amplitude_count + integer :: amplitude_item_count + integer :: bc_count + integer :: cload_count + integer :: dload_count + + call inp_count_model(filename, node_count, elem_count, nset_count_total, & + elset_count_total, nset_item_count, elset_item_count, & + material_count, plastic_item_count, section_count, & + amplitude_count, amplitude_item_count, bc_count, & + cload_count, dload_count, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_model(node_count, elem_count, nset_count_total, & + elset_count_total, nset_item_count, & + elset_item_count, material_count, & + plastic_item_count, section_count, & + amplitude_count, amplitude_item_count, & + bc_count, cload_count, dload_count, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call inp_fill_model(filename, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call inp_apply_sections(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_assign_dofs(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call log_info('INP model read completed') + end subroutine inp_read_file + + subroutine inp_count_model(filename, node_count, elem_count, nset_count_total, & + elset_count_total, nset_item_count, elset_item_count, & + material_count, plastic_item_count, section_count, & + amplitude_count, amplitude_item_count, bc_count, & + cload_count, dload_count, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: node_count + integer, intent(out) :: elem_count + integer, intent(out) :: nset_count_total + integer, intent(out) :: elset_count_total + integer, intent(out) :: nset_item_count + integer, intent(out) :: elset_item_count + integer, intent(out) :: material_count + integer, intent(out) :: plastic_item_count + integer, intent(out) :: section_count + integer, intent(out) :: amplitude_count + integer, intent(out) :: amplitude_item_count + integer, intent(out) :: bc_count + integer, intent(out) :: cload_count + integer, intent(out) :: dload_count + integer, intent(out) :: ierr + integer :: unit_id + integer :: ios + integer :: mode + logical :: generate_mode + logical :: element_elset_active + character(len=LINE_LEN) :: line + character(len=LINE_LEN) :: clean + character(len=LINE_LEN) :: set_name + + node_count = 0 + elem_count = 0 + nset_count_total = 0 + elset_count_total = 0 + nset_item_count = 0 + elset_item_count = 0 + material_count = 0 + plastic_item_count = 0 + section_count = 0 + amplitude_count = 0 + amplitude_item_count = 0 + bc_count = 0 + cload_count = 0 + dload_count = 0 + mode = MODE_NONE + generate_mode = .false. + element_elset_active = .false. + unit_id = 21 + + open(unit=unit_id, file=filename, status='old', action='read', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to open input file: '//trim(filename), ierr) + return + end if + + do + read(unit_id,'(A)',iostat=ios) line + if (ios .ne. 0) exit + + call clean_line(line, clean) + if (len_trim(clean) .eq. 0) cycle + + if (clean(1:1) .eq. '*') then + call count_keyword(clean, mode, generate_mode, element_elset_active, & + nset_count_total, elset_count_total, & + material_count, section_count, amplitude_count) + else if (mode .eq. MODE_NODE) then + node_count = node_count + 1 + else if (mode .eq. MODE_ELEMENT) then + elem_count = elem_count + 1 + if (element_elset_active) elset_item_count = elset_item_count + 1 + else if (mode .eq. MODE_NSET) then + nset_item_count = nset_item_count + count_set_items(clean, generate_mode) + else if (mode .eq. MODE_ELSET) then + elset_item_count = elset_item_count + count_set_items(clean, generate_mode) + else if (mode .eq. MODE_BOUNDARY) then + bc_count = bc_count + 1 + else if (mode .eq. MODE_CLOAD) then + cload_count = cload_count + 1 + else if (mode .eq. MODE_DLOAD) then + dload_count = dload_count + 1 + else if (mode .eq. MODE_PLASTIC) then + plastic_item_count = plastic_item_count + 1 + else if (mode .eq. MODE_AMPLITUDE) then + amplitude_item_count = amplitude_item_count + count_amplitude_items(clean) + end if + end do + + close(unit_id) + + if (node_count .le. 0) then + call error_set(FEM_ERR_INPUT, 'input file contains no nodes', ierr) + return + end if + + if (elem_count .le. 0) then + call error_set(FEM_ERR_INPUT, 'input file contains no elements', ierr) + return + end if + + set_name = ' ' + ierr = FEM_SUCCESS + end subroutine inp_count_model + + subroutine count_keyword(line, mode, generate_mode, element_elset_active, & + nset_count_total, elset_count_total, & + material_count, section_count, amplitude_count) + character(len=*), intent(in) :: line + integer, intent(out) :: mode + logical, intent(out) :: generate_mode + logical, intent(out) :: element_elset_active + integer, intent(inout) :: nset_count_total + integer, intent(inout) :: elset_count_total + integer, intent(inout) :: material_count + integer, intent(inout) :: section_count + integer, intent(inout) :: amplitude_count + character(len=LINE_LEN) :: upper + character(len=NAME_LEN) :: name_value + + call to_upper(line, upper) + mode = MODE_NONE + generate_mode = .false. + element_elset_active = .false. + + if (starts_with(upper, '*NODE')) then + mode = MODE_NODE + else if (starts_with(upper, '*ELEMENT')) then + mode = MODE_ELEMENT + call get_keyword_value(upper, 'ELSET', name_value) + if (len_trim(name_value) .gt. 0) then + elset_count_total = elset_count_total + 1 + element_elset_active = .true. + end if + else if (starts_with(upper, '*NSET')) then + mode = MODE_NSET + generate_mode = has_parameter(upper, 'GENERATE') + nset_count_total = nset_count_total + 1 + else if (starts_with(upper, '*ELSET')) then + mode = MODE_ELSET + generate_mode = has_parameter(upper, 'GENERATE') + elset_count_total = elset_count_total + 1 + else if (starts_with(upper, '*MATERIAL')) then + material_count = material_count + 1 + else if (starts_with(upper, '*PLASTIC')) then + if (is_johnson_cook_plastic_keyword(upper)) then + mode = MODE_JC_PLASTIC + else + mode = MODE_PLASTIC + end if + else if (starts_with(upper, '*DAMAGE INITIATION')) then + mode = MODE_DAMAGE_INIT + else if (starts_with(upper, '*RATE DEPENDENT')) then + mode = MODE_RATE_DEPENDENT + else if (starts_with(upper, '*SPECIFIC HEAT')) then + mode = MODE_SPECIFIC_HEAT + else if (starts_with(upper, '*INELASTIC HEAT FRACTION')) then + mode = MODE_INELASTIC_HEAT_FRACTION + else if (starts_with(upper, '*SOLID SECTION')) then + section_count = section_count + 1 + else if (starts_with(upper, '*SHELL SECTION')) then + section_count = section_count + 1 + else if (starts_with(upper, '*BOUNDARY')) then + mode = MODE_BOUNDARY + else if (starts_with(upper, '*CLOAD')) then + mode = MODE_CLOAD + else if (starts_with(upper, '*DLOAD')) then + mode = MODE_DLOAD + else if (starts_with(upper, '*DYNAMIC')) then + mode = MODE_DYNAMIC + else if (starts_with(upper, '*AMPLITUDE')) then + mode = MODE_AMPLITUDE + amplitude_count = amplitude_count + 1 + end if + end subroutine count_keyword + + subroutine inp_fill_model(filename, ierr) + character(len=*), intent(in) :: filename + integer, intent(out) :: ierr + integer :: unit_id + integer :: ios + integer :: mode + integer :: inode + integer :: ielem + integer :: current_nset + integer :: current_elset + integer :: current_material + integer :: current_section + integer :: current_amp + integer :: current_load_amp + integer :: current_bc + integer :: current_cload + integer :: current_dload + integer :: plastic_pos + integer :: amp_pos + integer :: nset_pos + integer :: elset_pos + integer :: nvals + integer :: node_id + integer :: elem_id + integer :: conn_ext(MAX_NEN) + integer :: conn_int + integer :: i + real(rk) :: x + real(rk) :: y + real(rk) :: z + character(len=LINE_LEN) :: line + character(len=LINE_LEN) :: clean + character(len=LINE_LEN) :: upper + character(len=ELEM_TYPE_LEN) :: current_elem_type + logical :: generate_mode + logical :: current_element_elset_active + + unit_id = 22 + mode = MODE_NONE + inode = 0 + ielem = 0 + current_nset = 0 + current_elset = 0 + current_material = 0 + current_section = 0 + current_amp = 0 + current_load_amp = 0 + current_bc = 0 + current_cload = 0 + current_dload = 0 + plastic_pos = 0 + amp_pos = 0 + nset_pos = 0 + elset_pos = 0 + current_elem_type = ' ' + generate_mode = .false. + current_element_elset_active = .false. + + open(unit=unit_id, file=filename, status='old', action='read', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to open input file: '//trim(filename), ierr) + return + end if + + do + read(unit_id,'(A)',iostat=ios) line + if (ios .ne. 0) exit + + call clean_line(line, clean) + if (len_trim(clean) .eq. 0) cycle + + if (clean(1:1) .eq. '*') then + call to_upper(clean, upper) + call fill_keyword(upper, mode, generate_mode, current_element_elset_active, & + current_elem_type, current_nset, current_elset, & + current_material, current_section, current_amp, & + current_load_amp, nset_pos, elset_pos, amp_pos, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_NODE) then + inode = inode + 1 + call parse_node_line(clean, node_id, x, y, z, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + + node_id_ext(inode) = node_id + coord0(1,inode) = x + coord0(2,inode) = y + coord0(3,inode) = z + coord(:,inode) = coord0(:,inode) + else if (mode .eq. MODE_ELEMENT) then + ielem = ielem + 1 + call parse_element_line(clean, elem_id, conn_ext, nvals, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + + elem_id_ext(ielem) = elem_id + elem_type(ielem) = current_elem_type + elem_nnode(ielem) = nvals + elem_status(ielem) = 0 + elem_mat(ielem) = 0 + elem_sec(ielem) = 0 + + do i = 1, nvals + conn_int = database_find_node(conn_ext(i)) + if (conn_int .eq. 0) then + close(unit_id) + call error_set(FEM_ERR_INPUT, 'element references unknown node id', ierr) + return + end if + elem_conn(i,ielem) = conn_int + end do + + if (current_element_elset_active) then + call add_elset_item(current_elset, elset_pos, elem_id) + end if + else if (mode .eq. MODE_NSET) then + call add_set_line(clean, generate_mode, current_nset, nset_pos, .true., ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_ELSET) then + call add_set_line(clean, generate_mode, current_elset, elset_pos, .false., ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_DENSITY) then + call parse_density_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_ELASTIC) then + call parse_elastic_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_PLASTIC) then + call parse_plastic_line(clean, current_material, plastic_pos, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_JC_PLASTIC) then + call parse_jc_plastic_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_DAMAGE_INIT) then + call parse_damage_init_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_RATE_DEPENDENT) then + call parse_rate_dependent_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_SPECIFIC_HEAT) then + call parse_specific_heat_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_INELASTIC_HEAT_FRACTION) then + call parse_heat_fraction_line(clean, current_material, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_DYNAMIC) then + call parse_dynamic_line(clean, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_AMPLITUDE) then + call parse_amplitude_line(clean, current_amp, amp_pos, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_SHELL_SECTION) then + call parse_shell_section_line(clean, current_section, ierr) + mode = MODE_NONE + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_BOUNDARY) then + current_bc = current_bc + 1 + call parse_boundary_line(clean, current_bc, current_load_amp, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_CLOAD) then + current_cload = current_cload + 1 + call parse_cload_line(clean, current_cload, current_load_amp, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + else if (mode .eq. MODE_DLOAD) then + current_dload = current_dload + 1 + call parse_dload_line(clean, current_dload, current_load_amp, ierr) + if (ierr .ne. FEM_SUCCESS) then + close(unit_id) + return + end if + end if + end do + + close(unit_id) + ierr = FEM_SUCCESS + end subroutine inp_fill_model + + subroutine fill_keyword(line, mode, generate_mode, element_elset_active, & + current_elem_type, current_nset, current_elset, & + current_material, current_section, current_amp, & + current_load_amp, nset_pos, elset_pos, amp_pos, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: mode + logical, intent(out) :: generate_mode + logical, intent(out) :: element_elset_active + character(len=*), intent(out) :: current_elem_type + integer, intent(inout) :: current_nset + integer, intent(inout) :: current_elset + integer, intent(inout) :: current_material + integer, intent(inout) :: current_section + integer, intent(inout) :: current_amp + integer, intent(out) :: current_load_amp + integer, intent(inout) :: nset_pos + integer, intent(inout) :: elset_pos + integer, intent(in) :: amp_pos + integer, intent(out) :: ierr + character(len=NAME_LEN) :: name_value + + mode = MODE_NONE + generate_mode = .false. + element_elset_active = .false. + current_load_amp = 0 + ierr = FEM_SUCCESS + + if (starts_with(line, '*NODE')) then + mode = MODE_NODE + else if (starts_with(line, '*ELEMENT')) then + mode = MODE_ELEMENT + call get_keyword_value(line, 'TYPE', current_elem_type) + if (len_trim(current_elem_type) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*ELEMENT is missing TYPE parameter', ierr) + return + end if + + call get_keyword_value(line, 'ELSET', name_value) + if (len_trim(name_value) .gt. 0) then + current_elset = current_elset + 1 + call start_elset(current_elset, elset_pos, name_value) + element_elset_active = .true. + end if + else if (starts_with(line, '*NSET')) then + mode = MODE_NSET + generate_mode = has_parameter(line, 'GENERATE') + call get_keyword_value(line, 'NSET', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*NSET is missing NSET parameter', ierr) + return + end if + current_nset = current_nset + 1 + call start_nset(current_nset, nset_pos, name_value) + else if (starts_with(line, '*ELSET')) then + mode = MODE_ELSET + generate_mode = has_parameter(line, 'GENERATE') + call get_keyword_value(line, 'ELSET', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*ELSET is missing ELSET parameter', ierr) + return + end if + current_elset = current_elset + 1 + call start_elset(current_elset, elset_pos, name_value) + else if (starts_with(line, '*MATERIAL')) then + current_material = current_material + 1 + call get_keyword_value(line, 'NAME', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*MATERIAL is missing NAME parameter', ierr) + return + end if + mat_name(current_material) = name_value + else if (starts_with(line, '*DENSITY')) then + mode = MODE_DENSITY + else if (starts_with(line, '*ELASTIC')) then + mode = MODE_ELASTIC + else if (starts_with(line, '*PLASTIC')) then + if (current_material .le. 0) then + call error_set(FEM_ERR_INPUT, '*PLASTIC appears before *MATERIAL', ierr) + return + end if + if (is_johnson_cook_plastic_keyword(line)) then + mat_plastic_model(current_material) = 'J2_JC' + mode = MODE_JC_PLASTIC + else + mat_plastic_model(current_material) = 'J2' + mode = MODE_PLASTIC + end if + else if (starts_with(line, '*DAMAGE INITIATION')) then + if (current_material .le. 0) then + call error_set(FEM_ERR_INPUT, '*DAMAGE INITIATION appears before *MATERIAL', ierr) + return + end if + if (is_johnson_cook_damage_keyword(line)) then + mat_failure_model(current_material) = 'JOHNSON_COOK' + else + mat_failure_model(current_material) = 'EQPS' + end if + mode = MODE_DAMAGE_INIT + else if (starts_with(line, '*RATE DEPENDENT')) then + if (current_material .le. 0) then + call error_set(FEM_ERR_INPUT, '*RATE DEPENDENT appears before *MATERIAL', ierr) + return + end if + if (.not. is_johnson_cook_rate_keyword(line)) then + call error_set(FEM_ERR_INPUT, 'only Johnson-Cook rate dependence is supported', ierr) + return + end if + mat_rate_model(current_material) = 'JOHNSON_COOK' + mode = MODE_RATE_DEPENDENT + else if (starts_with(line, '*SPECIFIC HEAT')) then + if (current_material .le. 0) then + call error_set(FEM_ERR_INPUT, '*SPECIFIC HEAT appears before *MATERIAL', ierr) + return + end if + mode = MODE_SPECIFIC_HEAT + else if (starts_with(line, '*INELASTIC HEAT FRACTION')) then + if (current_material .le. 0) then + call error_set(FEM_ERR_INPUT, '*INELASTIC HEAT FRACTION appears before *MATERIAL', ierr) + return + end if + mode = MODE_INELASTIC_HEAT_FRACTION + else if (starts_with(line, '*SOLID SECTION')) then + current_section = current_section + 1 + call fill_section_header(line, current_section, 'SOLID', ierr) + else if (starts_with(line, '*SHELL SECTION')) then + current_section = current_section + 1 + call fill_section_header(line, current_section, 'SHELL', ierr) + mode = MODE_SHELL_SECTION + else if (starts_with(line, '*BOUNDARY')) then + mode = MODE_BOUNDARY + else if (starts_with(line, '*CLOAD')) then + mode = MODE_CLOAD + else if (starts_with(line, '*DLOAD')) then + mode = MODE_DLOAD + else if (starts_with(line, '*DYNAMIC')) then + mode = MODE_DYNAMIC + else if (starts_with(line, '*AMPLITUDE')) then + mode = MODE_AMPLITUDE + call get_keyword_value(line, 'NAME', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*AMPLITUDE is missing NAME parameter', ierr) + return + end if + current_amp = current_amp + 1 + amp_name(current_amp) = name_value + amp_start(current_amp) = amp_pos + 1 + amp_count(current_amp) = 0 + end if + + if (mode .eq. MODE_BOUNDARY .or. mode .eq. MODE_CLOAD .or. & + mode .eq. MODE_DLOAD) then + call get_keyword_value(line, 'AMPLITUDE', name_value) + if (len_trim(name_value) .gt. 0) then + current_load_amp = database_find_amplitude(name_value) + if (current_load_amp .eq. 0) then + call error_set(FEM_ERR_INPUT, 'load references unknown amplitude', ierr) + return + end if + end if + end if + end subroutine fill_keyword + + subroutine start_nset(set_index, item_pos, set_name_value) + integer, intent(in) :: set_index + integer, intent(in) :: item_pos + character(len=*), intent(in) :: set_name_value + + nset_name(set_index) = set_name_value + nset_start(set_index) = item_pos + 1 + nset_count(set_index) = 0 + end subroutine start_nset + + subroutine start_elset(set_index, item_pos, set_name_value) + integer, intent(in) :: set_index + integer, intent(in) :: item_pos + character(len=*), intent(in) :: set_name_value + + elset_name(set_index) = set_name_value + elset_start(set_index) = item_pos + 1 + elset_count(set_index) = 0 + end subroutine start_elset + + subroutine add_elset_item(set_index, item_pos, item_value) + integer, intent(in) :: set_index + integer, intent(inout) :: item_pos + integer, intent(in) :: item_value + + item_pos = item_pos + 1 + elset_items(item_pos) = item_value + elset_count(set_index) = elset_count(set_index) + 1 + end subroutine add_elset_item + + subroutine add_set_line(line, generate_mode, set_index, item_pos, is_node_set, ierr) + character(len=*), intent(in) :: line + logical, intent(in) :: generate_mode + integer, intent(in) :: set_index + integer, intent(inout) :: item_pos + logical, intent(in) :: is_node_set + integer, intent(out) :: ierr + integer :: start_value + integer :: end_value + integer :: increment + integer :: value + integer :: field + integer :: ios + character(len=LINE_LEN) :: token + + if (generate_mode) then + call parse_generate_line(line, start_value, end_value, increment, ierr) + if (ierr .ne. FEM_SUCCESS) return + value = start_value + do + if ((increment .gt. 0 .and. value .gt. end_value) .or. & + (increment .lt. 0 .and. value .lt. end_value)) exit + call add_set_item(set_index, item_pos, is_node_set, value) + value = value + increment + end do + else + do field = 1, MAX_NEN + call csv_token(line, field, token) + if (len_trim(token) .eq. 0) exit + read(token,*,iostat=ios) value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse set item', ierr) + return + end if + call add_set_item(set_index, item_pos, is_node_set, value) + end do + end if + + ierr = FEM_SUCCESS + end subroutine add_set_line + + subroutine add_set_item(set_index, item_pos, is_node_set, item_value) + integer, intent(in) :: set_index + integer, intent(inout) :: item_pos + logical, intent(in) :: is_node_set + integer, intent(in) :: item_value + + item_pos = item_pos + 1 + if (is_node_set) then + nset_items(item_pos) = item_value + nset_count(set_index) = nset_count(set_index) + 1 + else + elset_items(item_pos) = item_value + elset_count(set_index) = elset_count(set_index) + 1 + end if + end subroutine add_set_item + + subroutine fill_section_header(line, section_index, type_name, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: section_index + character(len=*), intent(in) :: type_name + integer, intent(out) :: ierr + character(len=NAME_LEN) :: name_value + + section_type(section_index) = type_name + + call get_keyword_value(line, 'ELSET', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section is missing ELSET parameter', ierr) + return + end if + section_elset_name(section_index) = name_value + + call get_keyword_value(line, 'MATERIAL', name_value) + if (len_trim(name_value) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section is missing MATERIAL parameter', ierr) + return + end if + section_material_name(section_index) = name_value + + ierr = FEM_SUCCESS + end subroutine fill_section_header + + subroutine parse_density_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*DENSITY appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_density(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse material density', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_density_line + + subroutine parse_elastic_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*ELASTIC appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_e(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse elastic modulus', ierr) + return + end if + + call csv_token(line, 2, token) + read(token,*,iostat=ios) mat_nu(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Poisson ratio', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_elastic_line + + subroutine parse_plastic_line(line, material_index, plastic_pos, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(inout) :: plastic_pos + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + real(rk) :: yield_stress + real(rk) :: plastic_strain + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*PLASTIC data appears before *MATERIAL', ierr) + return + end if + if (.not. allocated(mat_plastic_stress)) then + call error_set(FEM_ERR_INPUT, '*PLASTIC data storage is not allocated', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) yield_stress + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse plastic yield stress', ierr) + return + end if + + call csv_token(line, 2, token) + if (len_trim(token) .eq. 0) then + plastic_strain = ZERO + else + read(token,*,iostat=ios) plastic_strain + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse plastic strain', ierr) + return + end if + end if + + if (yield_stress .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'plastic yield stress must be positive', ierr) + return + end if + if (plastic_strain .lt. ZERO) then + call error_set(FEM_ERR_INPUT, 'plastic strain must be non-negative', ierr) + return + end if + + plastic_pos = plastic_pos + 1 + if (plastic_pos .gt. n_plastic_item) then + call error_set(FEM_ERR_INTERNAL, 'plastic curve item count overflow', ierr) + return + end if + + if (mat_plastic_count(material_index) .eq. 0) then + mat_plastic_start(material_index) = plastic_pos + mat_yield0(material_index) = yield_stress + end if + + mat_plastic_count(material_index) = mat_plastic_count(material_index) + 1 + mat_plastic_stress(plastic_pos) = yield_stress + mat_plastic_strain(plastic_pos) = plastic_strain + call update_material_hardening(material_index) + + ierr = FEM_SUCCESS + end subroutine parse_plastic_line + + subroutine update_material_hardening(material_index) + integer, intent(in) :: material_index + integer :: start_pos + integer :: count + real(rk) :: deps + + start_pos = mat_plastic_start(material_index) + count = mat_plastic_count(material_index) + if (count .le. 1) then + mat_hardening_modulus(material_index) = ZERO + else + deps = mat_plastic_strain(start_pos + 1) - mat_plastic_strain(start_pos) + if (deps .gt. ZERO) then + mat_hardening_modulus(material_index) = & + (mat_plastic_stress(start_pos + 1) - mat_plastic_stress(start_pos)) / deps + else + mat_hardening_modulus(material_index) = ZERO + end if + end if + end subroutine update_material_hardening + + subroutine parse_jc_plastic_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, 'Johnson-Cook plastic data appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_jc_a(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook A', ierr) + return + end if + + call csv_token(line, 2, token) + read(token,*,iostat=ios) mat_jc_b(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook B', ierr) + return + end if + + call csv_token(line, 3, token) + read(token,*,iostat=ios) mat_jc_n(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook n', ierr) + return + end if + + call csv_token(line, 4, token) + if (len_trim(token) .gt. 0) then + read(token,*,iostat=ios) mat_jc_m(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook m', ierr) + return + end if + if (mat_jc_m(material_index) .gt. ZERO) mat_temp_model(material_index) = 'JOHNSON_COOK' + end if + + call csv_token(line, 5, token) + if (len_trim(token) .gt. 0) then + read(token,*,iostat=ios) mat_jc_tmelt(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook melt temperature', ierr) + return + end if + end if + + call csv_token(line, 6, token) + if (len_trim(token) .gt. 0) then + read(token,*,iostat=ios) mat_jc_troom(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook room temperature', ierr) + return + end if + end if + + mat_yield0(material_index) = mat_jc_a(material_index) + ierr = FEM_SUCCESS + end subroutine parse_jc_plastic_line + + subroutine parse_rate_dependent_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*RATE DEPENDENT data appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_jc_c(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook C', ierr) + return + end if + + call csv_token(line, 2, token) + if (len_trim(token) .eq. 0) then + mat_jc_epsdot0(material_index) = ONE + else + read(token,*,iostat=ios) mat_jc_epsdot0(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook reference strain rate', ierr) + return + end if + end if + + ierr = FEM_SUCCESS + end subroutine parse_rate_dependent_line + + subroutine parse_specific_heat_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*SPECIFIC HEAT data appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_jc_cp(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse specific heat', ierr) + return + end if + if (mat_jc_cp(material_index) .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'specific heat must be positive', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_specific_heat_line + + subroutine parse_heat_fraction_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*INELASTIC HEAT FRACTION data appears before *MATERIAL', ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_jc_chi(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse inelastic heat fraction', ierr) + return + end if + if (mat_jc_chi(material_index) .lt. ZERO .or. mat_jc_chi(material_index) .gt. ONE) then + call error_set(FEM_ERR_INPUT, 'inelastic heat fraction must be in [0,1]', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_heat_fraction_line + + subroutine parse_damage_init_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + if (material_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*DAMAGE INITIATION data appears before *MATERIAL', ierr) + return + end if + + if (trim(mat_failure_model(material_index)) .eq. 'JOHNSON_COOK') then + call parse_jc_damage_line(line, material_index, ierr) + return + end if + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_eqps_fail(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse damage initiation threshold', ierr) + return + end if + + if (mat_eqps_fail(material_index) .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'damage initiation threshold must be positive', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_damage_init_line + + subroutine parse_jc_damage_line(line, material_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: material_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + call csv_token(line, 1, token) + read(token,*,iostat=ios) mat_jc_d1(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook damage D1', ierr) + return + end if + + call csv_token(line, 2, token) + read(token,*,iostat=ios) mat_jc_d2(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook damage D2', ierr) + return + end if + + call csv_token(line, 3, token) + read(token,*,iostat=ios) mat_jc_d3(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook damage D3', ierr) + return + end if + + call csv_token(line, 4, token) + read(token,*,iostat=ios) mat_jc_d4(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook damage D4', ierr) + return + end if + + call csv_token(line, 5, token) + read(token,*,iostat=ios) mat_jc_d5(material_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse Johnson-Cook damage D5', ierr) + return + end if + + if (mat_jc_d1(material_index) .le. ZERO .and. & + mat_jc_d2(material_index) .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'Johnson-Cook damage D1 or D2 must be positive', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_jc_damage_line + + subroutine parse_dynamic_line(line, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + real(rk) :: first_value + real(rk) :: second_value + real(rk) :: ratio + integer :: ios + + call csv_token(line, 1, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, '*DYNAMIC data line is empty', ierr) + return + end if + read(token,*,iostat=ios) first_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse *DYNAMIC first value', ierr) + return + end if + if (first_value .le. ZERO) then + call error_set(FEM_ERR_INPUT, '*DYNAMIC first value must be positive', ierr) + return + end if + + call csv_token(line, 2, token) + if (len_trim(token) .eq. 0) then + time_end = first_value + dt_user = ZERO + use_user_dt = .false. + nstep_max = 0 + else + read(token,*,iostat=ios) second_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse *DYNAMIC total time', ierr) + return + end if + if (second_value .le. ZERO) then + call error_set(FEM_ERR_INPUT, '*DYNAMIC total time must be positive', ierr) + return + end if + + dt_user = first_value + time_end = second_value + use_user_dt = .true. + ratio = time_end / dt_user + nstep_max = int(ratio) + if (real(nstep_max, rk) * dt_user .lt. & + time_end * (ONE - 1.0e-10_rk)) nstep_max = nstep_max + 1 + end if + + ierr = FEM_SUCCESS + end subroutine parse_dynamic_line + + subroutine parse_shell_section_line(line, section_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: section_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + call csv_token(line, 1, token) + read(token,*,iostat=ios) section_thickness(section_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse shell thickness', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_shell_section_line + + subroutine parse_boundary_line(line, bc_index, amplitude_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: bc_index + integer, intent(in) :: amplitude_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + character(len=NAME_LEN) :: upper_token + integer :: ios + + call csv_token(line, 1, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'boundary line is missing target', ierr) + return + end if + call to_upper(token, upper_token) + bc_target(bc_index) = upper_token + + call csv_token(line, 2, token) + read(token,*,iostat=ios) bc_dof_start(bc_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse boundary first dof', ierr) + return + end if + + call csv_token(line, 3, token) + if (len_trim(token) .eq. 0) then + bc_dof_end(bc_index) = bc_dof_start(bc_index) + else + read(token,*,iostat=ios) bc_dof_end(bc_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse boundary last dof', ierr) + return + end if + end if + + call csv_token(line, 4, token) + if (len_trim(token) .eq. 0) then + bc_value(bc_index) = ZERO + else + read(token,*,iostat=ios) bc_value(bc_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse boundary value', ierr) + return + end if + end if + + bc_amp(bc_index) = amplitude_index + ierr = FEM_SUCCESS + end subroutine parse_boundary_line + + subroutine parse_cload_line(line, cload_index, amplitude_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: cload_index + integer, intent(in) :: amplitude_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + character(len=NAME_LEN) :: upper_token + integer :: ios + + call csv_token(line, 1, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'cload line is missing target', ierr) + return + end if + call to_upper(token, upper_token) + cload_target(cload_index) = upper_token + + call csv_token(line, 2, token) + read(token,*,iostat=ios) cload_dof(cload_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse cload dof', ierr) + return + end if + + call csv_token(line, 3, token) + read(token,*,iostat=ios) cload_value(cload_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse cload value', ierr) + return + end if + + cload_amp(cload_index) = amplitude_index + ierr = FEM_SUCCESS + end subroutine parse_cload_line + + subroutine parse_dload_line(line, dload_index, amplitude_index, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: dload_index + integer, intent(in) :: amplitude_index + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + character(len=NAME_LEN) :: upper_target + character(len=16) :: upper_label + integer :: ios + + call csv_token(line, 1, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'dload line is missing target', ierr) + return + end if + call to_upper(token, upper_target) + dload_target(dload_index) = upper_target + + call csv_token(line, 2, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'dload line is missing label', ierr) + return + end if + call to_upper(token, upper_label) + dload_label(dload_index) = upper_label + + call csv_token(line, 3, token) + read(token,*,iostat=ios) dload_value(dload_index) + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse dload value', ierr) + return + end if + + dload_amp(dload_index) = amplitude_index + ierr = FEM_SUCCESS + end subroutine parse_dload_line + + subroutine inp_apply_sections(ierr) + integer, intent(out) :: ierr + integer :: sec + integer :: mat + integer :: set_id + integer :: item + integer :: elem_internal + integer :: start_pos + integer :: stop_pos + + do sec = 1, n_section + mat = database_find_material(section_material_name(sec)) + if (mat .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section references unknown material', ierr) + return + end if + + set_id = database_find_elset(section_elset_name(sec)) + if (set_id .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section references unknown elset', ierr) + return + end if + + start_pos = elset_start(set_id) + stop_pos = start_pos + elset_count(set_id) - 1 + do item = start_pos, stop_pos + elem_internal = database_find_elem(elset_items(item)) + if (elem_internal .eq. 0) then + call error_set(FEM_ERR_INPUT, 'elset references unknown element id', ierr) + return + end if + elem_sec(elem_internal) = sec + elem_mat(elem_internal) = mat + end do + end do + + ierr = FEM_SUCCESS + end subroutine inp_apply_sections + + integer function count_set_items(line, generate_mode) + character(len=*), intent(in) :: line + logical, intent(in) :: generate_mode + integer :: start_value + integer :: end_value + integer :: increment + integer :: ierr_local + integer :: field + character(len=LINE_LEN) :: token + + count_set_items = 0 + if (generate_mode) then + call parse_generate_line(line, start_value, end_value, increment, ierr_local) + if (ierr_local .ne. FEM_SUCCESS .or. increment .eq. 0) return + count_set_items = ((end_value - start_value) / increment) + 1 + if (count_set_items .lt. 0) count_set_items = 0 + else + do field = 1, MAX_NEN + call csv_token(line, field, token) + if (len_trim(token) .eq. 0) exit + count_set_items = count_set_items + 1 + end do + end if + end function count_set_items + + integer function count_amplitude_items(line) + character(len=*), intent(in) :: line + integer :: field + character(len=LINE_LEN) :: token + + count_amplitude_items = 0 + field = 1 + do + call csv_token(line, field, token) + if (len_trim(token) .eq. 0) exit + call csv_token(line, field + 1, token) + if (len_trim(token) .eq. 0) exit + count_amplitude_items = count_amplitude_items + 1 + field = field + 2 + end do + end function count_amplitude_items + + subroutine parse_amplitude_line(line, amplitude_index, amp_pos, ierr) + character(len=*), intent(in) :: line + integer, intent(in) :: amplitude_index + integer, intent(inout) :: amp_pos + integer, intent(out) :: ierr + integer :: field + integer :: ios + real(rk) :: time_value + real(rk) :: scale_value + character(len=LINE_LEN) :: token + + if (amplitude_index .le. 0) then + call error_set(FEM_ERR_INPUT, '*AMPLITUDE data appears before *AMPLITUDE', ierr) + return + end if + if (.not. allocated(amp_time) .or. .not. allocated(amp_value)) then + call error_set(FEM_ERR_INPUT, '*AMPLITUDE data storage is not allocated', ierr) + return + end if + + field = 1 + do + call csv_token(line, field, token) + if (len_trim(token) .eq. 0) exit + read(token,*,iostat=ios) time_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse amplitude time', ierr) + return + end if + + call csv_token(line, field + 1, token) + if (len_trim(token) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'amplitude data has unmatched time value', ierr) + return + end if + read(token,*,iostat=ios) scale_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse amplitude value', ierr) + return + end if + + amp_pos = amp_pos + 1 + if (amp_pos .gt. n_amp_item) then + call error_set(FEM_ERR_INTERNAL, 'amplitude data count overflow', ierr) + return + end if + + amp_time(amp_pos) = time_value + amp_value(amp_pos) = scale_value + amp_count(amplitude_index) = amp_count(amplitude_index) + 1 + field = field + 2 + end do + + ierr = FEM_SUCCESS + end subroutine parse_amplitude_line + + subroutine parse_generate_line(line, start_value, end_value, increment, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: start_value + integer, intent(out) :: end_value + integer, intent(out) :: increment + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + call csv_token(line, 1, token) + read(token,*,iostat=ios) start_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse generate start value', ierr) + return + end if + + call csv_token(line, 2, token) + read(token,*,iostat=ios) end_value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse generate end value', ierr) + return + end if + + call csv_token(line, 3, token) + if (len_trim(token) .eq. 0) then + increment = 1 + else + read(token,*,iostat=ios) increment + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse generate increment', ierr) + return + end if + end if + + if (increment .eq. 0) then + call error_set(FEM_ERR_INPUT, 'generate increment must not be zero', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_generate_line + + subroutine parse_node_line(line, node_id, x, y, z, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: node_id + real(rk), intent(out) :: x + real(rk), intent(out) :: y + real(rk), intent(out) :: z + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: ios + + call csv_token(line, 1, token) + read(token,*,iostat=ios) node_id + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse node id', ierr) + return + end if + + call csv_token(line, 2, token) + read(token,*,iostat=ios) x + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse node x coordinate', ierr) + return + end if + + call csv_token(line, 3, token) + read(token,*,iostat=ios) y + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse node y coordinate', ierr) + return + end if + + call csv_token(line, 4, token) + read(token,*,iostat=ios) z + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse node z coordinate', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_node_line + + subroutine parse_element_line(line, elem_id, conn, nconn, ierr) + character(len=*), intent(in) :: line + integer, intent(out) :: elem_id + integer, intent(out) :: conn(MAX_NEN) + integer, intent(out) :: nconn + integer, intent(out) :: ierr + character(len=LINE_LEN) :: token + integer :: field + integer :: ios + integer :: value + + conn = 0 + nconn = 0 + + call csv_token(line, 1, token) + read(token,*,iostat=ios) elem_id + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse element id', ierr) + return + end if + + do field = 2, MAX_NEN + 1 + call csv_token(line, field, token) + if (len_trim(token) .eq. 0) exit + + read(token,*,iostat=ios) value + if (ios .ne. 0) then + call error_set(FEM_ERR_INPUT, 'failed to parse element connectivity', ierr) + return + end if + + nconn = nconn + 1 + conn(nconn) = value + end do + + if (nconn .le. 0) then + call error_set(FEM_ERR_INPUT, 'element line contains no connectivity', ierr) + return + end if + + ierr = FEM_SUCCESS + end subroutine parse_element_line + + subroutine clean_line(line, clean) + character(len=*), intent(in) :: line + character(len=*), intent(out) :: clean + integer :: comment_pos + + clean = ' ' + clean = adjustl(line) + comment_pos = index(clean, '**') + if (comment_pos .eq. 1) then + clean = ' ' + else if (comment_pos .gt. 1) then + clean = clean(1:comment_pos-1) + end if + end subroutine clean_line + + subroutine get_keyword_value(line, key, value) + character(len=*), intent(in) :: line + character(len=*), intent(in) :: key + character(len=*), intent(out) :: value + character(len=LINE_LEN) :: upper_line + character(len=LINE_LEN) :: upper_key + character(len=LINE_LEN) :: upper_value + integer :: start_pos + integer :: end_pos + integer :: comma_pos + + value = ' ' + call to_upper(line, upper_line) + call to_upper(key, upper_key) + + start_pos = index(upper_line, trim(upper_key)//'=') + if (start_pos .eq. 0) return + + start_pos = start_pos + len_trim(upper_key) + 1 + comma_pos = index(upper_line(start_pos:), ',') + if (comma_pos .eq. 0) then + end_pos = len_trim(line) + else + end_pos = start_pos + comma_pos - 2 + end if + + if (end_pos .ge. start_pos) then + value = adjustl(line(start_pos:end_pos)) + call to_upper(value, upper_value) + value = upper_value + end if + end subroutine get_keyword_value + + logical function has_parameter(line, key) + character(len=*), intent(in) :: line + character(len=*), intent(in) :: key + character(len=LINE_LEN) :: upper_line + character(len=LINE_LEN) :: upper_key + + call to_upper(line, upper_line) + call to_upper(key, upper_key) + has_parameter = index(upper_line, trim(upper_key)) .gt. 0 + end function has_parameter + + logical function is_johnson_cook_plastic_keyword(line) + character(len=*), intent(in) :: line + + is_johnson_cook_plastic_keyword = & + has_parameter(line, 'HARDENING=JOHNSON COOK') .or. & + has_parameter(line, 'HARDENING=JOHNSON_COOK') + end function is_johnson_cook_plastic_keyword + + logical function is_johnson_cook_rate_keyword(line) + character(len=*), intent(in) :: line + + is_johnson_cook_rate_keyword = & + has_parameter(line, 'TYPE=JOHNSON COOK') .or. & + has_parameter(line, 'TYPE=JOHNSON_COOK') .or. & + has_parameter(line, 'JOHNSON COOK') .or. & + has_parameter(line, 'JOHNSON_COOK') + end function is_johnson_cook_rate_keyword + + logical function is_johnson_cook_damage_keyword(line) + character(len=*), intent(in) :: line + + is_johnson_cook_damage_keyword = & + has_parameter(line, 'CRITERION=JOHNSON COOK') .or. & + has_parameter(line, 'CRITERION=JOHNSON_COOK') .or. & + has_parameter(line, 'JOHNSON COOK') .or. & + has_parameter(line, 'JOHNSON_COOK') + end function is_johnson_cook_damage_keyword + + subroutine csv_token(line, field_index, token) + character(len=*), intent(in) :: line + integer, intent(in) :: field_index + character(len=*), intent(out) :: token + integer :: i + integer :: field + integer :: start_pos + integer :: end_pos + integer :: line_len + + token = ' ' + field = 1 + start_pos = 1 + line_len = len_trim(line) + + do i = 1, line_len + 1 + if (i .gt. line_len .or. line(i:i) .eq. ',') then + if (field .eq. field_index) then + end_pos = i - 1 + if (end_pos .ge. start_pos) token = adjustl(line(start_pos:end_pos)) + return + end if + field = field + 1 + start_pos = i + 1 + end if + end do + end subroutine csv_token + + subroutine to_upper(input, output) + character(len=*), intent(in) :: input + character(len=*), intent(out) :: output + integer :: i + integer :: code + + output = ' ' + output = input + do i = 1, len(output) + code = ichar(output(i:i)) + if (code .ge. ichar('a') .and. code .le. ichar('z')) then + output(i:i) = char(code - ichar('a') + ichar('A')) + end if + end do + end subroutine to_upper + + logical function starts_with(text, prefix) + character(len=*), intent(in) :: text + character(len=*), intent(in) :: prefix + integer :: n + + n = len_trim(prefix) + starts_with = .false. + if (len_trim(text) .lt. n) return + starts_with = text(1:n) .eq. prefix(1:n) + end function starts_with + +end module fem_inp_reader diff --git a/src/material/fem_material_j2.f90 b/src/material/fem_material_j2.f90 new file mode 100644 index 0000000..298180d --- /dev/null +++ b/src/material/fem_material_j2.f90 @@ -0,0 +1,550 @@ +module fem_material_j2 + use fem_precision + use fem_error + use fem_database + implicit none + +contains + + subroutine material_update_small_strain(mat, deps, state_old, stress_old, & + stress_new, state_new, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: deps(NSTRESS) + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(in) :: stress_old(NSTRESS) + real(rk), intent(out) :: stress_new(NSTRESS) + real(rk), intent(out) :: state_new(NSTATEV_MAX) + integer, intent(out) :: ierr + + call material_update_small_strain_dt(mat, ZERO, deps, state_old, & + stress_old, stress_new, state_new, ierr) + end subroutine material_update_small_strain + + subroutine material_update_small_strain_dt(mat, dt, deps, state_old, & + stress_old, stress_new, & + state_new, ierr, spin_inc) + integer, intent(in) :: mat + real(rk), intent(in) :: dt + real(rk), intent(in) :: deps(NSTRESS) + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(in) :: stress_old(NSTRESS) + real(rk), intent(out) :: stress_new(NSTRESS) + real(rk), intent(out) :: state_new(NSTATEV_MAX) + integer, intent(out) :: ierr + real(rk), intent(in), optional :: spin_inc(3) + real(rk) :: stress_obj(NSTRESS) + + if (mat .lt. 1 .or. mat .gt. n_material) then + call error_set(FEM_ERR_MATERIAL, 'invalid material index for stress update', ierr) + return + end if + + stress_obj = stress_old + if (present(spin_inc)) call apply_jaumann_increment(stress_old, spin_inc, stress_obj) + + if (trim(mat_plastic_model(mat)) .eq. 'J2' .or. & + trim(mat_plastic_model(mat)) .eq. 'J2_JC') then + call material_update_j2(mat, dt, deps, state_old, stress_obj, & + stress_new, state_new, ierr) + else + call material_update_elastic(mat, deps, state_old, stress_obj, & + stress_new, state_new, ierr) + end if + if (ierr .ne. FEM_SUCCESS) return + + call material_apply_failure(mat, stress_new, state_new, ierr) + end subroutine material_update_small_strain_dt + + subroutine apply_jaumann_increment(stress_old, spin_inc, stress_obj) + real(rk), intent(in) :: stress_old(NSTRESS) + real(rk), intent(in) :: spin_inc(3) + real(rk), intent(out) :: stress_obj(NSTRESS) + real(rk) :: sig(3,3) + real(rk) :: spin(3,3) + real(rk) :: corr(3,3) + + sig = ZERO + sig(1,1) = stress_old(1) + sig(2,2) = stress_old(2) + sig(3,3) = stress_old(3) + sig(1,2) = stress_old(4) + sig(2,1) = stress_old(4) + sig(1,3) = stress_old(5) + sig(3,1) = stress_old(5) + sig(2,3) = stress_old(6) + sig(3,2) = stress_old(6) + + spin = ZERO + spin(1,2) = spin_inc(1) + spin(2,1) = -spin_inc(1) + spin(1,3) = spin_inc(2) + spin(3,1) = -spin_inc(2) + spin(2,3) = spin_inc(3) + spin(3,2) = -spin_inc(3) + + corr = matmul(spin, sig) - matmul(sig, spin) + + stress_obj(1) = stress_old(1) + corr(1,1) + stress_obj(2) = stress_old(2) + corr(2,2) + stress_obj(3) = stress_old(3) + corr(3,3) + stress_obj(4) = stress_old(4) + corr(1,2) + stress_obj(5) = stress_old(5) + corr(1,3) + stress_obj(6) = stress_old(6) + corr(2,3) + end subroutine apply_jaumann_increment + + subroutine material_update_elastic(mat, deps, state_old, stress_old, & + stress_new, state_new, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: deps(NSTRESS) + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(in) :: stress_old(NSTRESS) + real(rk), intent(out) :: stress_new(NSTRESS) + real(rk), intent(out) :: state_new(NSTATEV_MAX) + integer, intent(out) :: ierr + real(rk) :: dsig(NSTRESS) + + call elastic_stress_increment(mat, deps, dsig, ierr) + if (ierr .ne. FEM_SUCCESS) return + + stress_new = stress_old + dsig + state_new = state_old + state_new(20) = ZERO + state_new(22) = ZERO + ierr = FEM_SUCCESS + end subroutine material_update_elastic + + subroutine material_update_j2(mat, dt, deps, state_old, stress_old, & + stress_new, state_new, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: dt + real(rk), intent(in) :: deps(NSTRESS) + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(in) :: stress_old(NSTRESS) + real(rk), intent(out) :: stress_new(NSTRESS) + real(rk), intent(out) :: state_new(NSTATEV_MAX) + integer, intent(out) :: ierr + real(rk) :: dsig(NSTRESS) + real(rk) :: trial(NSTRESS) + real(rk) :: dev_trial(NSTRESS) + real(rk) :: seq_trial + real(rk) :: yield_stress + real(rk) :: hardening + real(rk) :: mu + real(rk) :: dgamma + real(rk) :: residual + real(rk) :: scale + real(rk) :: flow(NSTRESS) + real(rk) :: tol + integer :: iter + + call elastic_stress_increment(mat, deps, dsig, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call shear_modulus(mat, mu, ierr) + if (ierr .ne. FEM_SUCCESS) return + + trial = stress_old + dsig + call deviatoric_stress(trial, dev_trial) + seq_trial = mises_stress(dev_trial) + + call yield_curve_for_step(mat, state_old(1), ZERO, dt, state_old, & + yield_stress, hardening, ierr) + if (ierr .ne. FEM_SUCCESS) return + + tol = 1.0e-10_rk * max(ONE, yield_stress) + if (seq_trial - yield_stress .le. tol) then + stress_new = trial + state_new = state_old + if (trim(mat_plastic_model(mat)) .eq. 'J2_JC') then + if (state_new(3) .le. ZERO) state_new(3) = material_reference_temperature(mat) + state_new(20) = ZERO + state_new(21) = yield_stress + state_new(22) = ZERO + end if + ierr = FEM_SUCCESS + return + end if + + if (seq_trial .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'J2 trial stress has zero deviatoric norm', ierr) + return + end if + + dgamma = (seq_trial - yield_stress) / (3.0_rk * mu + hardening) + if (dgamma .lt. ZERO) dgamma = ZERO + do iter = 1, 25 + call yield_curve_for_step(mat, state_old(1) + dgamma, dgamma, dt, & + state_old, yield_stress, hardening, ierr) + if (ierr .ne. FEM_SUCCESS) return + residual = seq_trial - 3.0_rk * mu * dgamma - yield_stress + if (abs(residual) .le. tol) exit + dgamma = dgamma + residual / (3.0_rk * mu + hardening) + if (dgamma .lt. ZERO) dgamma = ZERO + end do + + scale = ONE - 3.0_rk * mu * dgamma / seq_trial + stress_new = trial + stress_new(1) = hydrostatic_stress(trial) + scale * dev_trial(1) + stress_new(2) = hydrostatic_stress(trial) + scale * dev_trial(2) + stress_new(3) = hydrostatic_stress(trial) + scale * dev_trial(3) + stress_new(4) = scale * dev_trial(4) + stress_new(5) = scale * dev_trial(5) + stress_new(6) = scale * dev_trial(6) + + flow(1) = 1.5_rk * dev_trial(1) / seq_trial + flow(2) = 1.5_rk * dev_trial(2) / seq_trial + flow(3) = 1.5_rk * dev_trial(3) / seq_trial + flow(4) = 3.0_rk * dev_trial(4) / seq_trial + flow(5) = 3.0_rk * dev_trial(5) / seq_trial + flow(6) = 3.0_rk * dev_trial(6) / seq_trial + + state_new = state_old + state_new(1) = state_old(1) + dgamma + state_new(5:10) = state_old(5:10) + dgamma * flow + if (state_new(3) .le. ZERO) state_new(3) = material_reference_temperature(mat) + if (dt .gt. ZERO) then + state_new(20) = dgamma / dt + else + state_new(20) = ZERO + end if + state_new(21) = yield_stress + state_new(22) = dgamma + call apply_adiabatic_temperature(mat, yield_stress, dgamma, state_new) + + ierr = FEM_SUCCESS + end subroutine material_update_j2 + + subroutine elastic_stress_increment(mat, deps, dsig, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: deps(NSTRESS) + real(rk), intent(out) :: dsig(NSTRESS) + integer, intent(out) :: ierr + real(rk) :: lambda + real(rk) :: mu + real(rk) :: trace_deps + real(rk) :: young + real(rk) :: poisson + + young = mat_e(mat) + poisson = mat_nu(mat) + if (young .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'elastic modulus must be positive', ierr) + return + end if + if (poisson .le. -ONE .or. poisson .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'Poisson ratio must be in (-1,0.5)', ierr) + return + end if + + lambda = young * poisson / ((ONE + poisson) * (ONE - 2.0_rk * poisson)) + mu = young / (2.0_rk * (ONE + poisson)) + trace_deps = deps(1) + deps(2) + deps(3) + + dsig(1) = lambda * trace_deps + 2.0_rk * mu * deps(1) + dsig(2) = lambda * trace_deps + 2.0_rk * mu * deps(2) + dsig(3) = lambda * trace_deps + 2.0_rk * mu * deps(3) + dsig(4) = mu * deps(4) + dsig(5) = mu * deps(5) + dsig(6) = mu * deps(6) + + ierr = FEM_SUCCESS + end subroutine elastic_stress_increment + + subroutine material_apply_failure(mat, stress_new, state_new, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: stress_new(NSTRESS) + real(rk), intent(inout) :: state_new(NSTATEV_MAX) + integer, intent(out) :: ierr + real(rk) :: eps_failure + + if (trim(mat_failure_model(mat)) .eq. 'EQPS') then + if (state_new(1) .ge. mat_eqps_fail(mat)) then + state_new(2) = ONE + state_new(4) = ONE + end if + else if (trim(mat_failure_model(mat)) .eq. 'JOHNSON_COOK') then + if (state_new(22) .gt. ZERO) then + eps_failure = jc_failure_strain(mat, stress_new, state_new) + state_new(2) = state_new(2) + state_new(22) / eps_failure + if (state_new(2) .ge. ONE) then + state_new(2) = ONE + state_new(4) = ONE + end if + end if + end if + + ierr = FEM_SUCCESS + end subroutine material_apply_failure + + real(rk) function jc_failure_strain(mat, stress_new, state_new) + integer, intent(in) :: mat + real(rk), intent(in) :: stress_new(NSTRESS) + real(rk), intent(in) :: state_new(NSTATEV_MAX) + real(rk) :: dev(NSTRESS) + real(rk) :: seq + real(rk) :: triax + real(rk) :: triax_factor + real(rk) :: rate_factor + real(rk) :: temp_factor + real(rk) :: rate_ratio + real(rk) :: tstar + + call deviatoric_stress(stress_new, dev) + seq = mises_stress(dev) + if (seq .gt. ZERO) then + triax = hydrostatic_stress(stress_new) / seq + else + triax = ZERO + end if + + triax_factor = mat_jc_d1(mat) + mat_jc_d2(mat) * & + exp(mat_jc_d3(mat) * triax) + rate_factor = ONE + if (trim(mat_rate_model(mat)) .eq. 'JOHNSON_COOK' .and. & + abs(mat_jc_d4(mat)) .gt. ZERO) then + if (mat_jc_epsdot0(mat) .gt. ZERO .and. state_new(20) .gt. ZERO) then + rate_ratio = max(1.0e-12_rk, state_new(20) / mat_jc_epsdot0(mat)) + rate_factor = ONE + mat_jc_d4(mat) * log(rate_ratio) + end if + end if + + temp_factor = ONE + if (abs(mat_jc_d5(mat)) .gt. ZERO) then + tstar = jc_damage_temperature_star(mat, state_new(3)) + temp_factor = ONE + mat_jc_d5(mat) * tstar + end if + + jc_failure_strain = triax_factor * rate_factor * temp_factor + jc_failure_strain = max(1.0e-12_rk, jc_failure_strain) + end function jc_failure_strain + + real(rk) function jc_damage_temperature_star(mat, temp) + integer, intent(in) :: mat + real(rk), intent(in) :: temp + real(rk) :: tref + real(rk) :: tmelt + + jc_damage_temperature_star = ZERO + if (trim(mat_temp_model(mat)) .ne. 'JOHNSON_COOK') return + tref = mat_jc_troom(mat) + tmelt = mat_jc_tmelt(mat) + if (tmelt .le. tref) return + jc_damage_temperature_star = (temp - tref) / (tmelt - tref) + jc_damage_temperature_star = max(ZERO, min(ONE, jc_damage_temperature_star)) + end function jc_damage_temperature_star + + subroutine yield_curve_for_step(mat, eqps, dgamma, dt, state_old, & + yield_stress, hardening, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: eqps + real(rk), intent(in) :: dgamma + real(rk), intent(in) :: dt + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(out) :: yield_stress + real(rk), intent(out) :: hardening + integer, intent(out) :: ierr + + if (trim(mat_plastic_model(mat)) .eq. 'J2_JC') then + call yield_curve_johnson_cook(mat, eqps, dgamma, dt, state_old, & + yield_stress, hardening, ierr) + else + call yield_curve(mat, eqps, yield_stress, hardening, ierr) + end if + end subroutine yield_curve_for_step + + subroutine yield_curve_johnson_cook(mat, eqps, dgamma, dt, state_old, & + yield_stress, hardening, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: eqps + real(rk), intent(in) :: dgamma + real(rk), intent(in) :: dt + real(rk), intent(in) :: state_old(NSTATEV_MAX) + real(rk), intent(out) :: yield_stress + real(rk), intent(out) :: hardening + integer, intent(out) :: ierr + real(rk) :: base + real(rk) :: dbase + real(rk) :: rate_factor + real(rk) :: drate + real(rk) :: temp_factor + real(rk) :: epdot + real(rk) :: temp + + if (mat_jc_a(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook A must be positive', ierr) + return + end if + + base = mat_jc_a(mat) + dbase = ZERO + if (mat_jc_b(mat) .gt. ZERO) then + if (eqps .gt. ZERO) then + base = base + mat_jc_b(mat) * eqps ** mat_jc_n(mat) + if (mat_jc_n(mat) .gt. ZERO) then + dbase = mat_jc_b(mat) * mat_jc_n(mat) * & + eqps ** max(ZERO, mat_jc_n(mat) - ONE) + end if + else if (mat_jc_n(mat) .le. 1.0e-14_rk) then + base = base + mat_jc_b(mat) + end if + end if + + rate_factor = ONE + drate = ZERO + if (trim(mat_rate_model(mat)) .eq. 'JOHNSON_COOK' .and. & + mat_jc_c(mat) .gt. ZERO .and. dt .gt. ZERO) then + epdot = dgamma / dt + if (epdot .gt. mat_jc_epsdot0(mat)) then + rate_factor = ONE + mat_jc_c(mat) * log(epdot / mat_jc_epsdot0(mat)) + if (dgamma .gt. ZERO) drate = mat_jc_c(mat) / dgamma + end if + end if + + temp = state_old(3) + if (temp .le. ZERO) temp = mat_jc_troom(mat) + temp_factor = johnson_cook_temperature_factor(mat, temp) + + yield_stress = base * rate_factor * temp_factor + hardening = (dbase * rate_factor + base * drate) * temp_factor + ierr = FEM_SUCCESS + end subroutine yield_curve_johnson_cook + + real(rk) function johnson_cook_temperature_factor(mat, temp) + integer, intent(in) :: mat + real(rk), intent(in) :: temp + real(rk) :: tstar + + johnson_cook_temperature_factor = ONE + if (trim(mat_temp_model(mat)) .ne. 'JOHNSON_COOK') return + if (mat_jc_m(mat) .le. ZERO) return + if (mat_jc_tmelt(mat) .le. mat_jc_troom(mat)) return + + tstar = (temp - mat_jc_troom(mat)) / (mat_jc_tmelt(mat) - mat_jc_troom(mat)) + tstar = max(ZERO, min(ONE, tstar)) + johnson_cook_temperature_factor = max(ZERO, ONE - tstar ** mat_jc_m(mat)) + end function johnson_cook_temperature_factor + + real(rk) function material_reference_temperature(mat) + integer, intent(in) :: mat + + material_reference_temperature = mat_jc_troom(mat) + if (material_reference_temperature .le. ZERO) material_reference_temperature = 293.15_rk + end function material_reference_temperature + + subroutine apply_adiabatic_temperature(mat, yield_stress, dgamma, state_new) + integer, intent(in) :: mat + real(rk), intent(in) :: yield_stress + real(rk), intent(in) :: dgamma + real(rk), intent(inout) :: state_new(NSTATEV_MAX) + real(rk) :: denom + + if (mat_jc_chi(mat) .le. ZERO) return + if (mat_jc_cp(mat) .le. ZERO) return + if (mat_density(mat) .le. ZERO) return + + denom = mat_density(mat) * mat_jc_cp(mat) + state_new(3) = state_new(3) + mat_jc_chi(mat) * yield_stress * dgamma / denom + end subroutine apply_adiabatic_temperature + + subroutine yield_curve(mat, eqps, yield_stress, hardening, ierr) + integer, intent(in) :: mat + real(rk), intent(in) :: eqps + real(rk), intent(out) :: yield_stress + real(rk), intent(out) :: hardening + integer, intent(out) :: ierr + integer :: start_pos + integer :: count + integer :: item + integer :: left + integer :: right + + start_pos = mat_plastic_start(mat) + count = mat_plastic_count(mat) + if (count .le. 0) then + call error_set(FEM_ERR_MATERIAL, 'J2 material has no plastic curve', ierr) + return + end if + + if (count .eq. 1) then + yield_stress = mat_plastic_stress(start_pos) + hardening = ZERO + ierr = FEM_SUCCESS + return + end if + + left = start_pos + right = start_pos + 1 + do item = start_pos, start_pos + count - 2 + if (eqps .le. mat_plastic_strain(item + 1)) then + left = item + right = item + 1 + exit + end if + left = item + right = item + 1 + end do + + if (eqps .ge. mat_plastic_strain(start_pos + count - 1)) then + left = start_pos + count - 2 + right = start_pos + count - 1 + end if + + if (mat_plastic_strain(right) .le. mat_plastic_strain(left)) then + call error_set(FEM_ERR_MATERIAL, 'plastic strain curve must be increasing', ierr) + return + end if + + hardening = (mat_plastic_stress(right) - mat_plastic_stress(left)) / & + (mat_plastic_strain(right) - mat_plastic_strain(left)) + yield_stress = mat_plastic_stress(left) + hardening * & + (eqps - mat_plastic_strain(left)) + ierr = FEM_SUCCESS + end subroutine yield_curve + + subroutine shear_modulus(mat, mu, ierr) + integer, intent(in) :: mat + real(rk), intent(out) :: mu + integer, intent(out) :: ierr + + if (mat_e(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'elastic modulus must be positive', ierr) + return + end if + if (mat_nu(mat) .le. -ONE .or. mat_nu(mat) .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'Poisson ratio must be in (-1,0.5)', ierr) + return + end if + + mu = mat_e(mat) / (2.0_rk * (ONE + mat_nu(mat))) + ierr = FEM_SUCCESS + end subroutine shear_modulus + + subroutine deviatoric_stress(sig, dev) + real(rk), intent(in) :: sig(NSTRESS) + real(rk), intent(out) :: dev(NSTRESS) + real(rk) :: pressure + + pressure = hydrostatic_stress(sig) + dev = sig + dev(1) = sig(1) - pressure + dev(2) = sig(2) - pressure + dev(3) = sig(3) - pressure + end subroutine deviatoric_stress + + real(rk) function hydrostatic_stress(sig) + real(rk), intent(in) :: sig(NSTRESS) + + hydrostatic_stress = (sig(1) + sig(2) + sig(3)) / 3.0_rk + end function hydrostatic_stress + + real(rk) function mises_stress(dev) + real(rk), intent(in) :: dev(NSTRESS) + real(rk) :: norm_dev + + norm_dev = dev(1) * dev(1) + dev(2) * dev(2) + dev(3) * dev(3) + & + 2.0_rk * (dev(4) * dev(4) + dev(5) * dev(5) + & + dev(6) * dev(6)) + mises_stress = sqrt(1.5_rk * norm_dev) + end function mises_stress + +end module fem_material_j2 diff --git a/src/mesh/fem_database.f90 b/src/mesh/fem_database.f90 new file mode 100644 index 0000000..f8c0919 --- /dev/null +++ b/src/mesh/fem_database.f90 @@ -0,0 +1,1205 @@ +module fem_database + use fem_precision + use fem_error + implicit none + + integer :: nnode = 0 + integer :: nelem = 0 + integer :: ndof = 0 + integer :: ngauss_total = 0 + integer :: n_nset = 0 + integer :: n_elset = 0 + integer :: n_material = 0 + integer :: n_section = 0 + integer :: n_plastic_item = 0 + integer :: n_amp = 0 + integer :: n_amp_item = 0 + integer :: nbc = 0 + integer :: ncload = 0 + integer :: ndload = 0 + + real(rk), allocatable :: coord0(:,:) + real(rk), allocatable :: coord(:,:) + integer, allocatable :: node_id_ext(:) + integer, allocatable :: elem_conn(:,:) + integer, allocatable :: elem_id_ext(:) + integer, allocatable :: elem_nnode(:) + character(len=ELEM_TYPE_LEN), allocatable :: elem_type(:) + integer, allocatable :: elem_mat(:) + integer, allocatable :: elem_sec(:) + integer, allocatable :: elem_status(:) + integer, allocatable :: dof_id(:,:) + + real(rk), allocatable :: u(:) + real(rk), allocatable :: v(:) + real(rk), allocatable :: a(:) + real(rk), allocatable :: vhalf(:) + real(rk), allocatable :: mass_diag(:) + real(rk), allocatable :: fint(:) + real(rk), allocatable :: fext(:) + real(rk), allocatable :: fsph(:) + real(rk), allocatable :: fcontact(:) + + integer, allocatable :: elem_gp_start(:) + integer, allocatable :: elem_ngp(:) + real(rk), allocatable :: stress(:,:) + real(rk), allocatable :: strain(:,:) + real(rk), allocatable :: statev(:,:) + + character(len=NAME_LEN), allocatable :: nset_name(:) + integer, allocatable :: nset_start(:) + integer, allocatable :: nset_count(:) + integer, allocatable :: nset_items(:) + + character(len=NAME_LEN), allocatable :: elset_name(:) + integer, allocatable :: elset_start(:) + integer, allocatable :: elset_count(:) + integer, allocatable :: elset_items(:) + + character(len=NAME_LEN), allocatable :: mat_name(:) + character(len=MAT_MODEL_LEN), allocatable :: mat_plastic_model(:) + character(len=MAT_MODEL_LEN), allocatable :: mat_rate_model(:) + character(len=MAT_MODEL_LEN), allocatable :: mat_temp_model(:) + character(len=MAT_MODEL_LEN), allocatable :: mat_failure_model(:) + real(rk), allocatable :: mat_density(:) + real(rk), allocatable :: mat_e(:) + real(rk), allocatable :: mat_nu(:) + real(rk), allocatable :: mat_yield0(:) + real(rk), allocatable :: mat_hardening_modulus(:) + real(rk), allocatable :: mat_eqps_fail(:) + real(rk), allocatable :: mat_jc_a(:) + real(rk), allocatable :: mat_jc_b(:) + real(rk), allocatable :: mat_jc_n(:) + real(rk), allocatable :: mat_jc_c(:) + real(rk), allocatable :: mat_jc_m(:) + real(rk), allocatable :: mat_jc_epsdot0(:) + real(rk), allocatable :: mat_jc_troom(:) + real(rk), allocatable :: mat_jc_tmelt(:) + real(rk), allocatable :: mat_jc_chi(:) + real(rk), allocatable :: mat_jc_cp(:) + real(rk), allocatable :: mat_jc_d1(:) + real(rk), allocatable :: mat_jc_d2(:) + real(rk), allocatable :: mat_jc_d3(:) + real(rk), allocatable :: mat_jc_d4(:) + real(rk), allocatable :: mat_jc_d5(:) + integer, allocatable :: mat_plastic_start(:) + integer, allocatable :: mat_plastic_count(:) + real(rk), allocatable :: mat_plastic_stress(:) + real(rk), allocatable :: mat_plastic_strain(:) + + character(len=NAME_LEN), allocatable :: amp_name(:) + integer, allocatable :: amp_start(:) + integer, allocatable :: amp_count(:) + real(rk), allocatable :: amp_time(:) + real(rk), allocatable :: amp_value(:) + + character(len=16), allocatable :: section_type(:) + character(len=NAME_LEN), allocatable :: section_elset_name(:) + character(len=NAME_LEN), allocatable :: section_material_name(:) + real(rk), allocatable :: section_thickness(:) + + character(len=NAME_LEN), allocatable :: bc_target(:) + integer, allocatable :: bc_dof_start(:) + integer, allocatable :: bc_dof_end(:) + real(rk), allocatable :: bc_value(:) + integer, allocatable :: bc_amp(:) + + character(len=NAME_LEN), allocatable :: cload_target(:) + integer, allocatable :: cload_dof(:) + real(rk), allocatable :: cload_value(:) + integer, allocatable :: cload_amp(:) + + character(len=NAME_LEN), allocatable :: dload_target(:) + character(len=16), allocatable :: dload_label(:) + real(rk), allocatable :: dload_value(:) + integer, allocatable :: dload_amp(:) + +contains + + subroutine database_allocate_mesh(num_nodes, num_elems, ierr) + integer, intent(in) :: num_nodes + integer, intent(in) :: num_elems + integer, intent(out) :: ierr + + call database_allocate_model(num_nodes, num_elems, 0, 0, 0, 0, 0, 0, 0, & + 0, 0, 0, 0, 0, ierr) + end subroutine database_allocate_mesh + + subroutine database_allocate_model(num_nodes, num_elems, num_nsets, num_elsets, & + num_nset_items, num_elset_items, & + num_materials, num_plastic_items, & + num_sections, num_amp_in, num_amp_items, & + num_bc, num_cload, num_dload, ierr) + integer, intent(in) :: num_nodes + integer, intent(in) :: num_elems + integer, intent(in) :: num_nsets + integer, intent(in) :: num_elsets + integer, intent(in) :: num_nset_items + integer, intent(in) :: num_elset_items + integer, intent(in) :: num_materials + integer, intent(in) :: num_plastic_items + integer, intent(in) :: num_sections + integer, intent(in) :: num_amp_in + integer, intent(in) :: num_amp_items + integer, intent(in) :: num_bc + integer, intent(in) :: num_cload + integer, intent(in) :: num_dload + integer, intent(out) :: ierr + integer :: stat + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) return + + nnode = num_nodes + nelem = num_elems + ndof = 0 + ngauss_total = 0 + n_nset = num_nsets + n_elset = num_elsets + n_material = num_materials + n_section = num_sections + n_plastic_item = num_plastic_items + n_amp = num_amp_in + n_amp_item = num_amp_items + nbc = num_bc + ncload = num_cload + ndload = num_dload + + allocate(coord0(NDIM,nnode), coord(NDIM,nnode), node_id_ext(nnode), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate node arrays', ierr) + return + end if + + allocate(elem_conn(MAX_NEN,nelem), elem_id_ext(nelem), elem_nnode(nelem), & + elem_type(nelem), elem_mat(nelem), elem_sec(nelem), & + elem_status(nelem), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element arrays', ierr) + return + end if + + call database_initialize(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_sets(num_nsets, num_elsets, num_nset_items, & + num_elset_items, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_materials(num_materials, num_plastic_items, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_sections(num_sections, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_amplitudes(num_amp_in, num_amp_items, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call database_allocate_loads(num_bc, num_cload, num_dload, ierr) + end subroutine database_allocate_model + + subroutine database_allocate_sets(num_nsets, num_elsets, num_nset_items, & + num_elset_items, ierr) + integer, intent(in) :: num_nsets + integer, intent(in) :: num_elsets + integer, intent(in) :: num_nset_items + integer, intent(in) :: num_elset_items + integer, intent(out) :: ierr + integer :: stat + + if (num_nsets .gt. 0) then + allocate(nset_name(num_nsets), nset_start(num_nsets), & + nset_count(num_nsets), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate node set headers', ierr) + return + end if + nset_name = ' ' + nset_start = 0 + nset_count = 0 + end if + + if (num_nset_items .gt. 0) then + allocate(nset_items(num_nset_items), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate node set items', ierr) + return + end if + nset_items = 0 + end if + + if (num_elsets .gt. 0) then + allocate(elset_name(num_elsets), elset_start(num_elsets), & + elset_count(num_elsets), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element set headers', ierr) + return + end if + elset_name = ' ' + elset_start = 0 + elset_count = 0 + end if + + if (num_elset_items .gt. 0) then + allocate(elset_items(num_elset_items), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element set items', ierr) + return + end if + elset_items = 0 + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_sets + + subroutine database_allocate_materials(num_materials, num_plastic_items, ierr) + integer, intent(in) :: num_materials + integer, intent(in) :: num_plastic_items + integer, intent(out) :: ierr + integer :: stat + + if (num_materials .gt. 0) then + allocate(mat_name(num_materials), mat_plastic_model(num_materials), & + mat_rate_model(num_materials), mat_temp_model(num_materials), & + mat_failure_model(num_materials), & + mat_density(num_materials), mat_e(num_materials), & + mat_nu(num_materials), mat_yield0(num_materials), & + mat_hardening_modulus(num_materials), mat_eqps_fail(num_materials), & + mat_jc_a(num_materials), mat_jc_b(num_materials), & + mat_jc_n(num_materials), mat_jc_c(num_materials), & + mat_jc_m(num_materials), mat_jc_epsdot0(num_materials), & + mat_jc_troom(num_materials), mat_jc_tmelt(num_materials), & + mat_jc_chi(num_materials), mat_jc_cp(num_materials), & + mat_jc_d1(num_materials), mat_jc_d2(num_materials), & + mat_jc_d3(num_materials), mat_jc_d4(num_materials), & + mat_jc_d5(num_materials), & + mat_plastic_start(num_materials), mat_plastic_count(num_materials), & + stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate material arrays', ierr) + return + end if + mat_name = ' ' + mat_plastic_model = 'ELASTIC' + mat_rate_model = 'NONE' + mat_temp_model = 'NONE' + mat_failure_model = 'NONE' + mat_density = ZERO + mat_e = ZERO + mat_nu = ZERO + mat_yield0 = ZERO + mat_hardening_modulus = ZERO + mat_eqps_fail = ZERO + mat_jc_a = ZERO + mat_jc_b = ZERO + mat_jc_n = ZERO + mat_jc_c = ZERO + mat_jc_m = ZERO + mat_jc_epsdot0 = ONE + mat_jc_troom = 293.15_rk + mat_jc_tmelt = ZERO + mat_jc_chi = ZERO + mat_jc_cp = ZERO + mat_jc_d1 = ZERO + mat_jc_d2 = ZERO + mat_jc_d3 = ZERO + mat_jc_d4 = ZERO + mat_jc_d5 = ZERO + mat_plastic_start = 0 + mat_plastic_count = 0 + end if + + if (num_plastic_items .gt. 0) then + allocate(mat_plastic_stress(num_plastic_items), & + mat_plastic_strain(num_plastic_items), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate plastic curve arrays', ierr) + return + end if + mat_plastic_stress = ZERO + mat_plastic_strain = ZERO + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_materials + + subroutine database_allocate_sections(num_sections, ierr) + integer, intent(in) :: num_sections + integer, intent(out) :: ierr + integer :: stat + + if (num_sections .gt. 0) then + allocate(section_type(num_sections), section_elset_name(num_sections), & + section_material_name(num_sections), section_thickness(num_sections), & + stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate section arrays', ierr) + return + end if + section_type = ' ' + section_elset_name = ' ' + section_material_name = ' ' + section_thickness = ZERO + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_sections + + subroutine database_allocate_amplitudes(num_amp_in, num_amp_items, ierr) + integer, intent(in) :: num_amp_in + integer, intent(in) :: num_amp_items + integer, intent(out) :: ierr + integer :: stat + + if (num_amp_in .gt. 0) then + allocate(amp_name(num_amp_in), amp_start(num_amp_in), & + amp_count(num_amp_in), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate amplitude headers', ierr) + return + end if + amp_name = ' ' + amp_start = 0 + amp_count = 0 + end if + + if (num_amp_items .gt. 0) then + allocate(amp_time(num_amp_items), amp_value(num_amp_items), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate amplitude data', ierr) + return + end if + amp_time = ZERO + amp_value = ZERO + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_amplitudes + + subroutine database_allocate_loads(num_bc, num_cload, num_dload, ierr) + integer, intent(in) :: num_bc + integer, intent(in) :: num_cload + integer, intent(in) :: num_dload + integer, intent(out) :: ierr + integer :: stat + + if (num_bc .gt. 0) then + allocate(bc_target(num_bc), bc_dof_start(num_bc), bc_dof_end(num_bc), & + bc_value(num_bc), bc_amp(num_bc), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate boundary arrays', ierr) + return + end if + bc_target = ' ' + bc_dof_start = 0 + bc_dof_end = 0 + bc_value = ZERO + bc_amp = 0 + end if + + if (num_cload .gt. 0) then + allocate(cload_target(num_cload), cload_dof(num_cload), & + cload_value(num_cload), cload_amp(num_cload), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate cload arrays', ierr) + return + end if + cload_target = ' ' + cload_dof = 0 + cload_value = ZERO + cload_amp = 0 + end if + + if (num_dload .gt. 0) then + allocate(dload_target(num_dload), dload_label(num_dload), & + dload_value(num_dload), dload_amp(num_dload), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate dload arrays', ierr) + return + end if + dload_target = ' ' + dload_label = ' ' + dload_value = ZERO + dload_amp = 0 + end if + + ierr = FEM_SUCCESS + end subroutine database_allocate_loads + + subroutine database_allocate_basic(num_nodes, num_elems, num_dof, ierr) + integer, intent(in) :: num_nodes + integer, intent(in) :: num_elems + integer, intent(in) :: num_dof + integer, intent(out) :: ierr + integer :: stat + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) return + + nnode = num_nodes + nelem = num_elems + ndof = num_dof + ngauss_total = 0 + + allocate(coord0(NDIM,nnode), coord(NDIM,nnode), node_id_ext(nnode), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate node arrays', ierr) + return + end if + + allocate(elem_conn(MAX_NEN,nelem), elem_id_ext(nelem), elem_nnode(nelem), & + elem_type(nelem), elem_mat(nelem), elem_sec(nelem), & + elem_status(nelem), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate element arrays', ierr) + return + end if + + call database_initialize(ierr) + if (ierr .ne. FEM_SUCCESS) return + call database_allocate_vectors(num_dof, ierr) + end subroutine database_allocate_basic + + subroutine database_allocate_vectors(num_dof, ierr) + integer, intent(in) :: num_dof + integer, intent(out) :: ierr + integer :: stat + + ndof = num_dof + + if (allocated(u)) deallocate(u) + if (allocated(v)) deallocate(v) + if (allocated(a)) deallocate(a) + if (allocated(vhalf)) deallocate(vhalf) + if (allocated(mass_diag)) deallocate(mass_diag) + if (allocated(fint)) deallocate(fint) + if (allocated(fext)) deallocate(fext) + if (allocated(fsph)) deallocate(fsph) + if (allocated(fcontact)) deallocate(fcontact) + + if (.not. allocated(dof_id)) then + allocate(dof_id(NDOF_NODE,nnode), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate dof table', ierr) + return + end if + dof_id = 0 + end if + + allocate(u(ndof), v(ndof), a(ndof), vhalf(ndof), mass_diag(ndof), & + fint(ndof), fext(ndof), fsph(ndof), fcontact(ndof), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate global vectors', ierr) + return + end if + + u = ZERO + v = ZERO + a = ZERO + vhalf = ZERO + mass_diag = ZERO + fint = ZERO + fext = ZERO + fsph = ZERO + fcontact = ZERO + ierr = FEM_SUCCESS + end subroutine database_allocate_vectors + + subroutine database_initialize(ierr) + integer, intent(out) :: ierr + + if (allocated(coord0)) coord0 = ZERO + if (allocated(coord)) coord = ZERO + if (allocated(node_id_ext)) node_id_ext = 0 + if (allocated(elem_conn)) elem_conn = 0 + if (allocated(elem_id_ext)) elem_id_ext = 0 + if (allocated(elem_nnode)) elem_nnode = 0 + if (allocated(elem_type)) elem_type = ' ' + if (allocated(elem_mat)) elem_mat = 0 + if (allocated(elem_sec)) elem_sec = 0 + if (allocated(elem_status)) elem_status = 0 + if (allocated(dof_id)) dof_id = 0 + if (allocated(u)) u = ZERO + if (allocated(v)) v = ZERO + if (allocated(a)) a = ZERO + if (allocated(vhalf)) vhalf = ZERO + if (allocated(mass_diag)) mass_diag = ZERO + if (allocated(fint)) fint = ZERO + if (allocated(fext)) fext = ZERO + if (allocated(fsph)) fsph = ZERO + if (allocated(fcontact)) fcontact = ZERO + if (allocated(nset_name)) nset_name = ' ' + if (allocated(nset_start)) nset_start = 0 + if (allocated(nset_count)) nset_count = 0 + if (allocated(nset_items)) nset_items = 0 + if (allocated(elset_name)) elset_name = ' ' + if (allocated(elset_start)) elset_start = 0 + if (allocated(elset_count)) elset_count = 0 + if (allocated(elset_items)) elset_items = 0 + if (allocated(mat_name)) mat_name = ' ' + if (allocated(mat_plastic_model)) mat_plastic_model = 'ELASTIC' + if (allocated(mat_rate_model)) mat_rate_model = 'NONE' + if (allocated(mat_temp_model)) mat_temp_model = 'NONE' + if (allocated(mat_failure_model)) mat_failure_model = 'NONE' + if (allocated(mat_density)) mat_density = ZERO + if (allocated(mat_e)) mat_e = ZERO + if (allocated(mat_nu)) mat_nu = ZERO + if (allocated(mat_yield0)) mat_yield0 = ZERO + if (allocated(mat_hardening_modulus)) mat_hardening_modulus = ZERO + if (allocated(mat_eqps_fail)) mat_eqps_fail = ZERO + if (allocated(mat_jc_a)) mat_jc_a = ZERO + if (allocated(mat_jc_b)) mat_jc_b = ZERO + if (allocated(mat_jc_n)) mat_jc_n = ZERO + if (allocated(mat_jc_c)) mat_jc_c = ZERO + if (allocated(mat_jc_m)) mat_jc_m = ZERO + if (allocated(mat_jc_epsdot0)) mat_jc_epsdot0 = ONE + if (allocated(mat_jc_troom)) mat_jc_troom = 293.15_rk + if (allocated(mat_jc_tmelt)) mat_jc_tmelt = ZERO + if (allocated(mat_jc_chi)) mat_jc_chi = ZERO + if (allocated(mat_jc_cp)) mat_jc_cp = ZERO + if (allocated(mat_jc_d1)) mat_jc_d1 = ZERO + if (allocated(mat_jc_d2)) mat_jc_d2 = ZERO + if (allocated(mat_jc_d3)) mat_jc_d3 = ZERO + if (allocated(mat_jc_d4)) mat_jc_d4 = ZERO + if (allocated(mat_jc_d5)) mat_jc_d5 = ZERO + if (allocated(mat_plastic_start)) mat_plastic_start = 0 + if (allocated(mat_plastic_count)) mat_plastic_count = 0 + if (allocated(mat_plastic_stress)) mat_plastic_stress = ZERO + if (allocated(mat_plastic_strain)) mat_plastic_strain = ZERO + if (allocated(amp_name)) amp_name = ' ' + if (allocated(amp_start)) amp_start = 0 + if (allocated(amp_count)) amp_count = 0 + if (allocated(amp_time)) amp_time = ZERO + if (allocated(amp_value)) amp_value = ZERO + if (allocated(section_type)) section_type = ' ' + if (allocated(section_elset_name)) section_elset_name = ' ' + if (allocated(section_material_name)) section_material_name = ' ' + if (allocated(section_thickness)) section_thickness = ZERO + if (allocated(bc_target)) bc_target = ' ' + if (allocated(bc_dof_start)) bc_dof_start = 0 + if (allocated(bc_dof_end)) bc_dof_end = 0 + if (allocated(bc_value)) bc_value = ZERO + if (allocated(bc_amp)) bc_amp = 0 + if (allocated(cload_target)) cload_target = ' ' + if (allocated(cload_dof)) cload_dof = 0 + if (allocated(cload_value)) cload_value = ZERO + if (allocated(cload_amp)) cload_amp = 0 + if (allocated(dload_target)) dload_target = ' ' + if (allocated(dload_label)) dload_label = ' ' + if (allocated(dload_value)) dload_value = ZERO + if (allocated(dload_amp)) dload_amp = 0 + + ierr = FEM_SUCCESS + end subroutine database_initialize + + subroutine database_assign_dofs(ierr) + integer, intent(out) :: ierr + logical, allocatable :: has_translation(:) + logical, allocatable :: has_rotation(:) + integer :: elem + integer :: local_node + integer :: node + integer :: counter + integer :: stat + + if (nnode .lt. 0 .or. nelem .lt. 0) then + call error_set(FEM_ERR_MESH, 'invalid model size before dof assignment', ierr) + return + end if + + allocate(has_translation(nnode), has_rotation(nnode), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_INTERNAL, 'failed to allocate dof marker arrays', ierr) + return + end if + + has_translation = .false. + has_rotation = .false. + + do elem = 1, nelem + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node, elem) + if (node .ge. 1 .and. node .le. nnode) then + has_translation(node) = .true. + if (is_shell_element(elem_type(elem))) has_rotation(node) = .true. + end if + end do + end do + + counter = 0 + if (allocated(dof_id)) deallocate(dof_id) + allocate(dof_id(NDOF_NODE,nnode), stat=stat) + if (stat .ne. 0) then + deallocate(has_translation, has_rotation) + call error_set(FEM_ERR_INTERNAL, 'failed to allocate dof table', ierr) + return + end if + dof_id = 0 + + do node = 1, nnode + if (has_translation(node)) then + do local_node = 1, 3 + counter = counter + 1 + dof_id(local_node,node) = counter + end do + end if + if (has_rotation(node)) then + do local_node = 4, 6 + counter = counter + 1 + dof_id(local_node,node) = counter + end do + end if + end do + + deallocate(has_translation, has_rotation) + call database_allocate_vectors(counter, ierr) + end subroutine database_assign_dofs + + integer function database_find_node(node_id) + integer, intent(in) :: node_id + integer :: i + + database_find_node = 0 + if (.not. allocated(node_id_ext)) return + + do i = 1, nnode + if (node_id_ext(i) .eq. node_id) then + database_find_node = i + return + end if + end do + end function database_find_node + + integer function database_find_elem(elem_id) + integer, intent(in) :: elem_id + integer :: i + + database_find_elem = 0 + if (.not. allocated(elem_id_ext)) return + + do i = 1, nelem + if (elem_id_ext(i) .eq. elem_id) then + database_find_elem = i + return + end if + end do + end function database_find_elem + + integer function database_find_material(name) + character(len=*), intent(in) :: name + integer :: i + + database_find_material = 0 + if (.not. allocated(mat_name)) return + + do i = 1, n_material + if (trim(mat_name(i)) .eq. trim(name)) then + database_find_material = i + return + end if + end do + end function database_find_material + + integer function database_find_amplitude(name) + character(len=*), intent(in) :: name + integer :: i + + database_find_amplitude = 0 + if (.not. allocated(amp_name)) return + + do i = 1, n_amp + if (trim(amp_name(i)) .eq. trim(name)) then + database_find_amplitude = i + return + end if + end do + end function database_find_amplitude + + real(rk) function database_amplitude_value(amp, time) + integer, intent(in) :: amp + real(rk), intent(in) :: time + integer :: item + integer :: start_pos + integer :: count + real(rk) :: t0 + real(rk) :: t1 + real(rk) :: a0 + real(rk) :: a1 + + database_amplitude_value = ONE + if (amp .le. 0) return + if (amp .gt. n_amp) return + if (.not. allocated(amp_time) .or. .not. allocated(amp_value)) return + + start_pos = amp_start(amp) + count = amp_count(amp) + if (start_pos .le. 0 .or. count .le. 0) return + + if (time .le. amp_time(start_pos)) then + database_amplitude_value = amp_value(start_pos) + return + end if + + do item = start_pos, start_pos + count - 2 + t0 = amp_time(item) + t1 = amp_time(item + 1) + if (time .le. t1) then + a0 = amp_value(item) + a1 = amp_value(item + 1) + if (t1 .le. t0) then + database_amplitude_value = a1 + else + database_amplitude_value = a0 + (a1 - a0) * (time - t0) / (t1 - t0) + end if + return + end if + end do + + database_amplitude_value = amp_value(start_pos + count - 1) + end function database_amplitude_value + + integer function database_find_elset(name) + character(len=*), intent(in) :: name + integer :: i + + database_find_elset = 0 + if (.not. allocated(elset_name)) return + + do i = 1, n_elset + if (trim(elset_name(i)) .eq. trim(name)) then + database_find_elset = i + return + end if + end do + end function database_find_elset + + integer function database_find_nset(name) + character(len=*), intent(in) :: name + integer :: i + + database_find_nset = 0 + if (.not. allocated(nset_name)) return + + do i = 1, n_nset + if (trim(nset_name(i)) .eq. trim(name)) then + database_find_nset = i + return + end if + end do + end function database_find_nset + + subroutine database_validate_model(ierr) + integer, intent(out) :: ierr + + call validate_materials(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_sections(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_element_coverage(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_amplitudes(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_boundary_data(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_cload_data(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call validate_dload_data(ierr) + end subroutine database_validate_model + + subroutine validate_materials(ierr) + integer, intent(out) :: ierr + integer :: mat + + if (n_material .le. 0) then + call error_set(FEM_ERR_MATERIAL, 'model has no material definitions', ierr) + return + end if + + do mat = 1, n_material + if (len_trim(mat_name(mat)) .eq. 0) then + call error_set(FEM_ERR_MATERIAL, 'material is missing name', ierr) + return + end if + if (mat_density(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'material density must be positive', ierr) + return + end if + if (mat_e(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'elastic modulus must be positive', ierr) + return + end if + if (mat_nu(mat) .le. -ONE .or. mat_nu(mat) .ge. HALF) then + call error_set(FEM_ERR_MATERIAL, 'Poisson ratio must be in (-1,0.5)', ierr) + return + end if + if (trim(mat_plastic_model(mat)) .eq. 'J2') then + if (mat_plastic_count(mat) .le. 0) then + call error_set(FEM_ERR_MATERIAL, 'plastic material has no plastic curve', ierr) + return + end if + if (mat_yield0(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'initial yield stress must be positive', ierr) + return + end if + else if (trim(mat_plastic_model(mat)) .eq. 'J2_JC') then + if (mat_jc_a(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook A must be positive', ierr) + return + end if + if (mat_jc_b(mat) .lt. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook B must be non-negative', ierr) + return + end if + if (mat_jc_n(mat) .lt. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook n must be non-negative', ierr) + return + end if + if (mat_jc_c(mat) .lt. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook C must be non-negative', ierr) + return + end if + if (mat_jc_epsdot0(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook reference strain rate must be positive', ierr) + return + end if + if (mat_jc_m(mat) .lt. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook m must be non-negative', ierr) + return + end if + if (trim(mat_temp_model(mat)) .eq. 'JOHNSON_COOK') then + if (mat_jc_tmelt(mat) .le. mat_jc_troom(mat)) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook melt temperature must exceed room temperature', ierr) + return + end if + end if + if (mat_jc_chi(mat) .lt. ZERO .or. mat_jc_chi(mat) .gt. ONE) then + call error_set(FEM_ERR_MATERIAL, 'inelastic heat fraction must be in [0,1]', ierr) + return + end if + if (mat_jc_chi(mat) .gt. ZERO .and. mat_jc_cp(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'specific heat must be positive when heat fraction is active', ierr) + return + end if + else if (trim(mat_plastic_model(mat)) .ne. 'ELASTIC') then + call error_set(FEM_ERR_MATERIAL, 'unsupported plastic material model', ierr) + return + end if + if (trim(mat_failure_model(mat)) .eq. 'EQPS') then + if (mat_eqps_fail(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'EQPS failure threshold must be positive', ierr) + return + end if + else if (trim(mat_failure_model(mat)) .eq. 'JOHNSON_COOK') then + if (trim(mat_plastic_model(mat)) .ne. 'J2_JC') then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook damage requires Johnson-Cook plasticity', ierr) + return + end if + if (mat_jc_d1(mat) .le. ZERO .and. mat_jc_d2(mat) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'Johnson-Cook damage D1 or D2 must be positive', ierr) + return + end if + else if (trim(mat_failure_model(mat)) .ne. 'NONE') then + call error_set(FEM_ERR_MATERIAL, 'unsupported failure material model', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_materials + + subroutine validate_amplitudes(ierr) + integer, intent(out) :: ierr + integer :: amp + integer :: item + integer :: start_pos + + do amp = 1, n_amp + if (len_trim(amp_name(amp)) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'amplitude is missing name', ierr) + return + end if + if (amp_count(amp) .le. 0) then + call error_set(FEM_ERR_INPUT, 'amplitude has no data', ierr) + return + end if + + start_pos = amp_start(amp) + do item = start_pos + 1, start_pos + amp_count(amp) - 1 + if (amp_time(item) .lt. amp_time(item - 1)) then + call error_set(FEM_ERR_INPUT, 'amplitude time values must be nondecreasing', ierr) + return + end if + end do + end do + + ierr = FEM_SUCCESS + end subroutine validate_amplitudes + + subroutine validate_sections(ierr) + integer, intent(out) :: ierr + integer :: sec + + if (n_section .le. 0) then + call error_set(FEM_ERR_INPUT, 'model has no section definitions', ierr) + return + end if + + do sec = 1, n_section + if (database_find_material(section_material_name(sec)) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section references unknown material', ierr) + return + end if + if (database_find_elset(section_elset_name(sec)) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'section references unknown element set', ierr) + return + end if + if (trim(section_type(sec)) .eq. 'SHELL') then + if (section_thickness(sec) .le. ZERO) then + call error_set(FEM_ERR_INPUT, 'shell section thickness must be positive', ierr) + return + end if + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_sections + + subroutine validate_element_coverage(ierr) + integer, intent(out) :: ierr + integer :: elem + + do elem = 1, nelem + if (elem_mat(elem) .le. 0) then + call error_set(FEM_ERR_INPUT, 'element has no material assignment', ierr) + return + end if + if (elem_sec(elem) .le. 0) then + call error_set(FEM_ERR_INPUT, 'element has no section assignment', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_element_coverage + + subroutine validate_boundary_data(ierr) + integer, intent(out) :: ierr + integer :: item + + do item = 1, nbc + if (.not. node_or_nset_exists(bc_target(item))) then + call error_set(FEM_ERR_INPUT, 'boundary target is not a node or node set', ierr) + return + end if + if (bc_dof_start(item) .lt. 1 .or. bc_dof_start(item) .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'boundary first dof is outside 1..6', ierr) + return + end if + if (bc_dof_end(item) .lt. bc_dof_start(item) .or. & + bc_dof_end(item) .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'boundary last dof is invalid', ierr) + return + end if + if (bc_amp(item) .lt. 0 .or. bc_amp(item) .gt. n_amp) then + call error_set(FEM_ERR_INPUT, 'boundary references invalid amplitude', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_boundary_data + + subroutine validate_cload_data(ierr) + integer, intent(out) :: ierr + integer :: item + + do item = 1, ncload + if (.not. node_or_nset_exists(cload_target(item))) then + call error_set(FEM_ERR_INPUT, 'cload target is not a node or node set', ierr) + return + end if + if (cload_dof(item) .lt. 1 .or. cload_dof(item) .gt. NDOF_NODE) then + call error_set(FEM_ERR_INPUT, 'cload dof is outside 1..6', ierr) + return + end if + if (cload_amp(item) .lt. 0 .or. cload_amp(item) .gt. n_amp) then + call error_set(FEM_ERR_INPUT, 'cload references invalid amplitude', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_cload_data + + subroutine validate_dload_data(ierr) + integer, intent(out) :: ierr + integer :: item + + do item = 1, ndload + if (.not. elem_or_elset_exists(dload_target(item))) then + call error_set(FEM_ERR_INPUT, 'dload target is not an element or element set', ierr) + return + end if + if (len_trim(dload_label(item)) .eq. 0) then + call error_set(FEM_ERR_INPUT, 'dload label is empty', ierr) + return + end if + if (dload_amp(item) .lt. 0 .or. dload_amp(item) .gt. n_amp) then + call error_set(FEM_ERR_INPUT, 'dload references invalid amplitude', ierr) + return + end if + end do + + ierr = FEM_SUCCESS + end subroutine validate_dload_data + + logical function node_or_nset_exists(target) + character(len=*), intent(in) :: target + integer :: id + integer :: ios + + read(target,*,iostat=ios) id + if (ios .eq. 0) then + node_or_nset_exists = database_find_node(id) .ne. 0 + else + node_or_nset_exists = database_find_nset(target) .ne. 0 + end if + end function node_or_nset_exists + + logical function elem_or_elset_exists(target) + character(len=*), intent(in) :: target + integer :: id + integer :: ios + + read(target,*,iostat=ios) id + if (ios .eq. 0) then + elem_or_elset_exists = database_find_elem(id) .ne. 0 + else + elem_or_elset_exists = database_find_elset(target) .ne. 0 + end if + end function elem_or_elset_exists + + logical function is_shell_element(type_name) + character(len=*), intent(in) :: type_name + character(len=ELEM_TYPE_LEN) :: local_type + + local_type = adjustl(type_name) + is_shell_element = .false. + if (len_trim(local_type) .ge. 1) then + is_shell_element = local_type(1:1) .eq. 'S' + end if + end function is_shell_element + + subroutine database_finalize(ierr) + integer, intent(out) :: ierr + + if (allocated(coord0)) deallocate(coord0) + if (allocated(coord)) deallocate(coord) + if (allocated(node_id_ext)) deallocate(node_id_ext) + if (allocated(elem_conn)) deallocate(elem_conn) + if (allocated(elem_id_ext)) deallocate(elem_id_ext) + if (allocated(elem_nnode)) deallocate(elem_nnode) + if (allocated(elem_type)) deallocate(elem_type) + if (allocated(elem_mat)) deallocate(elem_mat) + if (allocated(elem_sec)) deallocate(elem_sec) + if (allocated(elem_status)) deallocate(elem_status) + if (allocated(dof_id)) deallocate(dof_id) + if (allocated(u)) deallocate(u) + if (allocated(v)) deallocate(v) + if (allocated(a)) deallocate(a) + if (allocated(vhalf)) deallocate(vhalf) + if (allocated(mass_diag)) deallocate(mass_diag) + if (allocated(fint)) deallocate(fint) + if (allocated(fext)) deallocate(fext) + if (allocated(fsph)) deallocate(fsph) + if (allocated(fcontact)) deallocate(fcontact) + if (allocated(elem_gp_start)) deallocate(elem_gp_start) + if (allocated(elem_ngp)) deallocate(elem_ngp) + if (allocated(stress)) deallocate(stress) + if (allocated(strain)) deallocate(strain) + if (allocated(statev)) deallocate(statev) + if (allocated(nset_name)) deallocate(nset_name) + if (allocated(nset_start)) deallocate(nset_start) + if (allocated(nset_count)) deallocate(nset_count) + if (allocated(nset_items)) deallocate(nset_items) + if (allocated(elset_name)) deallocate(elset_name) + if (allocated(elset_start)) deallocate(elset_start) + if (allocated(elset_count)) deallocate(elset_count) + if (allocated(elset_items)) deallocate(elset_items) + if (allocated(mat_name)) deallocate(mat_name) + if (allocated(mat_plastic_model)) deallocate(mat_plastic_model) + if (allocated(mat_rate_model)) deallocate(mat_rate_model) + if (allocated(mat_temp_model)) deallocate(mat_temp_model) + if (allocated(mat_failure_model)) deallocate(mat_failure_model) + if (allocated(mat_density)) deallocate(mat_density) + if (allocated(mat_e)) deallocate(mat_e) + if (allocated(mat_nu)) deallocate(mat_nu) + if (allocated(mat_yield0)) deallocate(mat_yield0) + if (allocated(mat_hardening_modulus)) deallocate(mat_hardening_modulus) + if (allocated(mat_eqps_fail)) deallocate(mat_eqps_fail) + if (allocated(mat_jc_a)) deallocate(mat_jc_a) + if (allocated(mat_jc_b)) deallocate(mat_jc_b) + if (allocated(mat_jc_n)) deallocate(mat_jc_n) + if (allocated(mat_jc_c)) deallocate(mat_jc_c) + if (allocated(mat_jc_m)) deallocate(mat_jc_m) + if (allocated(mat_jc_epsdot0)) deallocate(mat_jc_epsdot0) + if (allocated(mat_jc_troom)) deallocate(mat_jc_troom) + if (allocated(mat_jc_tmelt)) deallocate(mat_jc_tmelt) + if (allocated(mat_jc_chi)) deallocate(mat_jc_chi) + if (allocated(mat_jc_cp)) deallocate(mat_jc_cp) + if (allocated(mat_jc_d1)) deallocate(mat_jc_d1) + if (allocated(mat_jc_d2)) deallocate(mat_jc_d2) + if (allocated(mat_jc_d3)) deallocate(mat_jc_d3) + if (allocated(mat_jc_d4)) deallocate(mat_jc_d4) + if (allocated(mat_jc_d5)) deallocate(mat_jc_d5) + if (allocated(mat_plastic_start)) deallocate(mat_plastic_start) + if (allocated(mat_plastic_count)) deallocate(mat_plastic_count) + if (allocated(mat_plastic_stress)) deallocate(mat_plastic_stress) + if (allocated(mat_plastic_strain)) deallocate(mat_plastic_strain) + if (allocated(amp_name)) deallocate(amp_name) + if (allocated(amp_start)) deallocate(amp_start) + if (allocated(amp_count)) deallocate(amp_count) + if (allocated(amp_time)) deallocate(amp_time) + if (allocated(amp_value)) deallocate(amp_value) + if (allocated(section_type)) deallocate(section_type) + if (allocated(section_elset_name)) deallocate(section_elset_name) + if (allocated(section_material_name)) deallocate(section_material_name) + if (allocated(section_thickness)) deallocate(section_thickness) + if (allocated(bc_target)) deallocate(bc_target) + if (allocated(bc_dof_start)) deallocate(bc_dof_start) + if (allocated(bc_dof_end)) deallocate(bc_dof_end) + if (allocated(bc_value)) deallocate(bc_value) + if (allocated(bc_amp)) deallocate(bc_amp) + if (allocated(cload_target)) deallocate(cload_target) + if (allocated(cload_dof)) deallocate(cload_dof) + if (allocated(cload_value)) deallocate(cload_value) + if (allocated(cload_amp)) deallocate(cload_amp) + if (allocated(dload_target)) deallocate(dload_target) + if (allocated(dload_label)) deallocate(dload_label) + if (allocated(dload_value)) deallocate(dload_value) + if (allocated(dload_amp)) deallocate(dload_amp) + + nnode = 0 + nelem = 0 + ndof = 0 + ngauss_total = 0 + n_nset = 0 + n_elset = 0 + n_material = 0 + n_section = 0 + n_plastic_item = 0 + n_amp = 0 + n_amp_item = 0 + nbc = 0 + ncload = 0 + ndload = 0 + ierr = FEM_SUCCESS + end subroutine database_finalize + +end module fem_database diff --git a/src/output/fem_output_csv.f90 b/src/output/fem_output_csv.f90 new file mode 100644 index 0000000..705aa7b --- /dev/null +++ b/src/output/fem_output_csv.f90 @@ -0,0 +1,344 @@ +module fem_output_csv + use fem_precision + use fem_error + use fem_control + use fem_database + implicit none + + character(len=256) :: csv_output_directory = '.' + logical :: history_header_written = .false. + +contains + + subroutine output_csv_set_directory(path, ierr) + character(len=*), intent(in) :: path + integer, intent(out) :: ierr + + if (len_trim(path) .eq. 0) then + call error_set(FEM_ERR_OUTPUT, 'output directory path is empty', ierr) + return + end if + + csv_output_directory = adjustl(path) + history_header_written = .false. + ierr = FEM_SUCCESS + end subroutine output_csv_set_directory + + subroutine output_csv_set_interval(interval, ierr) + integer, intent(in) :: interval + integer, intent(out) :: ierr + + if (interval .lt. 0) then + call error_set(FEM_ERR_OUTPUT, 'output interval must be non-negative', ierr) + return + end if + + output_interval = interval + ierr = FEM_SUCCESS + end subroutine output_csv_set_interval + + subroutine output_csv_write(step, time, ierr) + integer, intent(in) :: step + real(rk), intent(in) :: time + integer, intent(out) :: ierr + character(len=512) :: filename + logical :: exists + integer :: unit_id + integer :: ios + real(rk) :: total_mass + real(rk) :: kinetic_energy + real(rk) :: max_displacement + real(rk) :: max_velocity + real(rk) :: max_acceleration + + call output_csv_filename(filename) + inquire(file=trim(filename), exist=exists) + + call output_csv_collect_history(total_mass, kinetic_energy, & + max_displacement, max_velocity, & + max_acceleration) + + unit_id = 61 + open(unit=unit_id, file=trim(filename), status='unknown', & + position='append', action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open history CSV output', ierr) + return + end if + + if (.not. exists .and. .not. history_header_written) then + write(unit_id,'(A)',iostat=ios) & + 'step,time,total_mass,kinetic_energy,max_displacement,max_velocity,max_acceleration' + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write history CSV header', ierr) + return + end if + end if + + write(unit_id,'(I12,A,5(ES24.16,A),ES24.16)',iostat=ios) step, ',', time, ',', & + total_mass, ',', kinetic_energy, ',', max_displacement, ',', & + max_velocity, ',', max_acceleration + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write history CSV row', ierr) + return + end if + + close(unit_id) + history_header_written = .true. + + call output_csv_write_nodes(step, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call output_csv_write_elements(step, ierr) + if (ierr .ne. FEM_SUCCESS) return + + ierr = FEM_SUCCESS + end subroutine output_csv_write + + subroutine output_csv_reset() + history_header_written = .false. + end subroutine output_csv_reset + + subroutine output_csv_filename(filename) + character(len=*), intent(out) :: filename + + filename = ' ' + if (trim(csv_output_directory) .eq. '.') then + filename = 'history.csv' + else + filename = trim(csv_output_directory)//'/history.csv' + end if + end subroutine output_csv_filename + + subroutine output_csv_step_filename(prefix, step, filename) + character(len=*), intent(in) :: prefix + integer, intent(in) :: step + character(len=*), intent(out) :: filename + character(len=8) :: step_text + + write(step_text,'(I8.8)') step + filename = ' ' + if (trim(csv_output_directory) .eq. '.') then + filename = trim(prefix)//'_'//step_text//'.csv' + else + filename = trim(csv_output_directory)//'/'//trim(prefix)//'_'// & + step_text//'.csv' + end if + end subroutine output_csv_step_filename + + subroutine output_csv_write_nodes(step, ierr) + integer, intent(in) :: step + integer, intent(out) :: ierr + character(len=512) :: filename + integer :: unit_id + integer :: ios + integer :: node + integer :: idir + integer :: dof + real(rk) :: xyz(NDIM) + real(rk) :: uu(NDIM) + real(rk) :: vv(NDIM) + real(rk) :: aa(NDIM) + + call output_csv_step_filename('nodes', step, filename) + unit_id = 62 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open node CSV output', ierr) + return + end if + + write(unit_id,'(A)',iostat=ios) & + 'node_id,x,y,z,ux,uy,uz,vx,vy,vz,ax,ay,az' + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write node CSV header', ierr) + return + end if + + do node = 1, nnode + xyz = coord0(:,node) + uu = ZERO + vv = ZERO + aa = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) then + uu(idir) = u(dof) + vv(idir) = v(dof) + aa(idir) = a(dof) + xyz(idir) = xyz(idir) + uu(idir) + end if + end do + + write(unit_id,'(I12,12(A,ES24.16))',iostat=ios) node_id_ext(node), & + ',', xyz(1), ',', xyz(2), ',', xyz(3), & + ',', uu(1), ',', uu(2), ',', uu(3), & + ',', vv(1), ',', vv(2), ',', vv(3), & + ',', aa(1), ',', aa(2), ',', aa(3) + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write node CSV row', ierr) + return + end if + end do + + close(unit_id) + ierr = FEM_SUCCESS + end subroutine output_csv_write_nodes + + subroutine output_csv_write_elements(step, ierr) + integer, intent(in) :: step + integer, intent(out) :: ierr + character(len=512) :: filename + integer :: unit_id + integer :: ios + integer :: elem + real(rk) :: sig(NSTRESS) + real(rk) :: eps(NSTRESS) + real(rk) :: peps(NSTRESS) + real(rk) :: eqps + real(rk) :: damage + + call output_csv_step_filename('elements', step, filename) + unit_id = 63 + open(unit=unit_id, file=trim(filename), status='replace', & + action='write', iostat=ios) + if (ios .ne. 0) then + call error_set(FEM_ERR_OUTPUT, 'failed to open element CSV output', ierr) + return + end if + + write(unit_id,'(A)',iostat=ios) & + 'elem_id,status,s11,s22,s33,s12,s13,s23,e11,e22,e33,e12,e13,e23,'// & + 'pe11,pe22,pe33,pe12,pe13,pe23,eqps,damage' + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write element CSV header', ierr) + return + end if + + do elem = 1, nelem + call output_csv_average_element(elem, sig, eps, peps, eqps, damage) + write(unit_id,'(I12,A,I4,20(A,ES24.16))',iostat=ios) & + elem_id_ext(elem), ',', elem_status(elem), & + ',', sig(1), ',', sig(2), ',', sig(3), & + ',', sig(4), ',', sig(5), ',', sig(6), & + ',', eps(1), ',', eps(2), ',', eps(3), & + ',', eps(4), ',', eps(5), ',', eps(6), & + ',', peps(1), ',', peps(2), ',', peps(3), & + ',', peps(4), ',', peps(5), ',', peps(6), & + ',', eqps, ',', damage + if (ios .ne. 0) then + close(unit_id) + call error_set(FEM_ERR_OUTPUT, 'failed to write element CSV row', ierr) + return + end if + end do + + close(unit_id) + ierr = FEM_SUCCESS + end subroutine output_csv_write_elements + + subroutine output_csv_average_element(elem, sig, eps, peps, eqps, damage) + integer, intent(in) :: elem + real(rk), intent(out) :: sig(NSTRESS) + real(rk), intent(out) :: eps(NSTRESS) + real(rk), intent(out) :: peps(NSTRESS) + real(rk), intent(out) :: eqps + real(rk), intent(out) :: damage + integer :: gp + integer :: first_gp + integer :: last_gp + integer :: gp_count + + sig = ZERO + eps = ZERO + peps = ZERO + eqps = ZERO + damage = ZERO + + if (.not. allocated(elem_gp_start)) return + if (.not. allocated(stress)) return + if (.not. allocated(strain)) return + if (.not. allocated(statev)) return + if (elem .lt. 1 .or. elem .gt. nelem) return + + first_gp = elem_gp_start(elem) + gp_count = elem_ngp(elem) + if (first_gp .le. 0 .or. gp_count .le. 0) return + last_gp = first_gp + gp_count - 1 + + do gp = first_gp, last_gp + sig = sig + stress(:,gp) + eps = eps + strain(:,gp) + peps = peps + statev(5:10,gp) + eqps = eqps + statev(1,gp) + damage = damage + statev(2,gp) + end do + + sig = sig / real(gp_count, rk) + eps = eps / real(gp_count, rk) + peps = peps / real(gp_count, rk) + eqps = eqps / real(gp_count, rk) + damage = damage / real(gp_count, rk) + end subroutine output_csv_average_element + + subroutine output_csv_collect_history(total_mass, kinetic_energy, & + max_displacement, max_velocity, & + max_acceleration) + real(rk), intent(out) :: total_mass + real(rk), intent(out) :: kinetic_energy + real(rk), intent(out) :: max_displacement + real(rk), intent(out) :: max_velocity + real(rk), intent(out) :: max_acceleration + integer :: node + integer :: idir + integer :: dof + real(rk) :: vec_norm + + total_mass = ZERO + kinetic_energy = ZERO + max_displacement = ZERO + max_velocity = ZERO + max_acceleration = ZERO + + if (allocated(mass_diag)) then + do node = 1, nnode + dof = dof_id(1,node) + if (dof .gt. 0) total_mass = total_mass + mass_diag(dof) + end do + + do dof = 1, ndof + kinetic_energy = kinetic_energy + HALF * mass_diag(dof) * v(dof) * v(dof) + end do + end if + + do node = 1, nnode + vec_norm = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) vec_norm = vec_norm + u(dof) * u(dof) + end do + max_displacement = max(max_displacement, sqrt(vec_norm)) + + vec_norm = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) vec_norm = vec_norm + v(dof) * v(dof) + end do + max_velocity = max(max_velocity, sqrt(vec_norm)) + + vec_norm = ZERO + do idir = 1, NDIM + dof = dof_id(idir,node) + if (dof .gt. 0) vec_norm = vec_norm + a(dof) * a(dof) + end do + max_acceleration = max(max_acceleration, sqrt(vec_norm)) + end do + end subroutine output_csv_collect_history + +end module fem_output_csv diff --git a/tests/data/c3d4_boundary_amplitude.inp b/tests/data/c3d4_boundary_amplitude.inp new file mode 100644 index 0000000..f5e97d9 --- /dev/null +++ b/tests/data/c3d4_boundary_amplitude.inp @@ -0,0 +1,25 @@ +*HEADING +C3D4 prescribed displacement with amplitude +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 1.0, 1.0 +*BOUNDARY +1, 1, 3, 0.0 +*BOUNDARY, AMPLITUDE=RAMP +2, 1, 1, 0.01 +*STEP +*DYNAMIC +0.5, 1.0 +*END STEP diff --git a/tests/data/c3d4_cload_amplitude.inp b/tests/data/c3d4_cload_amplitude.inp new file mode 100644 index 0000000..abf8e8d --- /dev/null +++ b/tests/data/c3d4_cload_amplitude.inp @@ -0,0 +1,19 @@ +*HEADING +C3D4 nodal load with amplitude +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*AMPLITUDE, NAME=RAMP +0.0, 0.0, 1.0, 2.0 +*CLOAD, AMPLITUDE=RAMP +2, 1, 10.0 diff --git a/tests/data/c3d4_elastic_wave_pair.inp b/tests/data/c3d4_elastic_wave_pair.inp new file mode 100644 index 0000000..493381f --- /dev/null +++ b/tests/data/c3d4_elastic_wave_pair.inp @@ -0,0 +1,27 @@ +*HEADING +C3D4 two element elastic wave pair +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +5, 1.0, 1.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +2, 2, 3, 4, 5 +*MATERIAL, NAME=STEEL +*DENSITY +1.0 +*ELASTIC +2.10E5, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*AMPLITUDE, NAME=PULSE +0.0, 1.0, 1.0E-4, 0.0 +*BOUNDARY +1, 1, 3, 0.0 +*CLOAD, AMPLITUDE=PULSE +5, 1, 100.0 +*STEP +*DYNAMIC +1.0E-4, 2.0E-3 +*END STEP diff --git a/tests/data/c3d4_j2_eqps_failure.inp b/tests/data/c3d4_j2_eqps_failure.inp new file mode 100644 index 0000000..c971046 --- /dev/null +++ b/tests/data/c3d4_j2_eqps_failure.inp @@ -0,0 +1,19 @@ +*HEADING +C3D4 J2 plasticity with EQPS failure +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_FAIL +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC +200.0, 0.0 +*DAMAGE INITIATION, CRITERION=DUCTILE +1.0E-4 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_FAIL diff --git a/tests/data/c3d4_j2_ideal_shear.inp b/tests/data/c3d4_j2_ideal_shear.inp new file mode 100644 index 0000000..3cdbd0c --- /dev/null +++ b/tests/data/c3d4_j2_ideal_shear.inp @@ -0,0 +1,17 @@ +*HEADING +C3D4 ideal J2 plasticity simple shear +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_J2 +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC +200.0, 0.0 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_J2 diff --git a/tests/data/c3d4_j2_linear_shear.inp b/tests/data/c3d4_j2_linear_shear.inp new file mode 100644 index 0000000..83d0720 --- /dev/null +++ b/tests/data/c3d4_j2_linear_shear.inp @@ -0,0 +1,18 @@ +*HEADING +C3D4 linear hardening J2 plasticity simple shear +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_J2_LINEAR +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC +200.0, 0.0 +400.0, 0.01 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_J2_LINEAR diff --git a/tests/data/c3d4_j2_multilinear_shear.inp b/tests/data/c3d4_j2_multilinear_shear.inp new file mode 100644 index 0000000..3609c4d --- /dev/null +++ b/tests/data/c3d4_j2_multilinear_shear.inp @@ -0,0 +1,19 @@ +*HEADING +C3D4 multilinear hardening J2 plasticity simple shear +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_J2_MULTI +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC +200.0, 0.0 +250.0, 0.001 +400.0, 0.01 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_J2_MULTI diff --git a/tests/data/c3d4_jc_damage_shear.inp b/tests/data/c3d4_jc_damage_shear.inp new file mode 100644 index 0000000..a365303 --- /dev/null +++ b/tests/data/c3d4_jc_damage_shear.inp @@ -0,0 +1,19 @@ +*HEADING +C3D4 Johnson-Cook damage initiation simple shear +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_JC_DAMAGE +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC, HARDENING=JOHNSON COOK +200.0, 0.0, 1.0 +*DAMAGE INITIATION, CRITERION=JOHNSON COOK +1.0E-4, 0.0, 0.0, 0.0, 0.0 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_JC_DAMAGE diff --git a/tests/data/c3d4_jc_rate_shear.inp b/tests/data/c3d4_jc_rate_shear.inp new file mode 100644 index 0000000..fc3a869 --- /dev/null +++ b/tests/data/c3d4_jc_rate_shear.inp @@ -0,0 +1,19 @@ +*HEADING +C3D4 Johnson-Cook rate dependent plasticity +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL_JC +*DENSITY +7850.0 +*ELASTIC +2.10E5, 0.30 +*PLASTIC, HARDENING=JOHNSON COOK +200.0, 0.0, 1.0 +*RATE DEPENDENT, TYPE=JOHNSON COOK +0.05, 1.0 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL_JC diff --git a/tests/data/c3d8_elastic_wave_bar.inp b/tests/data/c3d8_elastic_wave_bar.inp new file mode 100644 index 0000000..b560f65 --- /dev/null +++ b/tests/data/c3d8_elastic_wave_bar.inp @@ -0,0 +1,38 @@ +*HEADING +C3D8 two element elastic wave bar +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 1.0, 1.0, 0.0 +4, 0.0, 1.0, 0.0 +5, 0.0, 0.0, 1.0 +6, 1.0, 0.0, 1.0 +7, 1.0, 1.0, 1.0 +8, 0.0, 1.0, 1.0 +9, 2.0, 0.0, 0.0 +10, 2.0, 1.0, 0.0 +11, 2.0, 0.0, 1.0 +12, 2.0, 1.0, 1.0 +*ELEMENT, TYPE=C3D8, ELSET=EALL +1, 1, 2, 3, 4, 5, 6, 7, 8 +2, 2, 9, 10, 3, 6, 11, 12, 7 +*NSET, NSET=LEFT +1, 4, 5, 8 +*NSET, NSET=RIGHT +9, 10, 11, 12 +*MATERIAL, NAME=STEEL +*DENSITY +1.0 +*ELASTIC +2.10E5, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*AMPLITUDE, NAME=PULSE +0.0, 1.0, 1.0E-4, 0.0 +*BOUNDARY +LEFT, 1, 3, 0.0 +*CLOAD, AMPLITUDE=PULSE +RIGHT, 1, 100.0 +*STEP +*DYNAMIC +1.0E-4, 2.0E-3 +*END STEP diff --git a/tests/data/c3d8_two_element_shared_face.inp b/tests/data/c3d8_two_element_shared_face.inp new file mode 100644 index 0000000..d5ba89d --- /dev/null +++ b/tests/data/c3d8_two_element_shared_face.inp @@ -0,0 +1,24 @@ +*HEADING +Two C3D8 elements sharing one internal face +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 1.0, 1.0, 0.0 +4, 0.0, 1.0, 0.0 +5, 0.0, 0.0, 1.0 +6, 1.0, 0.0, 1.0 +7, 1.0, 1.0, 1.0 +8, 0.0, 1.0, 1.0 +9, 2.0, 0.0, 0.0 +10, 2.0, 1.0, 0.0 +11, 2.0, 0.0, 1.0 +12, 2.0, 1.0, 1.0 +*ELEMENT, TYPE=C3D8, ELSET=EALL +1, 1, 2, 3, 4, 5, 6, 7, 8 +2, 2, 9, 10, 3, 6, 11, 12, 7 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL diff --git a/tests/data/invalid_boundary_dof.inp b/tests/data/invalid_boundary_dof.inp new file mode 100644 index 0000000..9d4df05 --- /dev/null +++ b/tests/data/invalid_boundary_dof.inp @@ -0,0 +1,21 @@ +*HEADING +Invalid model: boundary dof outside supported range +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*BOUNDARY +1, 7, 7, 0.0 +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/tests/data/invalid_missing_density.inp b/tests/data/invalid_missing_density.inp new file mode 100644 index 0000000..81613b9 --- /dev/null +++ b/tests/data/invalid_missing_density.inp @@ -0,0 +1,17 @@ +*HEADING +Invalid model: missing density +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/tests/data/invalid_unknown_cload_target.inp b/tests/data/invalid_unknown_cload_target.inp new file mode 100644 index 0000000..259988d --- /dev/null +++ b/tests/data/invalid_unknown_cload_target.inp @@ -0,0 +1,21 @@ +*HEADING +Invalid model: cload target is not a node or node set +*NODE +1, 0.0, 0.0, 0.0 +2, 1.0, 0.0, 0.0 +3, 0.0, 1.0, 0.0 +4, 0.0, 0.0, 1.0 +*ELEMENT, TYPE=C3D4, ELSET=EALL +1, 1, 2, 3, 4 +*MATERIAL, NAME=STEEL +*DENSITY +7850.0 +*ELASTIC +2.10E11, 0.30 +*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL +*CLOAD +UNKNOWN_SET, 3, -1.0E3 +*STEP +*DYNAMIC +1.0E-7, 1.0E-5 +*END STEP diff --git a/tests/performance/generate_p1_c3d4.py b/tests/performance/generate_p1_c3d4.py new file mode 100644 index 0000000..61daf48 --- /dev/null +++ b/tests/performance/generate_p1_c3d4.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +"""Generate the FemLite P1 performance input deck. + +The generated case has exactly 10000 nodes, 10000 C3D4 elements, and a +100-step explicit dynamic control. It is intentionally generated outside git. +""" + +import argparse +from pathlib import Path + + +NX = 20 +NY = 20 +NZ = 25 +TARGET_ELEMS = 10000 + + +def node_id(i, j, k): + return 1 + i + NX * (j + NY * k) + + +def coords(nid): + idx = nid - 1 + i = idx % NX + j = (idx // NX) % NY + k = idx // (NX * NY) + return float(i), float(j), float(k) + + +def det3(a, b, c, d): + ax, ay, az = coords(a) + bx, by, bz = coords(b) + cx, cy, cz = coords(c) + dx, dy, dz = coords(d) + ux, uy, uz = bx - ax, by - ay, bz - az + vx, vy, vz = cx - ax, cy - ay, cz - az + wx, wy, wz = dx - ax, dy - ay, dz - az + return ( + ux * (vy * wz - vz * wy) + - vx * (uy * wz - uz * wy) + + wx * (uy * vz - uz * vy) + ) + + +def oriented(tet): + a, b, c, d = tet + if det3(a, b, c, d) > 0.0: + return tet + return (a, c, b, d) + + +def cell_tets(i, j, k): + n000 = node_id(i, j, k) + n100 = node_id(i + 1, j, k) + n010 = node_id(i, j + 1, k) + n110 = node_id(i + 1, j + 1, k) + n001 = node_id(i, j, k + 1) + n101 = node_id(i + 1, j, k + 1) + n011 = node_id(i, j + 1, k + 1) + n111 = node_id(i + 1, j + 1, k + 1) + return ( + (n000, n100, n110, n111), + (n000, n110, n010, n111), + (n000, n010, n011, n111), + (n000, n011, n001, n111), + (n000, n001, n101, n111), + (n000, n101, n100, n111), + ) + + +def generate_elements(): + cells = [ + (i, j, k) + for k in range(NZ - 1) + for j in range(NY - 1) + for i in range(NX - 1) + ] + elems = [] + for index, (i, j, k) in enumerate(cells): + variants = cell_tets(i, j, k) + elems.append(oriented(variants[index % len(variants)])) + for pass_id in range(1, 6): + for index, (i, j, k) in enumerate(cells): + if len(elems) >= TARGET_ELEMS: + return elems + variants = cell_tets(i, j, k) + elems.append(oriented(variants[(index + pass_id) % len(variants)])) + return elems + + +def write_inp(path): + elems = generate_elements() + if len(elems) != TARGET_ELEMS: + raise RuntimeError(f"generated {len(elems)} elements, expected {TARGET_ELEMS}") + + with path.open("w", encoding="ascii", newline="\n") as f: + f.write("*HEADING\n") + f.write("FemLite P1 performance 10000 nodes 10000 C3D4 100 steps\n") + f.write("*NODE\n") + for nid in range(1, NX * NY * NZ + 1): + x, y, z = coords(nid) + f.write(f"{nid}, {x:.6f}, {y:.6f}, {z:.6f}\n") + f.write("*ELEMENT, TYPE=C3D4, ELSET=EALL\n") + for eid, tet in enumerate(elems, start=1): + f.write(f"{eid}, {tet[0]}, {tet[1]}, {tet[2]}, {tet[3]}\n") + f.write("*MATERIAL, NAME=STEEL\n") + f.write("*DENSITY\n") + f.write("7850.0\n") + f.write("*ELASTIC\n") + f.write("2.10E5, 0.30\n") + f.write("*SOLID SECTION, ELSET=EALL, MATERIAL=STEEL\n") + f.write("*STEP\n") + f.write("*DYNAMIC\n") + f.write("1.0E-6, 1.0E-4\n") + f.write("*END STEP\n") + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("output", type=Path) + args = parser.parse_args() + args.output.parent.mkdir(parents=True, exist_ok=True) + write_inp(args.output) + + +if __name__ == "__main__": + main() diff --git a/tests/performance/perf_p1.f90 b/tests/performance/perf_p1.f90 new file mode 100644 index 0000000..6f2b575 --- /dev/null +++ b/tests/performance/perf_p1.f90 @@ -0,0 +1,107 @@ +program perf_p1 + use fem_precision + use fem_api + implicit none + + integer :: ierr + integer :: major + integer :: minor + integer :: patch + integer :: argc + integer :: num_nodes + integer :: num_elems + integer :: num_dof + integer :: num_nsets + integer :: num_elsets + integer :: num_materials + integer :: num_sections + integer :: num_nset_items + integer :: num_elset_items + integer :: step + real(rk) :: time + real(rk) :: t0 + real(rk) :: t1 + character(len=256) :: filename + + intrinsic iargc + intrinsic getarg + intrinsic exit + + argc = iargc() + if (argc .lt. 1) then + write(*,'(A)') 'usage: perf_p1 ' + call exit(2) + end if + + call getarg(1, filename) + + call fem_initialize(ierr) + call exit_if_error(ierr) + + call fem_get_version(major, minor, patch, ierr) + call exit_if_error(ierr) + if (major .ne. 0 .or. minor .ne. 1 .or. patch .ne. 0) then + write(*,'(A,I1,A,I1,A,I1)') 'unexpected FemLite version ', major, '.', minor, '.', patch + call exit(3) + end if + + call fem_read_inp(trim(filename), ierr) + call exit_if_error(ierr) + + call fem_set_output_interval(0, ierr) + call exit_if_error(ierr) + + call fem_build_model(ierr) + call exit_if_error(ierr) + + call fem_get_model_counts(num_nodes, num_elems, num_dof, num_nsets, & + num_elsets, num_materials, num_sections, & + num_nset_items, num_elset_items, ierr) + call exit_if_error(ierr) + if (num_nodes .ne. 10000 .or. num_elems .ne. 10000) then + write(*,'(A,I8,A,I8)') 'unexpected P1 model size nodes=', num_nodes, ' elems=', num_elems + call exit(4) + end if + + call cpu_time(t0) + call fem_run_explicit(ierr) + call cpu_time(t1) + call exit_if_error(ierr) + + call fem_get_current_time(time, step, ierr) + call exit_if_error(ierr) + if (step .ne. 100) then + write(*,'(A,I8)') 'unexpected P1 step count ', step + call exit(5) + end if + + write(*,'(A)') 'FemLite P1 performance' + write(*,'(A,I1,A,I1,A,I1)') ' version: ', major, '.', minor, '.', patch + write(*,'(A,I8)') ' nodes: ', num_nodes + write(*,'(A,I8)') ' elements:', num_elems + write(*,'(A,I8)') ' dofs: ', num_dof + write(*,'(A,I8)') ' steps: ', step + write(*,'(A,ES12.5)') ' time: ', time + write(*,'(A,F12.6)') ' cpu_sec: ', t1 - t0 + write(*,'(A)') ' result: PASS' + + call fem_finalize(ierr) + call exit_if_error(ierr) + +contains + + subroutine exit_if_error(ierr_in) + integer, intent(in) :: ierr_in + integer :: code + integer :: ierr_local + character(len=256) :: message + + if (ierr_in .eq. 0) return + + call fem_get_last_error(code, message, ierr_local) + write(*,'(A,I8)') 'FemLite error code: ', code + write(*,'(A,A)') 'FemLite error: ', trim(message) + call fem_finalize(ierr_local) + call exit(1) + end subroutine exit_if_error +end program perf_p1 diff --git a/tests/unit/test_material_j2.f90 b/tests/unit/test_material_j2.f90 new file mode 100644 index 0000000..2a36813 --- /dev/null +++ b/tests/unit/test_material_j2.f90 @@ -0,0 +1,458 @@ +program test_material_j2 + use fem_precision + use fem_error + use fem_database + use fem_material_j2 + implicit none + + real(rk), parameter :: E_TEST = 2.10e5_rk + real(rk), parameter :: NU_TEST = 0.30_rk + real(rk), parameter :: YIELD_TEST = 200.0_rk + real(rk), parameter :: HARD_TEST = 2.00e4_rk + real(rk), parameter :: TOL = 1.0e-8_rk + + integer :: ierr + real(rk) :: deps(NSTRESS) + real(rk) :: stress_old(NSTRESS) + real(rk) :: stress_new(NSTRESS) + real(rk) :: state_old(NSTATEV_MAX) + real(rk) :: state_new(NSTATEV_MAX) + real(rk) :: lateral_deps + real(rk) :: eps_total + real(rk) :: eqps_expected + real(rk) :: sigma_expected + real(rk) :: stress_low_rate + real(rk) :: temp_expected + real(rk) :: spin_inc(3) + + call setup_elastic(ierr) + if (ierr .ne. FEM_SUCCESS) stop 1 + + deps = ZERO + deps(1) = 1.0e-3_rk + deps(2) = -NU_TEST * deps(1) + deps(3) = -NU_TEST * deps(1) + stress_old = ZERO + state_old = ZERO + + call material_update_small_strain(1, deps, state_old, stress_old, & + stress_new, state_new, ierr) + if (ierr .ne. FEM_SUCCESS) stop 2 + if (abs(stress_new(1) - E_TEST * deps(1)) .gt. TOL * E_TEST * deps(1)) stop 3 + if (abs(stress_new(2)) .gt. TOL * E_TEST * deps(1)) stop 4 + if (abs(stress_new(3)) .gt. TOL * E_TEST * deps(1)) stop 5 + if (maxval(abs(stress_new(4:6))) .gt. TOL * E_TEST * deps(1)) stop 6 + if (abs(state_new(1)) .gt. TOL) stop 7 + + deps = ZERO + stress_old = ZERO + stress_old(1) = 100.0_rk + state_old = ZERO + spin_inc = ZERO + spin_inc(1) = -1.0e-2_rk + call material_update_small_strain_dt(1, ZERO, deps, state_old, stress_old, & + stress_new, state_new, ierr, spin_inc) + if (ierr .ne. FEM_SUCCESS) stop 8 + if (abs(stress_new(1) - 100.0_rk) .gt. TOL * 100.0_rk) stop 9 + if (abs(stress_new(4) - 1.0_rk) .gt. TOL) stop 100 + + call setup_j2_linear(ierr) + if (ierr .ne. FEM_SUCCESS) stop 10 + + stress_old = ZERO + state_old = ZERO + call uniaxial_stress_update(5.0e-4_rk, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 11 + if (abs(stress_new(1) - E_TEST * 5.0e-4_rk) .gt. & + TOL * E_TEST * 5.0e-4_rk) stop 12 + if (abs(stress_new(2)) .gt. TOL * E_TEST * 5.0e-4_rk) stop 13 + if (abs(stress_new(3)) .gt. TOL * E_TEST * 5.0e-4_rk) stop 14 + if (abs(state_new(1)) .gt. TOL) stop 15 + + stress_old = ZERO + state_old = ZERO + eps_total = 5.0e-3_rk + call uniaxial_stress_update(eps_total, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 20 + + eqps_expected = (E_TEST * eps_total - YIELD_TEST) / (E_TEST + HARD_TEST) + sigma_expected = YIELD_TEST + HARD_TEST * eqps_expected + if (abs(stress_new(1) - sigma_expected) .gt. TOL * sigma_expected) stop 21 + if (abs(stress_new(2)) .gt. TOL * sigma_expected) stop 22 + if (abs(stress_new(3)) .gt. TOL * sigma_expected) stop 23 + if (abs(state_new(1) - eqps_expected) .gt. TOL * eqps_expected) stop 24 + + stress_old = stress_new + state_old = state_new + call uniaxial_stress_update(-1.0e-3_rk, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 30 + + sigma_expected = sigma_expected - E_TEST * 1.0e-3_rk + if (abs(stress_new(1) - sigma_expected) .gt. TOL * max(ONE, abs(sigma_expected))) stop 31 + if (abs(stress_new(2)) .gt. TOL * YIELD_TEST) stop 32 + if (abs(stress_new(3)) .gt. TOL * YIELD_TEST) stop 33 + if (abs(state_new(1) - eqps_expected) .gt. TOL * eqps_expected) stop 34 + + call setup_jc_rate(ierr) + if (ierr .ne. FEM_SUCCESS) stop 50 + + stress_old = ZERO + state_old = ZERO + eps_total = 5.0e-3_rk + call uniaxial_stress_update_dt(eps_total, 10.0_rk, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 51 + stress_low_rate = stress_new(1) + sigma_expected = YIELD_TEST + if (abs(stress_new(1) - sigma_expected) .gt. TOL * sigma_expected) stop 52 + if (state_new(20) .gt. ONE) stop 53 + + call setup_jc_rate(ierr) + if (ierr .ne. FEM_SUCCESS) stop 54 + + stress_old = ZERO + state_old = ZERO + call uniaxial_stress_update_dt(eps_total, 1.0e-3_rk, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 55 + call solve_jc_rate_uniaxial_expected(eps_total, 1.0e-3_rk, & + sigma_expected, eqps_expected, ierr) + if (ierr .ne. FEM_SUCCESS) stop 56 + if (stress_new(1) .le. stress_low_rate) stop 57 + if (abs(stress_new(1) - sigma_expected) .gt. TOL * sigma_expected) stop 58 + if (abs(state_new(1) - eqps_expected) .gt. TOL * eqps_expected) stop 59 + if (state_new(20) .le. ONE) stop 60 + + call setup_jc_temperature(ierr) + if (ierr .ne. FEM_SUCCESS) stop 70 + + stress_old = ZERO + state_old = ZERO + state_old(3) = 800.0_rk + eps_total = 5.0e-3_rk + call uniaxial_stress_update(eps_total, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 71 + sigma_expected = YIELD_TEST * 0.5_rk + eqps_expected = (E_TEST * eps_total - sigma_expected) / E_TEST + if (abs(stress_new(1) - sigma_expected) .gt. TOL * sigma_expected) stop 72 + if (abs(state_new(1) - eqps_expected) .gt. TOL * eqps_expected) stop 73 + if (abs(state_new(3) - 800.0_rk) .gt. TOL * 800.0_rk) stop 74 + + call setup_jc_heat(ierr) + if (ierr .ne. FEM_SUCCESS) stop 80 + + stress_old = ZERO + state_old = ZERO + eps_total = 5.0e-3_rk + call uniaxial_stress_update(eps_total, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 81 + sigma_expected = YIELD_TEST + eqps_expected = (E_TEST * eps_total - sigma_expected) / E_TEST + temp_expected = 300.0_rk + 0.9_rk * sigma_expected * eqps_expected / 100.0_rk + if (abs(stress_new(1) - sigma_expected) .gt. TOL * sigma_expected) stop 82 + if (abs(state_new(1) - eqps_expected) .gt. TOL * eqps_expected) stop 83 + if (abs(state_new(3) - temp_expected) .gt. TOL * temp_expected) stop 84 + + call setup_jc_damage(ierr) + if (ierr .ne. FEM_SUCCESS) stop 90 + + stress_old = ZERO + state_old = ZERO + eps_total = 5.0e-3_rk + call uniaxial_stress_update(eps_total, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 91 + sigma_expected = YIELD_TEST + eqps_expected = (E_TEST * eps_total - sigma_expected) / E_TEST + if (abs(state_new(2) - eqps_expected / 0.01_rk) .gt. & + TOL * eqps_expected / 0.01_rk) stop 92 + if (abs(state_new(4)) .gt. TOL) stop 93 + + stress_old = stress_new + state_old = state_new + call uniaxial_stress_update(ZERO, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 94 + if (abs(state_new(2) - state_old(2)) .gt. TOL * state_old(2)) stop 95 + if (abs(state_new(22)) .gt. TOL) stop 96 + + mat_jc_d1(1) = 1.0e-4_rk + stress_old = ZERO + state_old = ZERO + call uniaxial_stress_update(eps_total, stress_old, state_old, & + stress_new, state_new, lateral_deps, ierr) + if (ierr .ne. FEM_SUCCESS) stop 97 + if (state_new(2) .lt. ONE) stop 98 + if (state_new(4) .lt. ONE) stop 99 + + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) stop 40 + +contains + + subroutine setup_elastic(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'ELASTIC' + mat_plastic_model(1) = 'ELASTIC' + mat_density(1) = 7850.0_rk + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + ierr = FEM_SUCCESS + end subroutine setup_elastic + + subroutine setup_j2_linear(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 2, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'J2_LINEAR' + mat_plastic_model(1) = 'J2' + mat_density(1) = 7850.0_rk + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + mat_yield0(1) = YIELD_TEST + mat_hardening_modulus(1) = HARD_TEST + mat_plastic_start(1) = 1 + mat_plastic_count(1) = 2 + mat_plastic_stress(1) = YIELD_TEST + mat_plastic_strain(1) = ZERO + mat_plastic_stress(2) = YIELD_TEST + HARD_TEST * 0.01_rk + mat_plastic_strain(2) = 0.01_rk + ierr = FEM_SUCCESS + end subroutine setup_j2_linear + + subroutine setup_jc_rate(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'JC_RATE' + mat_plastic_model(1) = 'J2_JC' + mat_rate_model(1) = 'JOHNSON_COOK' + mat_density(1) = 7850.0_rk + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + mat_yield0(1) = YIELD_TEST + mat_jc_a(1) = YIELD_TEST + mat_jc_b(1) = ZERO + mat_jc_n(1) = ONE + mat_jc_c(1) = 0.05_rk + mat_jc_epsdot0(1) = ONE + ierr = FEM_SUCCESS + end subroutine setup_jc_rate + + subroutine setup_jc_temperature(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'JC_TEMP' + mat_plastic_model(1) = 'J2_JC' + mat_temp_model(1) = 'JOHNSON_COOK' + mat_density(1) = 7850.0_rk + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + mat_yield0(1) = YIELD_TEST + mat_jc_a(1) = YIELD_TEST + mat_jc_b(1) = ZERO + mat_jc_n(1) = ONE + mat_jc_m(1) = ONE + mat_jc_troom(1) = 300.0_rk + mat_jc_tmelt(1) = 1300.0_rk + ierr = FEM_SUCCESS + end subroutine setup_jc_temperature + + subroutine setup_jc_heat(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'JC_HEAT' + mat_plastic_model(1) = 'J2_JC' + mat_density(1) = ONE + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + mat_yield0(1) = YIELD_TEST + mat_jc_a(1) = YIELD_TEST + mat_jc_b(1) = ZERO + mat_jc_n(1) = ONE + mat_jc_troom(1) = 300.0_rk + mat_jc_chi(1) = 0.9_rk + mat_jc_cp(1) = 100.0_rk + ierr = FEM_SUCCESS + end subroutine setup_jc_heat + + subroutine setup_jc_damage(ierr) + integer, intent(out) :: ierr + + call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & + 0, 0, 0, 0, 0, ierr) + if (ierr .ne. FEM_SUCCESS) return + + mat_name(1) = 'JC_DAMAGE' + mat_plastic_model(1) = 'J2_JC' + mat_failure_model(1) = 'JOHNSON_COOK' + mat_density(1) = 7850.0_rk + mat_e(1) = E_TEST + mat_nu(1) = NU_TEST + mat_yield0(1) = YIELD_TEST + mat_jc_a(1) = YIELD_TEST + mat_jc_b(1) = ZERO + mat_jc_n(1) = ONE + mat_jc_d1(1) = 0.01_rk + mat_jc_d2(1) = ZERO + mat_jc_d3(1) = ZERO + mat_jc_d4(1) = ZERO + mat_jc_d5(1) = ZERO + ierr = FEM_SUCCESS + end subroutine setup_jc_damage + + subroutine uniaxial_stress_update(axial_deps, stress_in, state_in, & + stress_out, state_out, lateral_deps, ierr) + real(rk), intent(in) :: axial_deps + real(rk), intent(in) :: stress_in(NSTRESS) + real(rk), intent(in) :: state_in(NSTATEV_MAX) + real(rk), intent(out) :: stress_out(NSTRESS) + real(rk), intent(out) :: state_out(NSTATEV_MAX) + real(rk), intent(out) :: lateral_deps + integer, intent(out) :: ierr + call uniaxial_stress_update_dt(axial_deps, ZERO, stress_in, state_in, & + stress_out, state_out, lateral_deps, ierr) + end subroutine uniaxial_stress_update + + subroutine uniaxial_stress_update_dt(axial_deps, dt, stress_in, state_in, & + stress_out, state_out, lateral_deps, ierr) + real(rk), intent(in) :: axial_deps + real(rk), intent(in) :: dt + real(rk), intent(in) :: stress_in(NSTRESS) + real(rk), intent(in) :: state_in(NSTATEV_MAX) + real(rk), intent(out) :: stress_out(NSTRESS) + real(rk), intent(out) :: state_out(NSTATEV_MAX) + real(rk), intent(out) :: lateral_deps + integer, intent(out) :: ierr + real(rk) :: deps_local(NSTRESS) + real(rk) :: stress_trial(NSTRESS) + real(rk) :: state_trial(NSTATEV_MAX) + real(rk) :: stress_pert(NSTRESS) + real(rk) :: state_pert(NSTATEV_MAX) + real(rk) :: lateral + real(rk) :: residual + real(rk) :: residual_pert + real(rk) :: tangent + real(rk) :: perturb + integer :: iter + + lateral = -NU_TEST * axial_deps + perturb = max(1.0e-8_rk, abs(axial_deps) * 1.0e-6_rk) + + do iter = 1, 30 + call update_with_lateral(axial_deps, lateral, dt, stress_in, state_in, & + stress_trial, state_trial, ierr) + if (ierr .ne. FEM_SUCCESS) return + residual = HALF * (stress_trial(2) + stress_trial(3)) + if (abs(residual) .le. 1.0e-9_rk * E_TEST) exit + + call update_with_lateral(axial_deps, lateral + perturb, dt, stress_in, & + state_in, stress_pert, state_pert, ierr) + if (ierr .ne. FEM_SUCCESS) return + residual_pert = HALF * (stress_pert(2) + stress_pert(3)) + tangent = (residual_pert - residual) / perturb + if (abs(tangent) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'uniaxial test found zero lateral tangent', ierr) + return + end if + + lateral = lateral - residual / tangent + end do + + deps_local = ZERO + deps_local(1) = axial_deps + deps_local(2) = lateral + deps_local(3) = lateral + call material_update_small_strain_dt(1, dt, deps_local, state_in, & + stress_in, stress_out, state_out, ierr) + if (ierr .ne. FEM_SUCCESS) return + lateral_deps = lateral + end subroutine uniaxial_stress_update_dt + + subroutine update_with_lateral(axial_deps, lateral_deps, dt, stress_in, state_in, & + stress_out, state_out, ierr) + real(rk), intent(in) :: axial_deps + real(rk), intent(in) :: lateral_deps + real(rk), intent(in) :: dt + real(rk), intent(in) :: stress_in(NSTRESS) + real(rk), intent(in) :: state_in(NSTATEV_MAX) + real(rk), intent(out) :: stress_out(NSTRESS) + real(rk), intent(out) :: state_out(NSTATEV_MAX) + integer, intent(out) :: ierr + real(rk) :: deps_local(NSTRESS) + + deps_local = ZERO + deps_local(1) = axial_deps + deps_local(2) = lateral_deps + deps_local(3) = lateral_deps + call material_update_small_strain_dt(1, dt, deps_local, state_in, & + stress_in, stress_out, state_out, ierr) + end subroutine update_with_lateral + + subroutine solve_jc_rate_uniaxial_expected(total_strain, dt, sigma, eqps, ierr) + real(rk), intent(in) :: total_strain + real(rk), intent(in) :: dt + real(rk), intent(out) :: sigma + real(rk), intent(out) :: eqps + integer, intent(out) :: ierr + real(rk) :: residual + real(rk) :: tangent + real(rk) :: rate_factor + real(rk) :: drate + integer :: iter + + eqps = max(ZERO, (E_TEST * total_strain - YIELD_TEST) / E_TEST) + do iter = 1, 30 + if (eqps / dt .gt. ONE) then + rate_factor = ONE + 0.05_rk * log(eqps / dt) + drate = 0.05_rk / eqps + else + rate_factor = ONE + drate = ZERO + end if + + residual = E_TEST * (total_strain - eqps) - YIELD_TEST * rate_factor + if (abs(residual) .le. TOL * YIELD_TEST) exit + tangent = -E_TEST - YIELD_TEST * drate + if (abs(tangent) .le. ZERO) then + call error_set(FEM_ERR_MATERIAL, 'failed to solve JC expected stress', ierr) + return + end if + eqps = eqps - residual / tangent + if (eqps .lt. ZERO) eqps = ZERO + end do + + if (eqps / dt .gt. ONE) then + rate_factor = ONE + 0.05_rk * log(eqps / dt) + else + rate_factor = ONE + end if + sigma = YIELD_TEST * rate_factor + ierr = FEM_SUCCESS + end subroutine solve_jc_rate_uniaxial_expected + +end program test_material_j2 diff --git a/tests/unit/test_smoke.f90 b/tests/unit/test_smoke.f90 new file mode 100644 index 0000000..971b5d0 --- /dev/null +++ b/tests/unit/test_smoke.f90 @@ -0,0 +1,1035 @@ +program test_smoke + use fem_precision + use fem_api + implicit none + + integer, parameter :: TEST_MAX_FACETS = 16 + integer, parameter :: TEST_MAX_FACE_NODES = MAX_FACE_NODES + + integer :: ierr + integer :: major + integer :: minor + integer :: patch + integer :: num_nodes + integer :: num_elems + integer :: num_dof + integer :: num_nsets + integer :: num_elsets + integer :: num_materials + integer :: num_sections + integer :: num_nset_items + integer :: num_elset_items + integer :: num_bc + integer :: num_cload + integer :: num_dload + real(rk) :: time + real(rk) :: total_mass + real(rk) :: rotational_inertia + real(rk) :: expected_mass + real(rk) :: expected_rot + real(rk) :: tol + real(rk) :: force_vec(NDIM) + real(rk) :: total_force(NDIM) + real(rk) :: force_batch(NDIM,2) + real(rk) :: pressure_vals(TEST_MAX_FACETS) + real(rk) :: shear_vals(NDIM,TEST_MAX_FACETS) + real(rk) :: displacement(NDIM) + real(rk) :: velocity(NDIM) + real(rk) :: acceleration(NDIM) + real(rk) :: internal_force(NDIM) + real(rk) :: internal_force_sum(NDIM) + real(rk) :: dt_test + real(rk) :: dtcrit + real(rk) :: expected_dt + real(rk) :: dt_user_query + real(rk) :: time_end_query + real(rk) :: nodal_mass + real(rk) :: eps_test + real(rk) :: gamma_test + real(rk) :: yield_test + real(rk) :: hardening_test + real(rk) :: seq_trial + real(rk) :: seq_expected + real(rk) :: eqps_expected + real(rk) :: plastic_shear_expected + real(rk) :: lambda_test + real(rk) :: mu_test + real(rk) :: sigma_x + real(rk) :: sigma_lat + real(rk) :: tau_yield + integer :: node_ids(2) + integer :: num_facets + integer :: max_fnodes + integer :: facet_ids(TEST_MAX_FACETS) + integer :: facet_elem_ids(TEST_MAX_FACETS) + integer :: facet_face_ids(TEST_MAX_FACETS) + integer :: facet_nnodes(TEST_MAX_FACETS) + integer :: facet_conn(TEST_MAX_FACE_NODES,TEST_MAX_FACETS) + integer :: load_facet_ids(TEST_MAX_FACETS) + real(rk) :: x_face(NDIM,TEST_MAX_FACE_NODES,TEST_MAX_FACETS) + real(rk) :: v_face(NDIM,TEST_MAX_FACE_NODES,TEST_MAX_FACETS) + real(rk) :: normal_face(NDIM,TEST_MAX_FACETS) + real(rk) :: area_face(TEST_MAX_FACETS) + integer :: query_elem_ids(1) + real(rk) :: stress_avg(NSTRESS,1) + real(rk) :: strain_avg(NSTRESS,1) + real(rk) :: plastic_avg(NSTRESS,1) + real(rk) :: eqps_avg(1) + real(rk) :: damage_avg(1) + integer :: status_query(1) + integer :: max_steps_query + integer :: step + integer :: wave_step + logical :: user_dt_query + logical :: history_exists + logical :: nodes_exists + logical :: elements_exists + + tol = 1.0e-8_rk + + call fem_initialize(ierr) + if (ierr .ne. 0) stop 1 + + call fem_get_version(major, minor, patch, ierr) + if (ierr .ne. 0) stop 2 + if (major .ne. 0) stop 3 + if (minor .ne. 1) stop 4 + if (patch .ne. 0) stop 5 + + call fem_read_inp('examples/c3d4_one_element.inp', ierr) + if (ierr .ne. 0) stop 11 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 12 + + lambda_test = 2.10e11_rk * 0.30_rk / & + ((ONE + 0.30_rk) * (ONE - 2.0_rk * 0.30_rk)) + mu_test = 2.10e11_rk / (2.0_rk * (ONE + 0.30_rk)) + expected_dt = 0.8_rk / sqrt((lambda_test + 2.0_rk * mu_test) / 7850.0_rk) + call fem_estimate_stable_dt(dtcrit, ierr) + if (ierr .ne. 0) stop 319 + if (abs(dtcrit - expected_dt) .gt. tol * expected_dt) stop 320 + call fem_get_time_control(dt_user_query, time_end_query, max_steps_query, & + user_dt_query, ierr) + if (ierr .ne. 0) stop 321 + if (.not. user_dt_query) stop 322 + if (abs(dt_user_query - 1.0e-7_rk) .gt. tol * 1.0e-7_rk) stop 323 + if (abs(time_end_query - 1.0e-5_rk) .gt. tol * 1.0e-5_rk) stop 324 + if (max_steps_query .ne. 100) stop 325 + + call fem_get_model_size(num_nodes, num_elems, num_dof, ierr) + if (ierr .ne. 0) stop 13 + if (num_nodes .ne. 4) stop 14 + if (num_elems .ne. 1) stop 15 + if (num_dof .ne. 12) stop 16 + call fem_get_model_counts(num_nodes, num_elems, num_dof, num_nsets, & + num_elsets, num_materials, num_sections, & + num_nset_items, num_elset_items, ierr) + if (ierr .ne. 0) stop 29 + if (num_nsets .ne. 1) stop 30 + if (num_elsets .ne. 1) stop 31 + if (num_materials .ne. 1) stop 32 + if (num_sections .ne. 1) stop 33 + call fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + if (ierr .ne. 0) stop 46 + if (num_bc .ne. 1) stop 47 + if (num_cload .ne. 0) stop 48 + if (num_dload .ne. 0) stop 49 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 68 + call fem_get_mass_summary(total_mass, rotational_inertia, ierr) + if (ierr .ne. 0) stop 69 + expected_mass = 7850.0_rk / 6.0_rk + if (abs(total_mass - expected_mass) .gt. tol * expected_mass) stop 70 + if (abs(rotational_inertia) .gt. tol) stop 71 + call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) + if (ierr .ne. 0) stop 104 + if (num_facets .ne. 4) stop 105 + if (max_fnodes .ne. 3) stop 106 + call fem_get_boundary_facets(TEST_MAX_FACETS, TEST_MAX_FACE_NODES, & + facet_ids, facet_elem_ids, facet_face_ids, & + facet_nnodes, facet_conn, x_face, v_face, & + normal_face, area_face, ierr) + if (ierr .ne. 0) stop 107 + if (facet_ids(1) .ne. 1) stop 108 + if (facet_elem_ids(1) .ne. 1) stop 109 + if (facet_face_ids(1) .ne. 1) stop 110 + if (facet_nnodes(1) .ne. 3) stop 111 + if (abs(area_face(1) - 0.5_rk) .gt. tol) stop 112 + if (abs(normal_face(1,1)) .gt. tol) stop 113 + if (abs(normal_face(2,1)) .gt. tol) stop 114 + if (abs(normal_face(3,1) + ONE) .gt. tol) stop 115 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 80 + force_vec = ZERO + force_vec(1) = 10.0_rk + force_vec(2) = -2.0_rk + call fem_apply_sph_nodal_force(1, force_vec, ierr) + if (ierr .ne. 0) stop 81 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 82 + if (abs(total_force(1) - 10.0_rk) .gt. tol) stop 83 + if (abs(total_force(2) + 2.0_rk) .gt. tol) stop 84 + if (abs(total_force(3)) .gt. tol) stop 85 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 86 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 87 + if (maxval(abs(total_force)) .gt. tol) stop 88 + load_facet_ids(1) = 1 + pressure_vals = ZERO + pressure_vals(1) = 2.0_rk + call fem_apply_sph_facet_pressure(1, load_facet_ids, pressure_vals, ierr) + if (ierr .ne. 0) stop 116 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 117 + if (abs(total_force(1)) .gt. tol) stop 118 + if (abs(total_force(2)) .gt. tol) stop 119 + if (abs(total_force(3) - 1.0_rk) .gt. tol) stop 120 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 121 + shear_vals = ZERO + shear_vals(1,1) = 3.0_rk + call fem_apply_sph_facet_tractions(1, load_facet_ids, pressure_vals, & + shear_vals, ierr) + if (ierr .ne. 0) stop 122 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 123 + if (abs(total_force(1) - 1.5_rk) .gt. tol) stop 124 + if (abs(total_force(2)) .gt. tol) stop 125 + if (abs(total_force(3) - 1.0_rk) .gt. tol) stop 126 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 127 + node_ids(1) = 1 + node_ids(2) = 2 + force_batch = ZERO + force_batch(1,1) = 1.0_rk + force_batch(2,1) = 2.0_rk + force_batch(3,1) = 3.0_rk + force_batch(1,2) = 4.0_rk + force_batch(2,2) = 5.0_rk + force_batch(3,2) = 6.0_rk + call fem_apply_sph_nodal_forces(2, node_ids, force_batch, ierr) + if (ierr .ne. 0) stop 89 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 90 + if (abs(total_force(1) - 5.0_rk) .gt. tol) stop 91 + if (abs(total_force(2) - 7.0_rk) .gt. tol) stop 92 + if (abs(total_force(3) - 9.0_rk) .gt. tol) stop 93 + dt_test = 1.0e-3_rk + call fem_advance_explicit(dt_test, ierr) + if (ierr .ne. 0) stop 94 + call fem_get_current_time(time, step, ierr) + if (ierr .ne. 0) stop 95 + if (step .ne. 1) stop 96 + if (abs(time - dt_test) .gt. tol * dt_test) stop 97 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 98 + nodal_mass = expected_mass / 4.0_rk + if (abs(acceleration(1) - 4.0_rk / nodal_mass) .gt. tol) stop 99 + if (abs(acceleration(2) - 5.0_rk / nodal_mass) .gt. tol) stop 100 + if (abs(acceleration(3) - 6.0_rk / nodal_mass) .gt. tol) stop 101 + if (abs(velocity(1) - acceleration(1) * dt_test) .gt. tol) stop 102 + if (abs(displacement(1) - HALF * acceleration(1) * dt_test**2) .gt. tol) stop 103 + call fem_get_node_kinematics(1, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 174 + if (maxval(abs(displacement)) .gt. tol) stop 175 + if (maxval(abs(velocity)) .gt. tol) stop 176 + if (maxval(abs(acceleration)) .gt. tol) stop 177 + + call fem_read_inp('tests/data/c3d4_cload_amplitude.inp', ierr) + if (ierr .ne. 0) stop 335 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 336 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 337 + + expected_mass = 7850.0_rk / 6.0_rk + nodal_mass = expected_mass / 4.0_rk + call fem_advance_explicit(0.5_rk, ierr) + if (ierr .ne. 0) stop 338 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 339 + if (maxval(abs(acceleration)) .gt. tol) stop 340 + + call fem_advance_explicit(0.5_rk, ierr) + if (ierr .ne. 0) stop 341 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 342 + if (abs(acceleration(1) - 10.0_rk / nodal_mass) .gt. tol) stop 343 + if (abs(acceleration(2)) .gt. tol) stop 344 + if (abs(acceleration(3)) .gt. tol) stop 345 + + call fem_read_inp('tests/data/c3d4_boundary_amplitude.inp', ierr) + if (ierr .ne. 0) stop 428 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 429 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 430 + + call fem_advance_explicit(0.5_rk, ierr) + if (ierr .ne. 0) stop 431 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 432 + if (abs(displacement(1) - 0.005_rk) .gt. tol * 0.01_rk) stop 433 + if (abs(velocity(1)) .gt. tol) stop 434 + + call fem_advance_explicit(0.5_rk, ierr) + if (ierr .ne. 0) stop 435 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 436 + if (abs(displacement(1) - 0.01_rk) .gt. tol * 0.01_rk) stop 437 + call fem_get_node_kinematics(1, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 438 + if (maxval(abs(displacement)) .gt. tol) stop 439 + + call fem_read_inp('tests/data/c3d4_jc_rate_shear.inp', ierr) + if (ierr .ne. 0) stop 346 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 347 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 348 + + gamma_test = 5.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 349 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 350 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 351 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 352 + + call fem_advance_explicit(1.0e-3_rk, ierr) + if (ierr .ne. 0) stop 353 + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 354 + yield_test = 200.0_rk + tau_yield = yield_test / sqrt(3.0_rk) + if (stress_avg(4,1) .le. tau_yield) stop 355 + if (eqps_avg(1) .le. ZERO) stop 356 + + call fem_read_inp('tests/data/c3d4_jc_damage_shear.inp', ierr) + if (ierr .ne. 0) stop 396 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 397 + + gamma_test = 5.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 398 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 399 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 400 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 401 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 402 + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 403 + call fem_get_element_status(1, query_elem_ids, status_query, ierr) + if (ierr .ne. 0) stop 404 + if (status_query(1) .ne. 1) stop 405 + if (damage_avg(1) .lt. ONE) stop 406 + if (eqps_avg(1) .le. ZERO) stop 407 + + call fem_read_inp('examples/c3d4_one_element.inp', ierr) + if (ierr .ne. 0) stop 190 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 191 + + eps_test = 1.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 192 + displacement(1) = eps_test + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 193 + displacement = ZERO + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 194 + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 195 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 196 + + lambda_test = 2.10e11_rk * 0.30_rk / & + ((ONE + 0.30_rk) * (ONE - 2.0_rk * 0.30_rk)) + mu_test = 2.10e11_rk / (2.0_rk * (ONE + 0.30_rk)) + sigma_x = (lambda_test + 2.0_rk * mu_test) * eps_test + sigma_lat = lambda_test * eps_test + + internal_force_sum = ZERO + call fem_get_node_internal_force(1, internal_force, ierr) + if (ierr .ne. 0) stop 197 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) + sigma_x / 6.0_rk) .gt. tol * sigma_x) stop 198 + if (abs(internal_force(2) + sigma_lat / 6.0_rk) .gt. tol * sigma_lat) stop 199 + if (abs(internal_force(3) + sigma_lat / 6.0_rk) .gt. tol * sigma_lat) stop 200 + + call fem_get_node_internal_force(2, internal_force, ierr) + if (ierr .ne. 0) stop 201 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) - sigma_x / 6.0_rk) .gt. tol * sigma_x) stop 202 + if (abs(internal_force(2)) .gt. tol * sigma_lat) stop 203 + if (abs(internal_force(3)) .gt. tol * sigma_lat) stop 204 + + call fem_get_node_internal_force(3, internal_force, ierr) + if (ierr .ne. 0) stop 205 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1)) .gt. tol * sigma_x) stop 206 + if (abs(internal_force(2) - sigma_lat / 6.0_rk) .gt. tol * sigma_lat) stop 207 + if (abs(internal_force(3)) .gt. tol * sigma_lat) stop 208 + + call fem_get_node_internal_force(4, internal_force, ierr) + if (ierr .ne. 0) stop 209 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1)) .gt. tol * sigma_x) stop 210 + if (abs(internal_force(2)) .gt. tol * sigma_lat) stop 211 + if (abs(internal_force(3) - sigma_lat / 6.0_rk) .gt. tol * sigma_lat) stop 212 + if (maxval(abs(internal_force_sum)) .gt. tol * sigma_x) stop 213 + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 214 + if (abs(strain_avg(1,1) - eps_test) .gt. tol * eps_test) stop 215 + if (abs(strain_avg(2,1)) .gt. tol * eps_test) stop 216 + if (abs(strain_avg(3,1)) .gt. tol * eps_test) stop 217 + if (maxval(abs(strain_avg(4:6,1))) .gt. tol * eps_test) stop 218 + if (abs(stress_avg(1,1) - sigma_x) .gt. tol * sigma_x) stop 219 + if (abs(stress_avg(2,1) - sigma_lat) .gt. tol * sigma_lat) stop 220 + if (abs(stress_avg(3,1) - sigma_lat) .gt. tol * sigma_lat) stop 221 + if (maxval(abs(stress_avg(4:6,1))) .gt. tol * sigma_x) stop 222 + if (maxval(abs(plastic_avg(:,1))) .gt. tol) stop 223 + if (abs(eqps_avg(1)) .gt. tol) stop 224 + if (abs(damage_avg(1)) .gt. tol) stop 225 + + call fem_read_inp('examples/c3d8_one_element.inp', ierr) + if (ierr .ne. 0) stop 17 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 18 + + call fem_get_model_size(num_nodes, num_elems, num_dof, ierr) + if (ierr .ne. 0) stop 19 + if (num_nodes .ne. 8) stop 20 + if (num_elems .ne. 1) stop 21 + if (num_dof .ne. 24) stop 22 + call fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + if (ierr .ne. 0) stop 50 + if (num_bc .ne. 0) stop 51 + if (num_cload .ne. 1) stop 52 + if (num_dload .ne. 0) stop 53 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 72 + call fem_get_mass_summary(total_mass, rotational_inertia, ierr) + if (ierr .ne. 0) stop 73 + expected_mass = 7850.0_rk + if (abs(total_mass - expected_mass) .gt. tol * expected_mass) stop 74 + if (abs(rotational_inertia) .gt. tol) stop 75 + call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) + if (ierr .ne. 0) stop 128 + if (num_facets .ne. 6) stop 129 + if (max_fnodes .ne. 4) stop 130 + call fem_get_boundary_facets(TEST_MAX_FACETS, TEST_MAX_FACE_NODES, & + facet_ids, facet_elem_ids, facet_face_ids, & + facet_nnodes, facet_conn, x_face, v_face, & + normal_face, area_face, ierr) + if (ierr .ne. 0) stop 131 + if (facet_face_ids(2) .ne. 2) stop 132 + if (facet_nnodes(2) .ne. 4) stop 133 + if (abs(area_face(2) - ONE) .gt. tol) stop 134 + if (abs(normal_face(1,2)) .gt. tol) stop 135 + if (abs(normal_face(2,2)) .gt. tol) stop 136 + if (abs(normal_face(3,2) - ONE) .gt. tol) stop 137 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 138 + load_facet_ids(1) = 2 + pressure_vals = ZERO + pressure_vals(1) = 2.0_rk + call fem_apply_sph_facet_pressure(1, load_facet_ids, pressure_vals, ierr) + if (ierr .ne. 0) stop 139 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 140 + if (abs(total_force(1)) .gt. tol) stop 141 + if (abs(total_force(2)) .gt. tol) stop 142 + if (abs(total_force(3) + 2.0_rk) .gt. tol) stop 143 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 144 + shear_vals = ZERO + shear_vals(1,1) = ONE + call fem_apply_sph_facet_tractions(1, load_facet_ids, pressure_vals, & + shear_vals, ierr) + if (ierr .ne. 0) stop 145 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 146 + if (abs(total_force(1) - ONE) .gt. tol) stop 147 + if (abs(total_force(2)) .gt. tol) stop 148 + if (abs(total_force(3) + 2.0_rk) .gt. tol) stop 149 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 178 + dt_test = 1.0e-3_rk + call fem_advance_explicit(dt_test, ierr) + if (ierr .ne. 0) stop 179 + call fem_get_node_kinematics(7, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 180 + nodal_mass = expected_mass / 8.0_rk + if (abs(acceleration(1)) .gt. tol) stop 181 + if (abs(acceleration(2)) .gt. tol) stop 182 + if (abs(acceleration(3) + 1000.0_rk / nodal_mass) .gt. tol) stop 183 + + call fem_read_inp('examples/c3d8_one_element.inp', ierr) + if (ierr .ne. 0) stop 226 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 227 + + eps_test = 1.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 228 + displacement(1) = eps_test + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 229 + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 230 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 231 + call fem_set_node_displacement(5, displacement, ierr) + if (ierr .ne. 0) stop 232 + displacement(1) = eps_test + call fem_set_node_displacement(6, displacement, ierr) + if (ierr .ne. 0) stop 233 + call fem_set_node_displacement(7, displacement, ierr) + if (ierr .ne. 0) stop 234 + displacement = ZERO + call fem_set_node_displacement(8, displacement, ierr) + if (ierr .ne. 0) stop 235 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 236 + + internal_force_sum = ZERO + call fem_get_node_internal_force(1, internal_force, ierr) + if (ierr .ne. 0) stop 237 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) + sigma_x / 4.0_rk) .gt. tol * sigma_x) stop 238 + if (abs(internal_force(2) + sigma_lat / 4.0_rk) .gt. tol * sigma_lat) stop 239 + if (abs(internal_force(3) + sigma_lat / 4.0_rk) .gt. tol * sigma_lat) stop 240 + + call fem_get_node_internal_force(7, internal_force, ierr) + if (ierr .ne. 0) stop 241 + if (abs(internal_force(1) - sigma_x / 4.0_rk) .gt. tol * sigma_x) stop 242 + if (abs(internal_force(2) - sigma_lat / 4.0_rk) .gt. tol * sigma_lat) stop 243 + if (abs(internal_force(3) - sigma_lat / 4.0_rk) .gt. tol * sigma_lat) stop 244 + + call fem_get_node_internal_force(2, internal_force, ierr) + if (ierr .ne. 0) stop 245 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(3, internal_force, ierr) + if (ierr .ne. 0) stop 246 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(4, internal_force, ierr) + if (ierr .ne. 0) stop 247 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(5, internal_force, ierr) + if (ierr .ne. 0) stop 248 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(6, internal_force, ierr) + if (ierr .ne. 0) stop 249 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(7, internal_force, ierr) + if (ierr .ne. 0) stop 250 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(8, internal_force, ierr) + if (ierr .ne. 0) stop 251 + internal_force_sum = internal_force_sum + internal_force + if (maxval(abs(internal_force_sum)) .gt. tol * sigma_x) stop 252 + + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 253 + if (abs(strain_avg(1,1) - eps_test) .gt. tol * eps_test) stop 254 + if (abs(stress_avg(1,1) - sigma_x) .gt. tol * sigma_x) stop 255 + if (abs(stress_avg(2,1) - sigma_lat) .gt. tol * sigma_lat) stop 256 + if (abs(stress_avg(3,1) - sigma_lat) .gt. tol * sigma_lat) stop 257 + + call fem_read_inp('tests/data/c3d4_j2_ideal_shear.inp', ierr) + if (ierr .ne. 0) stop 258 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 259 + + gamma_test = 5.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 260 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 261 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 262 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 263 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 264 + + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 265 + + yield_test = 200.0_rk + mu_test = 2.10e5_rk / (2.0_rk * (ONE + 0.30_rk)) + tau_yield = yield_test / sqrt(3.0_rk) + eqps_expected = (sqrt(3.0_rk) * mu_test * gamma_test - yield_test) / & + (3.0_rk * mu_test) + plastic_shear_expected = gamma_test - tau_yield / mu_test + + if (abs(strain_avg(4,1) - gamma_test) .gt. tol * gamma_test) stop 266 + if (abs(stress_avg(4,1) - tau_yield) .gt. tol * yield_test) stop 267 + if (maxval(abs(stress_avg(1:3,1))) .gt. tol * yield_test) stop 268 + if (maxval(abs(stress_avg(5:6,1))) .gt. tol * yield_test) stop 269 + if (abs(eqps_avg(1) - eqps_expected) .gt. tol * eqps_expected) stop 270 + if (abs(plastic_avg(4,1) - plastic_shear_expected) .gt. & + tol * plastic_shear_expected) stop 271 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 272 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 273 + if (abs(eqps_avg(1) - eqps_expected) .gt. tol * eqps_expected) stop 274 + if (abs(plastic_avg(4,1) - plastic_shear_expected) .gt. & + tol * plastic_shear_expected) stop 275 + + call fem_read_inp('tests/data/c3d4_j2_linear_shear.inp', ierr) + if (ierr .ne. 0) stop 276 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 277 + + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 278 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 279 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 280 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 281 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 282 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 283 + + hardening_test = (400.0_rk - 200.0_rk) / 0.01_rk + seq_trial = sqrt(3.0_rk) * mu_test * gamma_test + eqps_expected = (seq_trial - yield_test) / (3.0_rk * mu_test + hardening_test) + seq_expected = yield_test + hardening_test * eqps_expected + tau_yield = seq_expected / sqrt(3.0_rk) + plastic_shear_expected = sqrt(3.0_rk) * eqps_expected + + if (abs(stress_avg(4,1) - tau_yield) .gt. tol * seq_expected) stop 284 + if (abs(eqps_avg(1) - eqps_expected) .gt. tol * eqps_expected) stop 285 + if (abs(plastic_avg(4,1) - plastic_shear_expected) .gt. & + tol * plastic_shear_expected) stop 286 + + call fem_read_inp('tests/data/c3d4_j2_multilinear_shear.inp', ierr) + if (ierr .ne. 0) stop 287 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 288 + + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 289 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 290 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 291 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 292 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 293 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 294 + + hardening_test = (400.0_rk - 250.0_rk) / (0.01_rk - 0.001_rk) + seq_trial = sqrt(3.0_rk) * mu_test * gamma_test + eqps_expected = (seq_trial - (250.0_rk - hardening_test * 0.001_rk)) / & + (3.0_rk * mu_test + hardening_test) + seq_expected = 250.0_rk + hardening_test * (eqps_expected - 0.001_rk) + tau_yield = seq_expected / sqrt(3.0_rk) + plastic_shear_expected = sqrt(3.0_rk) * eqps_expected + + if (eqps_expected .le. 0.001_rk) stop 295 + if (abs(stress_avg(4,1) - tau_yield) .gt. tol * seq_expected) stop 296 + if (abs(eqps_avg(1) - eqps_expected) .gt. tol * eqps_expected) stop 297 + if (abs(plastic_avg(4,1) - plastic_shear_expected) .gt. & + tol * plastic_shear_expected) stop 298 + + call fem_read_inp('tests/data/c3d4_j2_eqps_failure.inp', ierr) + if (ierr .ne. 0) stop 299 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 300 + + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 301 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 302 + displacement(1) = gamma_test + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 303 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 304 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 305 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 306 + call fem_get_element_status(1, query_elem_ids, status_query, ierr) + if (ierr .ne. 0) stop 307 + + if (status_query(1) .ne. 1) stop 308 + if (damage_avg(1) .lt. HALF) stop 309 + if (eqps_avg(1) .lt. 1.0e-4_rk) stop 310 + if (maxval(abs(stress_avg(:,1))) .gt. tol * yield_test) stop 311 + call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) + if (ierr .ne. 0) stop 312 + if (num_facets .ne. 0) stop 313 + if (max_fnodes .ne. 0) stop 314 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 315 + call fem_get_mass_summary(total_mass, rotational_inertia, ierr) + if (ierr .ne. 0) stop 316 + if (abs(total_mass) .gt. tol) stop 317 + if (abs(rotational_inertia) .gt. tol) stop 318 + + call fem_read_inp('examples/s4_one_element.inp', ierr) + if (ierr .ne. 0) stop 23 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 24 + + call fem_get_model_size(num_nodes, num_elems, num_dof, ierr) + if (ierr .ne. 0) stop 25 + if (num_nodes .ne. 4) stop 26 + if (num_elems .ne. 1) stop 27 + if (num_dof .ne. 24) stop 28 + call fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + if (ierr .ne. 0) stop 54 + if (num_bc .ne. 0) stop 55 + if (num_cload .ne. 0) stop 56 + if (num_dload .ne. 1) stop 57 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 76 + call fem_get_mass_summary(total_mass, rotational_inertia, ierr) + if (ierr .ne. 0) stop 77 + expected_mass = 7850.0_rk * 0.01_rk + expected_rot = 7850.0_rk * 0.01_rk**3 / 4.0_rk + if (abs(total_mass - expected_mass) .gt. tol * expected_mass) stop 78 + if (abs(rotational_inertia - expected_rot) .gt. tol * expected_rot) stop 79 + call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) + if (ierr .ne. 0) stop 150 + if (num_facets .ne. 1) stop 151 + if (max_fnodes .ne. 4) stop 152 + call fem_get_boundary_facets(TEST_MAX_FACETS, TEST_MAX_FACE_NODES, & + facet_ids, facet_elem_ids, facet_face_ids, & + facet_nnodes, facet_conn, x_face, v_face, & + normal_face, area_face, ierr) + if (ierr .ne. 0) stop 153 + if (facet_nnodes(1) .ne. 4) stop 154 + if (abs(area_face(1) - ONE) .gt. tol) stop 155 + if (abs(normal_face(3,1) - ONE) .gt. tol) stop 156 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 157 + load_facet_ids(1) = 1 + pressure_vals = ZERO + pressure_vals(1) = 3.0_rk + call fem_apply_sph_facet_pressure(1, load_facet_ids, pressure_vals, ierr) + if (ierr .ne. 0) stop 158 + call fem_get_sph_force_summary(total_force, ierr) + if (ierr .ne. 0) stop 159 + if (abs(total_force(1)) .gt. tol) stop 160 + if (abs(total_force(2)) .gt. tol) stop 161 + if (abs(total_force(3) + 3.0_rk) .gt. tol) stop 162 + call fem_clear_sph_forces(ierr) + if (ierr .ne. 0) stop 184 + dt_test = 1.0e-6_rk + call fem_advance_explicit(dt_test, ierr) + if (ierr .ne. 0) stop 185 + call fem_get_node_kinematics(1, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 186 + nodal_mass = expected_mass / 4.0_rk + if (abs(acceleration(1)) .gt. tol) stop 187 + if (abs(acceleration(2)) .gt. tol) stop 188 + if (abs(acceleration(3) + 250000.0_rk / nodal_mass) .gt. tol) stop 189 + + call fem_read_inp('examples/s4_one_element.inp', ierr) + if (ierr .ne. 0) stop 357 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 358 + + eps_test = 1.0e-3_rk + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 359 + displacement(1) = eps_test + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 360 + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 361 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 362 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 363 + + sigma_x = 2.10e11_rk * eps_test / (ONE - 0.30_rk * 0.30_rk) + sigma_lat = 0.30_rk * sigma_x + internal_force_sum = ZERO + call fem_get_node_internal_force(1, internal_force, ierr) + if (ierr .ne. 0) stop 364 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) + HALF * 0.01_rk * sigma_x) .gt. & + tol * sigma_x * 0.01_rk) stop 365 + if (abs(internal_force(2) + HALF * 0.01_rk * sigma_lat) .gt. & + tol * sigma_lat * 0.01_rk) stop 366 + + call fem_get_node_internal_force(2, internal_force, ierr) + if (ierr .ne. 0) stop 367 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) - HALF * 0.01_rk * sigma_x) .gt. & + tol * sigma_x * 0.01_rk) stop 368 + if (abs(internal_force(2) + HALF * 0.01_rk * sigma_lat) .gt. & + tol * sigma_lat * 0.01_rk) stop 369 + + call fem_get_node_internal_force(3, internal_force, ierr) + if (ierr .ne. 0) stop 370 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) - HALF * 0.01_rk * sigma_x) .gt. & + tol * sigma_x * 0.01_rk) stop 371 + if (abs(internal_force(2) - HALF * 0.01_rk * sigma_lat) .gt. & + tol * sigma_lat * 0.01_rk) stop 372 + + call fem_get_node_internal_force(4, internal_force, ierr) + if (ierr .ne. 0) stop 373 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force(1) + HALF * 0.01_rk * sigma_x) .gt. & + tol * sigma_x * 0.01_rk) stop 374 + if (abs(internal_force(2) - HALF * 0.01_rk * sigma_lat) .gt. & + tol * sigma_lat * 0.01_rk) stop 375 + if (maxval(abs(internal_force_sum)) .gt. tol * sigma_x * 0.01_rk) stop 376 + + query_elem_ids(1) = 1 + call fem_get_element_average_state(1, query_elem_ids, stress_avg, strain_avg, & + plastic_avg, eqps_avg, damage_avg, ierr) + if (ierr .ne. 0) stop 377 + if (abs(strain_avg(1,1) - eps_test) .gt. tol * eps_test) stop 378 + if (abs(strain_avg(2,1)) .gt. tol * eps_test) stop 379 + if (abs(stress_avg(1,1) - sigma_x) .gt. tol * sigma_x) stop 380 + if (abs(stress_avg(2,1) - sigma_lat) .gt. tol * sigma_lat) stop 381 + + displacement = ZERO + call fem_set_node_displacement(1, displacement, ierr) + if (ierr .ne. 0) stop 382 + call fem_set_node_displacement(2, displacement, ierr) + if (ierr .ne. 0) stop 383 + displacement(3) = 1.0e-5_rk + call fem_set_node_displacement(3, displacement, ierr) + if (ierr .ne. 0) stop 384 + displacement = ZERO + call fem_set_node_displacement(4, displacement, ierr) + if (ierr .ne. 0) stop 385 + + call fem_compute_internal_force(ierr) + if (ierr .ne. 0) stop 386 + internal_force_sum = ZERO + call fem_get_node_internal_force(3, force_vec, ierr) + if (ierr .ne. 0) stop 387 + if (force_vec(3) .le. ZERO) stop 388 + call fem_get_node_internal_force(1, internal_force, ierr) + if (ierr .ne. 0) stop 389 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(2, internal_force, ierr) + if (ierr .ne. 0) stop 390 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(3, internal_force, ierr) + if (ierr .ne. 0) stop 391 + internal_force_sum = internal_force_sum + internal_force + call fem_get_node_internal_force(4, internal_force, ierr) + if (ierr .ne. 0) stop 392 + internal_force_sum = internal_force_sum + internal_force + if (abs(internal_force_sum(3)) .gt. tol * force_vec(3)) stop 393 + + call fem_read_inp('examples/generate_sets.inp', ierr) + if (ierr .ne. 0) stop 34 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 35 + + call fem_get_model_counts(num_nodes, num_elems, num_dof, num_nsets, & + num_elsets, num_materials, num_sections, & + num_nset_items, num_elset_items, ierr) + if (ierr .ne. 0) stop 36 + if (num_nodes .ne. 4) stop 37 + if (num_elems .ne. 1) stop 38 + if (num_dof .ne. 12) stop 39 + if (num_nsets .ne. 1) stop 40 + if (num_elsets .ne. 2) stop 41 + if (num_materials .ne. 1) stop 42 + if (num_sections .ne. 1) stop 43 + if (num_nset_items .ne. 4) stop 44 + if (num_elset_items .ne. 2) stop 45 + call fem_get_load_counts(num_bc, num_cload, num_dload, ierr) + if (ierr .ne. 0) stop 58 + if (num_bc .ne. 0) stop 59 + if (num_cload .ne. 0) stop 60 + if (num_dload .ne. 0) stop 61 + + call fem_read_inp('tests/data/c3d8_two_element_shared_face.inp', ierr) + if (ierr .ne. 0) stop 163 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 164 + + call fem_get_model_size(num_nodes, num_elems, num_dof, ierr) + if (ierr .ne. 0) stop 165 + if (num_nodes .ne. 12) stop 166 + if (num_elems .ne. 2) stop 167 + if (num_dof .ne. 36) stop 168 + call fem_get_boundary_facet_count(num_facets, max_fnodes, ierr) + if (ierr .ne. 0) stop 169 + if (num_facets .ne. 10) stop 170 + if (max_fnodes .ne. 4) stop 171 + call fem_get_boundary_facets(TEST_MAX_FACETS, TEST_MAX_FACE_NODES, & + facet_ids, facet_elem_ids, facet_face_ids, & + facet_nnodes, facet_conn, x_face, v_face, & + normal_face, area_face, ierr) + if (ierr .ne. 0) stop 172 + if (abs(sum(area_face) - 10.0_rk) .gt. tol) stop 173 + + call fem_read_inp('tests/data/c3d8_elastic_wave_bar.inp', ierr) + if (ierr .ne. 0) stop 408 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 409 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 410 + + dt_test = 1.0e-4_rk + do wave_step = 1, 20 + call fem_advance_explicit(dt_test, ierr) + if (ierr .ne. 0) stop 411 + end do + + call fem_get_node_kinematics(9, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 412 + if (displacement(1) .le. ZERO) stop 413 + call fem_get_node_kinematics(2, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 414 + if (displacement(1) .le. ZERO) stop 415 + call fem_get_node_kinematics(1, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 416 + if (maxval(abs(displacement)) .gt. tol) stop 417 + + call fem_read_inp('tests/data/c3d4_elastic_wave_pair.inp', ierr) + if (ierr .ne. 0) stop 418 + + call fem_build_model(ierr) + if (ierr .ne. 0) stop 419 + call fem_compute_lumped_mass(ierr) + if (ierr .ne. 0) stop 420 + + dt_test = 1.0e-4_rk + do wave_step = 1, 20 + call fem_advance_explicit(dt_test, ierr) + if (ierr .ne. 0) stop 421 + end do + + call fem_get_node_kinematics(5, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 422 + if (displacement(1) .le. ZERO) stop 423 + call fem_get_node_kinematics(3, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 424 + if (displacement(1) .le. ZERO) stop 425 + call fem_get_node_kinematics(1, displacement, velocity, acceleration, ierr) + if (ierr .ne. 0) stop 426 + if (maxval(abs(displacement)) .gt. tol) stop 427 + + call fem_read_inp('examples/c3d8_one_element.inp', ierr) + if (ierr .ne. 0) stop 326 + + call fem_set_output_directory('build-wsl-debug', ierr) + if (ierr .ne. 0) stop 332 + call fem_set_output_interval(25, ierr) + if (ierr .ne. 0) stop 333 + call fem_build_model(ierr) + if (ierr .ne. 0) stop 327 + call fem_run_explicit(ierr) + if (ierr .ne. 0) stop 328 + call fem_get_current_time(time, step, ierr) + if (ierr .ne. 0) stop 329 + if (step .ne. 100) stop 330 + if (abs(time - 1.0e-5_rk) .gt. tol * 1.0e-5_rk) stop 331 + inquire(file='build-wsl-debug/history.csv', exist=history_exists) + if (.not. history_exists) stop 334 + inquire(file='build-wsl-debug/nodes_00000100.csv', exist=nodes_exists) + if (.not. nodes_exists) stop 394 + inquire(file='build-wsl-debug/elements_00000100.csv', exist=elements_exists) + if (.not. elements_exists) stop 395 + + call fem_read_inp('tests/data/invalid_missing_density.inp', ierr) + if (ierr .ne. 0) stop 62 + call fem_build_model(ierr) + if (ierr .eq. 0) stop 63 + + call fem_read_inp('tests/data/invalid_boundary_dof.inp', ierr) + if (ierr .ne. 0) stop 64 + call fem_build_model(ierr) + if (ierr .eq. 0) stop 65 + + call fem_read_inp('tests/data/invalid_unknown_cload_target.inp', ierr) + if (ierr .ne. 0) stop 66 + call fem_build_model(ierr) + if (ierr .eq. 0) stop 67 + + call fem_finalize(ierr) + if (ierr .ne. 0) stop 10 +end program test_smoke