Files
FemLite/tests/interface/test_c_api.c
T
shenw 62f1293a9f
FemLite CI / build-test (push) Has been cancelled
Complete V4.5 convergence diagnostics
2026-06-25 09:33:20 +08:00

228 lines
9.1 KiB
C

#include "femlite_c_api.h"
#include <math.h>
#include <stdio.h>
static int check_ierr(int ierr, int code)
{
int err_code = 0;
char message[256];
if (ierr == 0) {
return 0;
}
(void)femlite_get_last_error(&err_code, message, (int)sizeof(message));
fprintf(stderr, "test_c_api failed at %d: ierr=%d code=%d %s\n",
code, ierr, err_code, message);
return code;
}
int main(void)
{
int ierr = 0;
int major = -1;
int minor = -1;
int patch = -1;
int nnode = 0;
int nelem = 0;
int ndof = 0;
int nfacet = 0;
int max_face_nodes = 0;
int step = 0;
int active_contact = -1;
int active_pair_count = -1;
int activated_pair_count = -1;
int maintained_pair_count = -1;
int released_pair_count = -1;
int contact_search_facets = -1;
int contact_search_masters = -1;
int contact_search_broad = -1;
int contact_search_active = -1;
int contact_search_bvh_nodes = -1;
int contact_cache_used = -1;
int contact_cache_overflow = -1;
int conv_increment = -1;
int conv_iteration = -1;
int conv_converged = -1;
int conv_cutback_count = -1;
int conv_status_code = -999;
int conv_failure_code = -999;
int node_ids[1] = {2};
int facet_id[8];
int elem_id[8];
int face_id[8];
int facet_nnode[8];
int facet_conn[8 * 8];
double x[3 * 8 * 8];
double vnod[3 * 8 * 8];
double normal[3 * 8];
double area[8];
double force[3] = {5.0, 1.0, -2.0};
double disp[3] = {0.0, 0.0, 0.0};
double vel[3] = {0.0, 0.0, 0.0};
double acc[3] = {0.0, 0.0, 0.0};
double dtcrit = 0.0;
double time = 0.0;
double contact_work_step = -1.0;
double contact_work_total = -1.0;
double contact_max_penetration = -1.0;
double contact_force_norm = -1.0;
double contact_gap_residual = -1.0;
double contact_aug_lambda = -1.0;
double contact_friction_work_step = -1.0;
double contact_friction_work_total = -1.0;
double contact_max_slip = -1.0;
int contact_history_slots = -1;
double max_slot_slip = -1.0;
double max_slot_work = -1.0;
double max_tangent_force = -1.0;
double contact_search_time = -1.0;
double contact_force_time = -1.0;
double contact_tangent_time = -1.0;
double conv_residual_norm = -1.0;
double conv_correction_norm = -1.0;
double conv_last_dt = -1.0;
double conv_last_accepted_time = -1.0;
double conv_relative_residual = -1.0;
double conv_contact_gap = -1.0;
double conv_combined_norm = -1.0;
ierr = femlite_initialize();
if (ierr != 0) return check_ierr(ierr, 1);
ierr = femlite_get_version(&major, &minor, &patch);
if (ierr != 0) return check_ierr(ierr, 2);
if (major != 0 || minor != 4 || patch != 3) return 3;
ierr = femlite_read_inp("examples/c3d4_one_element.inp");
if (ierr != 0) return check_ierr(ierr, 4);
ierr = femlite_build_model();
if (ierr != 0) return check_ierr(ierr, 5);
ierr = femlite_get_model_size(&nnode, &nelem, &ndof);
if (ierr != 0) return check_ierr(ierr, 6);
if (nnode != 4 || nelem != 1 || ndof != 12) return 7;
ierr = femlite_get_convergence_status(&conv_increment, &conv_iteration,
&conv_residual_norm,
&conv_correction_norm,
&conv_converged);
if (ierr != 0) return check_ierr(ierr, 38);
if (conv_increment != 0 || conv_iteration != 0 ||
conv_residual_norm != 0.0 || conv_correction_norm != 0.0 ||
conv_converged != 0) return 39;
ierr = femlite_get_convergence_diagnostics(&conv_cutback_count,
&conv_last_dt,
&conv_last_accepted_time,
&conv_status_code,
&conv_failure_code);
if (ierr != 0) return check_ierr(ierr, 40);
if (conv_cutback_count != 0 || conv_last_dt != 0.0 ||
conv_last_accepted_time != 0.0 ||
conv_status_code != 0 || conv_failure_code != 0) return 41;
ierr = femlite_get_convergence_metrics(&conv_relative_residual,
&conv_contact_gap,
&conv_combined_norm);
if (ierr != 0) return check_ierr(ierr, 42);
if (conv_relative_residual != 0.0 || conv_contact_gap != 0.0 ||
conv_combined_norm != 0.0) return 43;
ierr = femlite_get_contact_summary(&active_contact, &contact_work_step,
&contact_work_total,
&contact_max_penetration,
&contact_force_norm);
if (ierr != 0) return check_ierr(ierr, 24);
if (active_contact != 0 || contact_work_step != 0.0 ||
contact_work_total != 0.0 || contact_max_penetration != 0.0 ||
contact_force_norm != 0.0) return 25;
ierr = femlite_get_contact_diagnostics(&active_pair_count,
&activated_pair_count,
&maintained_pair_count,
&released_pair_count,
&contact_gap_residual,
&contact_aug_lambda);
if (ierr != 0) return check_ierr(ierr, 26);
if (active_pair_count != 0 || activated_pair_count != 0 ||
maintained_pair_count != 0 || released_pair_count != 0 ||
contact_gap_residual != 0.0 || contact_aug_lambda != 0.0) return 27;
ierr = femlite_get_contact_friction_summary(&contact_friction_work_step,
&contact_friction_work_total,
&contact_max_slip);
if (ierr != 0) return check_ierr(ierr, 28);
if (contact_friction_work_step != 0.0 ||
contact_friction_work_total != 0.0 ||
contact_max_slip != 0.0) return 29;
ierr = femlite_get_contact_history_stats(&contact_history_slots,
&max_slot_slip,
&max_slot_work,
&max_tangent_force);
if (ierr != 0) return check_ierr(ierr, 30);
if (contact_history_slots != 0 || max_slot_slip != 0.0 ||
max_slot_work != 0.0 || max_tangent_force != 0.0) return 31;
ierr = femlite_get_contact_search_stats(&contact_search_facets,
&contact_search_masters,
&contact_search_broad,
&contact_search_active,
&contact_search_bvh_nodes);
if (ierr != 0) return check_ierr(ierr, 32);
if (contact_search_facets != 0 || contact_search_masters != 0 ||
contact_search_broad != 0 || contact_search_active != 0 ||
contact_search_bvh_nodes != 0) return 33;
ierr = femlite_get_contact_perf_stats(&contact_search_time,
&contact_force_time,
&contact_tangent_time);
if (ierr != 0) return check_ierr(ierr, 34);
if (contact_search_time != 0.0 || contact_force_time != 0.0 ||
contact_tangent_time != 0.0) return 35;
ierr = femlite_get_contact_cache_stats(&contact_cache_used,
&contact_cache_overflow);
if (ierr != 0) return check_ierr(ierr, 36);
if (contact_cache_used != 0 || contact_cache_overflow != 0) return 37;
ierr = femlite_compute_lumped_mass();
if (ierr != 0) return check_ierr(ierr, 8);
ierr = femlite_estimate_stable_dt(&dtcrit);
if (ierr != 0) return check_ierr(ierr, 9);
if (dtcrit <= 0.0) return 10;
ierr = femlite_get_boundary_facet_count(&nfacet, &max_face_nodes);
if (ierr != 0) return check_ierr(ierr, 11);
if (nfacet != 4 || max_face_nodes != 3) return 12;
ierr = femlite_get_boundary_facets(8, 8, facet_id, elem_id, face_id,
facet_nnode, facet_conn, x, vnod,
normal, area);
if (ierr != 0) return check_ierr(ierr, 13);
if (facet_id[0] != 1 || elem_id[0] != 1 || facet_nnode[0] != 3) return 14;
ierr = femlite_clear_sph_forces();
if (ierr != 0) return check_ierr(ierr, 15);
ierr = femlite_apply_sph_nodal_forces(1, node_ids, force);
if (ierr != 0) return check_ierr(ierr, 16);
ierr = femlite_advance_explicit(1.0e-8);
if (ierr != 0) return check_ierr(ierr, 17);
ierr = femlite_get_current_time(&time, &step);
if (ierr != 0) return check_ierr(ierr, 18);
if (step != 1 || fabs(time - 1.0e-8) > 1.0e-16) return 19;
ierr = femlite_get_node_kinematics(2, disp, vel, acc);
if (ierr != 0) return check_ierr(ierr, 20);
if (acc[0] <= 0.0 || vel[0] <= 0.0 || disp[0] <= 0.0) return 21;
ierr = femlite_write_restart("build-wsl-debug/c_api_restart.frs");
if (ierr != 0) return check_ierr(ierr, 22);
ierr = femlite_finalize();
if (ierr != 0) return check_ierr(ierr, 23);
return 0;
}