FEMM/ffi/femm_elec.cpp

123 lines
3.9 KiB
C++

// electrostatic FFI body: opaque doc, pipeline, mesh accessors. delegates to belasolv's CFemmeDocCore.
#include "../belasolv/StdAfx.h"
#include "../belasolv/belasolvDlg.h"
#include "../belasolv/mesh.h"
#include "../belasolv/spars.h"
#include "../belasolv/femmedoccore.h"
#include "femm_elec.h"
#include <cstdio>
#include <cstdarg>
#include <string>
// stub view: math files reference TheView globally, math methods call SetPos/SetDlgItemText/InvalidateRect on it.
static CbelasolvDlg s_stub_view;
CbelasolvDlg* TheView = &s_stub_view;
// math files declare and call these for error reporting.
int MsgBox(const char* fmt, ...) {
va_list ap; va_start(ap, fmt);
std::vfprintf(stderr, fmt, ap);
std::fputc('\n', stderr);
va_end(ap);
return 0;
}
int MsgBox(const std::string& s) {
std::fprintf(stderr, "%s\n", s.c_str());
return 0;
}
// referenced by belasolv/main.cpp's old_main wait loop; main.cpp itself is not linked here.
inline bool IsWindow(void*) { return true; }
struct FemmElecDoc {
CFemmeDocCore doc;
FemmElecProgressFn cb = nullptr;
void* user = nullptr;
std::string path_buf;
};
extern "C" {
FemmElecDoc* femm_elec_doc_new(void) {
auto* d = new FemmElecDoc();
d->doc.TheView = &s_stub_view;
return d;
}
void femm_elec_doc_free(FemmElecDoc* d) {
delete d;
}
void femm_elec_doc_set_progress(FemmElecDoc* d, FemmElecProgressFn fn, void* user) {
if (!d) return;
d->cb = fn;
d->user = user;
}
int femm_elec_doc_load_fee(FemmElecDoc* d, const char* path) {
if (!d || !path) return 0;
d->path_buf = path;
d->doc.PathName = const_cast<char*>(d->path_buf.c_str());
return d->doc.OnOpenDocument() ? 1 : 0;
}
int femm_elec_doc_load_mesh(FemmElecDoc* d) {
return (d && d->doc.LoadMesh()) ? 1 : 0;
}
int femm_elec_doc_renumber(FemmElecDoc* d) {
return (d && d->doc.Cuthill()) ? 1 : 0;
}
// allocates a real-valued CBigLinProb and runs AnalyzeProblem + WriteResults.
int femm_elec_doc_solve(FemmElecDoc* d) {
if (!d) return 0;
CBigLinProb L;
L.TheView = &s_stub_view;
L.Precision = d->doc.Precision;
if (!L.Create(d->doc.NumNodes + d->doc.NumCircProps, d->doc.BandWidth)) return 0;
if (!d->doc.AnalyzeProblem(L)) return 0;
if (!d->doc.WriteResults(L)) return 0;
return 1;
}
int femm_elec_doc_write_results(FemmElecDoc* /*d*/, const char* /*out_path*/) {
// results currently emitted inline by solve(); reserved hook for explicit output redirection.
return 1;
}
int femm_elec_doc_axisymmetric (const FemmElecDoc* d) { return (d && d->doc.ProblemType) ? 1 : 0; }
double femm_elec_doc_depth (const FemmElecDoc* d) { return d ? d->doc.Depth : 0.0; }
double femm_elec_doc_precision (const FemmElecDoc* d) { return d ? d->doc.Precision : 0.0; }
int femm_elec_doc_num_nodes (const FemmElecDoc* d) { return d ? d->doc.NumNodes : 0; }
void femm_elec_doc_node (const FemmElecDoc* d, int i, double* x, double* y) {
if (!d || !d->doc.meshnode) return;
if (x) *x = d->doc.meshnode[i].x;
if (y) *y = d->doc.meshnode[i].y;
}
int femm_elec_doc_num_elements (const FemmElecDoc* d) { return d ? d->doc.NumEls : 0; }
void femm_elec_doc_element (const FemmElecDoc* d, int i, int* p0, int* p1, int* p2) {
if (!d || !d->doc.meshele) return;
if (p0) *p0 = d->doc.meshele[i].p[0];
if (p1) *p1 = d->doc.meshele[i].p[1];
if (p2) *p2 = d->doc.meshele[i].p[2];
}
int femm_elec_doc_num_materials (const FemmElecDoc* d) { return d ? d->doc.NumBlockProps : 0; }
int femm_elec_doc_num_boundaries (const FemmElecDoc* d) { return d ? d->doc.NumLineProps : 0; }
int femm_elec_doc_num_conductors (const FemmElecDoc* d) { return d ? d->doc.NumCircProps : 0; }
// field sampling is post-processor territory; not exposed by the solver alone.
double femm_elec_doc_field_at (const FemmElecDoc* /*d*/, double /*x*/, double /*y*/, FemmElecField /*c*/) {
return 0.0;
}
} // extern "C"