// 示例 CAE 求解器插件(VDE 微内核 P2 / 插件 ABI 1.2;纯 C ABI,不链接内核)。 // // 演示 VdeCaeSolverPlugin 扩展点:输入 VDE 序列化模型 + 参数 JSON,输出结果 JSON。 // 求解是"演示级估算"(线弹性轴向刚度/变形/应力),不代表真实 FEA 精度; // 目的是验证扩展点契约、宿主服务使用(body_from_serialized/body_query/body_bbox) // 与跨 ABI 内存所有权(host->alloc 输出、宿 free_buffer 释放)。 #include #include #include #include #include "vde/plugin/vde_plugin_api.h" namespace { int cae_fail(const VdePluginHost* host, int code, const char* message) { if (host && host->set_error) host->set_error(message); return code; } int validate_host(const VdePluginHost* host) { if (!host) return VDE_PLUGIN_ERR_INVALID_ARG; if (host->abi_major != VDE_PLUGIN_ABI_MAJOR) return cae_fail(host, VDE_PLUGIN_ERR_ABI, "sample_cae: 宿主 ABI major 不兼容"); if (!host->alloc || !host->free || !host->set_error) return cae_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "sample_cae: 宿主缺少 alloc/free/set_error"); if (!host->body_from_serialized || !host->body_query || !host->body_bbox || !host->body_destroy) return cae_fail(host, VDE_PLUGIN_ERR_UNSUPPORTED, "sample_cae: 宿主缺少 body_from_serialized/query/bbox/destroy"); return VDE_PLUGIN_OK; } /** 极简 JSON 数值字段提取:查找 "key" 后的 ':' 再 strtod;失败返回 fallback。 */ double json_number(const char* text, const char* key, double fallback) { if (!text || !key) return fallback; const std::string pattern = std::string("\"") + key + "\""; const char* pos = std::strstr(text, pattern.c_str()); if (!pos) return fallback; pos += pattern.size(); while (*pos == ' ' || *pos == '\t' || *pos == '\r' || *pos == '\n') ++pos; if (*pos != ':') return fallback; ++pos; char* end = nullptr; const double value = std::strtod(pos, &end); if (end == pos) return fallback; return value; } int cae_solve(const VdePluginHost* host, const void* model_data, size_t model_size, const char* params_json, VdeBuffer* result_json) { if (result_json) { result_json->data = nullptr; result_json->size = 0; } if (!result_json) return cae_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "sample_cae: result 为空"); const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; if (!model_data || model_size == 0) return cae_fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "sample_cae: 模型数据为空"); VdeBody body = host->body_from_serialized(model_data, model_size); if (!body) return cae_fail(host, VDE_PLUGIN_ERR_PARSE, "sample_cae: 无法反序列化模型(见宿主错误信息)"); VdeBodyInfo info{}; info.struct_size = static_cast(sizeof(VdeBodyInfo)); int rc = host->body_query(body, &info); if (rc != VDE_PLUGIN_OK) { host->body_destroy(body); return cae_fail(host, VDE_PLUGIN_ERR_INTERNAL, "sample_cae: body_query 失败"); } double bmin[3] = {0.0, 0.0, 0.0}; double bmax[3] = {0.0, 0.0, 0.0}; rc = host->body_bbox(body, bmin, bmax); if (rc != VDE_PLUGIN_OK) { host->body_destroy(body); return cae_fail(host, VDE_PLUGIN_ERR_INTERNAL, "sample_cae: body_bbox 失败"); } host->body_destroy(body); // 演示级线弹性估算:轴向刚度 k = E*A/L,变形 δ = F/k,应力 σ = F/A。 const double youngs = json_number(params_json, "youngs_modulus", 210.0e9); const double load = json_number(params_json, "load", 1000.0); const double extent_x = bmax[0] - bmin[0]; const double extent_y = bmax[1] - bmin[1]; const double length = bmax[2] - bmin[2]; const double area = (extent_x > 0.0 && extent_y > 0.0) ? extent_x * extent_y : 1.0; const double l = (length > 1e-12) ? length : 1.0; const double stiffness = (youngs > 0.0) ? youngs * area / l : 0.0; const double deformation = (stiffness > 1e-30) ? load / stiffness : 0.0; const double stress = load / area; char text[1024]; const int written = std::snprintf( text, sizeof(text), "{\"solver_id\":\"fea.linear_static\",\"ok\":true," "\"volume\":%.10g,\"bbox\":[[%.10g,%.10g,%.10g],[%.10g,%.10g,%.10g]]," "\"results\":{\"axial_stiffness\":%.10g,\"axial_deformation\":%.10g," "\"axial_stress\":%.10g}," "\"params\":{\"youngs_modulus\":%.10g,\"load\":%.10g}," "\"note\":\"demo-only estimate; not a validated FEA solver\"}", info.volume, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2], stiffness, deformation, stress, youngs, load); if (written <= 0) return cae_fail(host, VDE_PLUGIN_ERR_INTERNAL, "sample_cae: 结果 JSON 生成失败"); const std::size_t size = static_cast(written); void* buffer = host->alloc(size + 1); if (!buffer) return cae_fail(host, VDE_PLUGIN_ERR_BUFFER, "sample_cae: 结果缓冲分配失败"); std::memcpy(buffer, text, size + 1); result_json->data = buffer; result_json->size = size; return VDE_PLUGIN_OK; } const char* cae_describe() { return "VDE sample CAE solver: demo linear-static axial estimate (ABI 1.2)"; } const VdeCaeSolverPlugin kSolver = [] { VdeCaeSolverPlugin solver{}; solver.struct_size = static_cast(sizeof(VdeCaeSolverPlugin)); solver.solver_id = "fea.linear_static"; solver.capabilities_json = "{\"fields\":[\"displacement\",\"stress\"],\"element_types\":[\"tet4\",\"hex8\"]," "\"linear\":true,\"demo\":true}"; solver.solve = &cae_solve; solver.describe = &cae_describe; return solver; }(); } // namespace extern "C" { VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) { if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI; if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI; info->id = "com.vde.sample.cae"; info->name = "VDE Sample CAE Linear Static Solver"; info->version = "0.1.0"; info->kind = VDE_PLUGIN_KIND_CAE_SOLVER; info->abi_major = VDE_PLUGIN_ABI_MAJOR; info->abi_minor = VDE_PLUGIN_ABI_MINOR; info->capabilities = "cae_solver:fea.linear_static"; info->license = "Apache-2.0"; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) { if (!reg || reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI; const int host_rc = validate_host(host); if (host_rc != VDE_PLUGIN_OK) return host_rc; reg->kind = VDE_PLUGIN_KIND_CAE_SOLVER; reg->format = nullptr; reg->cae_solver = &kSolver; reg->cam_strategy = nullptr; return VDE_PLUGIN_OK; } VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {} } // extern "C"