Add coupling output restart and APIs

This commit is contained in:
Codex
2026-07-04 09:59:52 +08:00
parent 5be6b178bc
commit 5d9933d014
22 changed files with 2636 additions and 89 deletions
+133
View File
@@ -0,0 +1,133 @@
#include "sphlite_c_api.h"
#include <math.h>
#include <stdio.h>
static int fail(const char *message)
{
fprintf(stderr, "%s\n", message);
return 1;
}
int main(void)
{
char path[1024];
char name[128];
int ierr;
int region_count = 0;
int region_type = 0;
int export_mode = 0;
int import_mode = 0;
int enabled = 0;
int particle_count = 0;
int facet_count = 0;
int ext_ids[3] = {0, 0, 0};
int internal_indices[3] = {0, 0, 0};
int phases[3] = {0, 0, 0};
int statuses[3] = {0, 0, 0};
int materials[3] = {0, 0, 0};
int sections[3] = {0, 0, 0};
int facet_ids[3] = {0, 0, 0};
int facet_particles[3] = {0, 0, 0};
int indices[1] = {1};
int facets[1] = {1};
double particle_scalars[SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT * 3] = {0.0};
double particle_vectors[3 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT * 3] = {0.0};
double facet_vectors[3 * SPHLITE_COUPLING_FACET_VECTOR_COUNT * 3] = {0.0};
double facet_areas[3] = {0.0, 0.0, 0.0};
double forces[3] = {2.0, 0.0, 0.0};
double loads[3] = {0.0, 3.0, 0.0};
double target[3] = {0.0, 0.0, 1.0};
double diagnostics[SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT] = {0.0};
int counts[SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT] = {0};
if (sphlite_coupling_get_region_count(NULL) != -1) {
return fail("C coupling null region count did not return -1");
}
if (sphlite_coupling_get_region_info(1, NULL, (int)sizeof(name),
&region_type, &export_mode, &import_mode, &enabled, &particle_count,
&facet_count) != -1) {
return fail("C coupling null region name did not return -1");
}
if (sphlite_coupling_get_region_info(1, name, 1, &region_type,
&export_mode, &import_mode, &enabled, &particle_count,
&facet_count) != -1) {
return fail("C coupling small name capacity did not return -1");
}
if (sphlite_coupling_export_particles(1, 3, &particle_count, NULL,
internal_indices, phases, statuses, materials, sections,
particle_scalars, particle_vectors) != -1) {
return fail("C coupling null particle export buffer did not return -1");
}
if (sphlite_coupling_get_diagnostics(diagnostics,
SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT - 1, counts,
SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT) != -1) {
return fail("C coupling small diagnostics buffer did not return -1");
}
forces[0] = HUGE_VAL;
if (sphlite_coupling_apply_particle_forces(1, 1, indices, forces) != -1) {
return fail("C coupling nonfinite force did not return -1");
}
forces[0] = 2.0;
ierr = sphlite_initialize();
if (ierr != 0) return fail("C coupling initialize failed");
snprintf(path, sizeof(path), "%s/coupling_shell_region_080.inp",
SPHLITE_TEST_DATA_DIR);
ierr = sphlite_read_inp(path);
if (ierr != 0) return fail("C coupling read input failed");
ierr = sphlite_build_model();
if (ierr != 0) return fail("C coupling build model failed");
ierr = sphlite_coupling_get_region_count(&region_count);
if (ierr != 0 || region_count != 2) return fail("C coupling region count mismatch");
ierr = sphlite_coupling_get_region_info(2, name, (int)sizeof(name),
&region_type, &export_mode, &import_mode, &enabled, &particle_count,
&facet_count);
if (ierr != 0) return fail("C coupling region info failed");
if (export_mode != SPHLITE_COUPLING_EXPORT_BOTH ||
import_mode != SPHLITE_COUPLING_IMPORT_FORCE ||
enabled != 1 || particle_count != 3 || facet_count != 3) {
return fail("C coupling region info content mismatch");
}
ierr = sphlite_coupling_get_region_info(99, name, (int)sizeof(name),
&region_type, &export_mode, &import_mode, &enabled, &particle_count,
&facet_count);
if (ierr == 0) return fail("C coupling illegal region did not fail");
ierr = sphlite_coupling_export_particles(2, 3, &particle_count, ext_ids,
internal_indices, phases, statuses, materials, sections,
particle_scalars, particle_vectors);
if (ierr != 0 || particle_count != 3 || ext_ids[0] != 1) {
return fail("C coupling particle export mismatch");
}
ierr = sphlite_coupling_export_particles(2, 2, &particle_count, ext_ids,
internal_indices, phases, statuses, materials, sections,
particle_scalars, particle_vectors);
if (ierr == 0) return fail("C coupling small particle export did not fail");
ierr = sphlite_coupling_export_facets(1, 3, &facet_count, facet_ids,
facet_particles, materials, sections, facet_vectors, facet_areas);
if (ierr != 0 || facet_count != 3 || facet_ids[0] != 1 ||
facet_areas[0] <= 0.0) {
return fail("C coupling facet export mismatch");
}
ierr = sphlite_coupling_apply_particle_forces(2, 1, indices, forces);
if (ierr != 0) return fail("C coupling particle force import failed");
ierr = sphlite_coupling_apply_facet_loads(1, 1, facets, loads);
if (ierr != 0) return fail("C coupling facet load import failed");
ierr = sphlite_coupling_apply_motion_boundary(2, 2,
SPHLITE_COUPLING_MOTION_VELOCITY, SPHLITE_COUPLING_MOTION_DIRECT,
target);
if (ierr != 0) return fail("C coupling motion import failed");
ierr = sphlite_coupling_get_diagnostics(diagnostics,
SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT, counts,
SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT);
if (ierr != 0 || counts[3] != 2) return fail("C coupling diagnostics mismatch");
ierr = sphlite_coupling_clear_imports();
if (ierr != 0) return fail("C coupling clear imports failed");
ierr = sphlite_finalize();
if (ierr != 0) return fail("C coupling finalize failed");
return 0;
}
@@ -0,0 +1,82 @@
#include "sphlite.hpp"
#include <string>
#include <vector>
int main()
{
if (SPHLITE_COUPLING_EXPORT_BOTH != 3 ||
SPHLITE_COUPLING_IMPORT_FORCE != 1) {
return 1;
}
sphlite::Session session;
std::string input =
std::string(SPHLITE_TEST_DATA_DIR) + "/coupling_shell_region_080.inp";
sphlite::read_inp(input);
sphlite::build_model();
if (sphlite::coupling_region_count() != 2) {
return 1;
}
sphlite::CouplingRegionInfo info = sphlite::coupling_region_info(2);
if (info.name != "SHELL_BOTH" ||
info.export_mode != SPHLITE_COUPLING_EXPORT_BOTH ||
info.import_mode != SPHLITE_COUPLING_IMPORT_FORCE ||
info.particle_count != 3 || info.facet_count != 3) {
return 1;
}
try {
(void)sphlite::coupling_region_info(99);
return 1;
} catch (const sphlite::Error &) {
}
sphlite::CouplingParticleExport particles = sphlite::coupling_export_particles(2);
if (particles.ext_ids.size() != 3 || particles.ext_ids[0] != 1 ||
particles.scalar_values.size() != 3 * SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT ||
particles.vector_values.size() != 9 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT) {
return 1;
}
sphlite::CouplingFacetExport facets = sphlite::coupling_export_facets(1);
if (facets.facet_ids.size() != 3 || facets.facet_ids[0] != 1 ||
facets.area_values[0] <= 0.0) {
return 1;
}
std::vector<int> indices(1, 1);
std::vector<double> forces;
forces.push_back(2.0);
forces.push_back(0.0);
forces.push_back(0.0);
sphlite::coupling_apply_particle_forces(2, indices, forces);
std::vector<int> facet_ids(1, 1);
std::vector<double> loads;
loads.push_back(0.0);
loads.push_back(3.0);
loads.push_back(0.0);
sphlite::coupling_apply_facet_loads(1, facet_ids, loads);
double target[3] = {0.0, 0.0, 1.0};
sphlite::coupling_apply_motion_boundary(2, 2,
SPHLITE_COUPLING_MOTION_VELOCITY, SPHLITE_COUPLING_MOTION_DIRECT,
target);
sphlite::CouplingDiagnostics diagnostics = sphlite::coupling_diagnostics();
if (diagnostics.values.size() != SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT ||
diagnostics.counts.size() != SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT ||
diagnostics.counts[3] != 2) {
return 1;
}
try {
std::vector<double> bad_forces(2, 0.0);
sphlite::coupling_apply_particle_forces(2, indices, bad_forces);
return 1;
} catch (const sphlite::Error &) {
}
sphlite::coupling_clear_imports();
diagnostics = sphlite::coupling_diagnostics();
if (diagnostics.counts[0] < 1) {
return 1;
}
session.close();
return 0;
}