Desktop app for designing parametric 3D models in CadQuery together with the coding agent of your choice
TypeScript 3d-modeling3d-printing3d-printing-toolsagentagentic-aiai-cadcadcadqueryclaude-codecodexelectronfemllmmacdmakeropencodeparametric-designpythonsteptext-to-cad
soma: FEM mit CalculiX und Kontakt zwischen Teilen (E84)
Kai-Jungsthoefel7ec763974f5d
diff --git a/prototype/phase0/setup_env.ps1 b/prototype/phase0/setup_env.ps1index d56156f..bde7645 100644--- a/prototype/phase0/setup_env.ps1+++ b/prototype/phase0/setup_env.ps1@@ -5,5 +5,7 @@ $root = "$env:LOCALAPPDATA\cadide-phase0"New-Item -ItemType Directory -Force $root | Out-Nullif (-not (Test-Path "$root\venv-full\Scripts\python.exe")) { uv venv --python 3.12 "$root\venv-full" }uv pip install --python "$root\venv-full\Scripts\python.exe" -r "$PSScriptRoot\requirements-phase0.txt"+# CalculiX fuer die FEM (E84): von conda-forge nach venv-full\ccx, nur wenn es noch fehlt+if (-not (Test-Path "$root\venv-full\ccx\ccx.exe")) { & "$root\venv-full\Scripts\python.exe" "$PSScriptRoot\..\..\python\scripts\fetch_ccx.py" }Push-Location $PSScriptRoot; npm install --no-audit --no-fund; Pop-LocationWrite-Host "Umgebung bereit: $root\venv-full"diff --git a/python/scripts/fetch_ccx.py b/python/scripts/fetch_ccx.pynew file mode 100644index 0000000..06400ae--- /dev/null+++ b/python/scripts/fetch_ccx.py@@ -0,0 +1,162 @@+"""Holt CalculiX (ccx) von conda-forge und legt es neben die Python-Umgebung (E84).++ python python/scripts/fetch_ccx.py # nach <sys.prefix>/ccx, fuer dieses System+ python python/scripts/fetch_ccx.py --platform osx-arm64 --target build/ccx-osx-arm64++Ablauf: micromamba (eine Datei) laden, damit eine Wegwerf-Umgebung mit calculix und OpenBLAS anlegen (ohne Vorgabe+nimmt conda unter Windows MKL, 680 MB statt 53 MB), dann nur das Noetige kopieren:+- Windows: ccx.exe und die DLLs, die es wirklich laedt (Importtabellen gelesen), flach in einen Ordner.+ Windows sucht DLLs zuerst neben der EXE.+- Linux, macOS: bin/ccx und lib/ wie in der Umgebung (ccx findet seine Bibliotheken relativ, $ORIGIN/../lib bzw.+ @loader_path/../lib). Ungetestet, bisher nur unter Windows ausprobiert.+Dazu die Lizenztexte aller Pakete nach licenses/ und die Paketliste nach PACKAGES.txt (fuer den Installer, E30).+"""+import argparse+import json+import os+import platform+import shutil+import struct+import subprocess+import sys+import tempfile+import urllib.request+from pathlib import Path++SPECS = ["calculix=2.23", "libblas=*=*openblas"]+++def host_platform():+ m = platform.machine().lower()+ if sys.platform == "win32":+ return "win-64"+ if sys.platform == "darwin":+ return "osx-arm64" if m in ("arm64", "aarch64") else "osx-64"+ return "linux-aarch64" if m in ("arm64", "aarch64") else "linux-64"+++def micromamba(cache):+ me = host_platform()+ exe = cache / ("micromamba.exe" if me == "win-64" else "micromamba")+ if not exe.is_file():+ url = f"https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-{me}"+ print(f"Loading micromamba ({url})")+ cache.mkdir(parents=True, exist_ok=True)+ urllib.request.urlretrieve(url, exe)+ exe.chmod(0o755)+ return exe+++def pe_imports(path):+ """Namen der importierten DLLs einer PE-Datei (EXE/DLL)."""+ data = Path(path).read_bytes()+ pe = struct.unpack_from("<I", data, 0x3C)[0]+ nsec = struct.unpack_from("<H", data, pe + 6)[0]+ opt = pe + 24+ magic = struct.unpack_from("<H", data, opt)[0]+ dirs = opt + (112 if magic == 0x20B else 96)+ imp_rva = struct.unpack_from("<I", data, dirs + 8)[0]+ sec = opt + struct.unpack_from("<H", data, pe + 20)[0]+ sections = []+ for i in range(nsec):+ va, raw_size, raw_ptr = struct.unpack_from("<III", data, sec + 40 * i + 12)+ vsize = struct.unpack_from("<I", data, sec + 40 * i + 8)[0]+ sections.append((va, max(vsize, raw_size), raw_ptr))++ def off(rva):+ for va, size, ptr in sections:+ if va <= rva < va + size:+ return rva - va + ptr+ raise ValueError("RVA outside sections")++ names = []+ if not imp_rva:+ return names+ d = off(imp_rva)+ while True:+ name_rva = struct.unpack_from("<I", data, d + 12)[0]+ if not name_rva:+ break+ o = off(name_rva)+ names.append(data[o:data.index(b"\0", o)].decode("ascii"))+ d += 20+ return names+++def copy_windows(env, target):+ dirs = [env / "Library" / "bin", env / "Library" / "mingw-w64" / "bin", env]+ where = {}+ for d in dirs:+ if d.is_dir():+ for f in d.iterdir():+ if f.suffix.lower() in (".dll", ".exe"):+ where.setdefault(f.name.lower(), f)+ todo, seen = ["ccx.exe"], set()+ while todo:+ name = todo.pop().lower()+ if name in seen or name not in where:+ continue # Systembibliotheken (KERNEL32, UCRT) bringt Windows mit+ seen.add(name)+ todo += pe_imports(where[name])+ # openblas laedt libblas/liblapack per Weiterleitung: openblas.dll mitnehmen, wenn eine davon dabei ist+ if name in ("libblas.dll", "liblapack.dll"):+ todo.append("openblas.dll")+ # api-ms-win-*.dll und ucrtbase gibt es ab Windows 10 im System+ keep = [n for n in seen if not n.startswith("api-ms-win-") and n != "ucrtbase.dll"]+ for n in sorted(keep):+ shutil.copy2(where[n], target / where[n].name)+ return sorted(where[n].name for n in keep)+++def copy_unix(env, target):+ (target / "bin").mkdir(parents=True, exist_ok=True)+ shutil.copy2(env / "bin" / "ccx", target / "bin" / "ccx")+ shutil.copytree(env / "lib", target / "lib", symlinks=True,+ ignore=shutil.ignore_patterns("*.a", "pkgconfig", "cmake", "python*", "*.la"))+ return ["bin/ccx", "lib/"]+++def main():+ ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])+ ap.add_argument("--platform", default=host_platform())+ ap.add_argument("--target", default=str(Path(sys.prefix) / "ccx"))+ args = ap.parse_args()+ target = Path(args.target)+ cache = Path(tempfile.gettempdir()) / "soma-fetch-ccx"+ mm = micromamba(cache)+ work = Path(tempfile.mkdtemp(prefix="soma-ccx-env-"))+ env = work / "env"+ try:+ cmd = [str(mm), "create", "-y", "-p", str(env), "--platform", args.platform, "-c", "conda-forge",+ "--override-channels", *SPECS]+ print("Creating environment:", " ".join(SPECS), f"({args.platform})")+ subprocess.run(cmd, check=True, env=dict(os.environ, MAMBA_ROOT_PREFIX=str(work / "root")))+ if target.exists():+ backup = target.with_name(target.name + ".old")+ shutil.rmtree(backup, ignore_errors=True)+ target.rename(backup) # nichts still ueberschreiben: der alte Stand bleibt als ccx.old+ print(f"Previous version moved to {backup}")+ target.mkdir(parents=True)+ files = copy_windows(env, target) if args.platform.startswith("win") else copy_unix(env, target)+ # Paketliste und Lizenzen+ pkgs, lic = [], target / "licenses"+ for meta in sorted((env / "conda-meta").glob("*.json")):+ m = json.loads(meta.read_text(encoding="utf-8"))+ pkgs.append(f"{m['name']} {m['version']} {m['build']} ({m.get('license', '?')})")+ src = Path(m.get("extracted_package_dir") or "") / "info" / "licenses"+ if src.is_dir():+ shutil.copytree(src, lic / m["name"], dirs_exist_ok=True)+ (target / "PACKAGES.txt").write_text("CalculiX for SOMA, from conda-forge\n\n" + "\n".join(pkgs) + "\n", encoding="utf-8")+ size = sum(f.stat().st_size for f in target.rglob("*") if f.is_file())+ print(f"{len(files)} files to {target} ({size / 2**20:.0f} MB with licenses)")+ if args.platform == host_platform():+ exe = target / ("ccx.exe" if args.platform.startswith("win") else "bin/ccx")+ p = subprocess.run([str(exe), "-v"], capture_output=True, text=True, errors="replace",+ env=dict(os.environ, PATH=str(exe.parent) + os.pathsep + os.environ.get("PATH", "")))+ print("Check:", (p.stdout + p.stderr).strip().splitlines()[-1] if (p.stdout + p.stderr).strip() else "no output")+ finally:+ shutil.rmtree(work, ignore_errors=True)+++if __name__ == "__main__":+ main()diff --git a/python/soma/ccx.py b/python/soma/ccx.pynew file mode 100644index 0000000..24f6a26--- /dev/null+++ b/python/soma/ccx.py@@ -0,0 +1,333 @@+"""CalculiX (ccx) als Rechenwerk der FEM (E84).++`soma.fem` vernetzt, baut die Knotenlasten und prueft selbst. Dieses Modul schreibt daraus eine Eingabedatei fuer ccx,+rechnet in einem eigenen Ordner und liest Verschiebungen (aus der .dat, 7 Stellen; die frd hat nur 5, daraus+nachgerechnete Lagerkraefte hatten 0.004 N Querkraft am Kragbalken) und Kontaktgroessen (aus der frd) zurueck. Die Spannungen+rechnet `fem.py` aus den Verschiebungen selbst (je Teil, CalculiX mittelt ueber Werkstoffgrenzen hinweg).++Gefunden wird ccx in dieser Reihenfolge: SOMA_CCX (Pfad zum Programm), mitgeliefert neben der Python-Umgebung+(<sys.prefix>/ccx/ccx.exe unter Windows, <sys.prefix>/ccx/bin/ccx sonst, geholt mit python/scripts/fetch_ccx.py),+dann `ccx` im PATH.++Robustheit: ccx bricht bei einem Teil, das wegrutscht oder abhebt, nicht von selbst ab, sondern rechnet minutenlang+weiter (die Verschiebungen wachsen auf 1e11 mm). Die Ausgabe wird deshalb mitgelesen und die Rechnung abgebrochen,+sobald eine Verschiebung ein Vielfaches der Modellgroesse erreicht oder die Schritte zu klein werden.+"""+import os+import re+import shutil+import subprocess+import sys+import tempfile+import threading+import time+from pathlib import Path++import numpy as np+++class CcxError(Exception):+ """Fehler beim Rechnen. `kind`: missing, failed, diverged (ein Teil fliegt weg), chatter (Kontakt pendelt zwischen+ offen und geschlossen), increments (Schritte zu klein oder zu viele)."""++ def __init__(self, message, kind="failed", log=""):+ super().__init__(message)+ self.kind = kind+ self.log = log+++# Kanten wie in gmsh (fem.EDGES): (0,1) (1,2) (2,0) (3,0) (3,2) (3,1). CalculiX/Abaqus: 1-2 2-3 3-1 1-4 2-4 3-4+NODE_ORDER = [0, 1, 2, 3, 4, 5, 6, 7, 9, 8]+# Flaeche k von fem (gegenueber Ecke k) -> Flaechennummer S1..S4 von C3D10 (S1 = 1-2-3, S2 = 1-4-2, S3 = 2-4-3, S4 = 3-4-1)+FACE_S = {0: 3, 1: 4, 2: 2, 3: 1}++_found = {}+++def find():+ """Pfad zu ccx oder None."""+ if "exe" in _found:+ return _found["exe"]+ cands = []+ if os.environ.get("SOMA_CCX"):+ cands.append(Path(os.environ["SOMA_CCX"]))+ base = Path(sys.prefix) / "ccx"+ cands += [base / "ccx.exe", base / "bin" / "ccx"] if sys.platform == "win32" else [base / "bin" / "ccx", base / "ccx"]+ w = shutil.which("ccx")+ if w:+ cands.append(Path(w))+ exe = next((c for c in cands if c.is_file()), None)+ _found["exe"] = exe+ return exe+++def version(exe=None):+ """Version als Text ("2.23") oder None, wenn ccx fehlt oder nicht startet."""+ exe = exe or find()+ if exe is None:+ return None+ if ("version", exe) in _found:+ return _found[("version", exe)]+ v = None+ try:+ p = subprocess.run([str(exe), "-v"], capture_output=True, text=True, errors="replace", timeout=30, env=_env(exe, 1),+ creationflags=_NO_WINDOW)+ m = re.search(r"Version\s+([0-9][0-9.]*)", p.stdout + p.stderr)+ v = m.group(1) if m else None+ except (OSError, subprocess.SubprocessError):+ v = None+ _found[("version", exe)] = v+ return v+++_NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0+++def threads():+ # Logische Kerne halbe: Hyperthreading bringt SPOOLES nichts, und die App soll bedienbar bleiben+ return max(1, min(8, (os.cpu_count() or 2) // 2))+++def _env(exe, n):+ if sys.platform == "win32":+ # Nur der eigene Ordner und System32: keine fremden DLLs gleichen Namens (libgfortran, openblas) aus dem PATH+ root = os.environ.get("SystemRoot", r"C:\Windows")+ env = {"SystemRoot": root, "PATH": f"{Path(exe).parent};{root}\\System32",+ "TEMP": tempfile.gettempdir(), "TMP": tempfile.gettempdir()}+ else:+ env = dict(os.environ)+ env.update(OMP_NUM_THREADS=str(n), CCX_NPROC_EQUATION_SOLVER=str(n), CCX_NPROC_RESULTS=str(n), CCX_NPROC_STIFFNESS=str(n))+ return env+++# ---- Eingabe ------------------------------------------------------------------------------------------------------++def _savetxt(o, rows, fmt):+ if len(rows):+ np.savetxt(o, rows, fmt=fmt)+++def write_input(path, nodes, tets, epart, mats, f, fixed, contacts=(), nonlinear=False, law="linear"):+ """Eingabedatei. contacts: dict(slave=[(el, k)], master=[(el, k)], friction, stiffness, stick, c0, p0).++ law "linear": Druck = Steifigkeit mal Ueberschneidung, genau, aber bei Flaechen, die mit Spalt 0 beginnen und sich+ teilweise trennen, pendelt die Rechnung (zwei Platten, die abheben: ueber 40 Iterationen ohne Ende).+ law "exponential": weich einsetzender Druck schon ab dem Spalt c0 (bei Spalt 0 der Druck p0), konvergiert dort in+ 5 Iterationen, glaettet aber Pressungsspitzen und laesst die Teile um etwa c0 einsinken."""+ with open(path, "w", newline="\n", encoding="ascii") as o:+ o.write("*HEADING\nsoma fem\n*NODE, NSET=NALL\n")+ _savetxt(o, np.column_stack([np.arange(1, len(nodes) + 1), nodes]), "%d,%.10g,%.10g,%.10g")+ t = tets[:, NODE_ORDER] + 1+ for p in range(len(mats)):+ sel = np.nonzero(epart == p)[0]+ if not len(sel):+ continue+ o.write(f"*ELEMENT, TYPE=C3D10, ELSET=P{p}\n")+ _savetxt(o, np.column_stack([sel + 1, t[sel]]), "%d," * 10 + "%d")+ for p, m in enumerate(mats):+ if not (epart == p).any():+ continue+ o.write(f"*MATERIAL, NAME=M{p}\n*ELASTIC\n{float(m['E']):.10g},{float(m['nu']):.10g}\n"+ f"*SOLID SECTION, ELSET=P{p}, MATERIAL=M{p}\n")+ for k, c in enumerate(contacts):+ for side in ("slave", "master"):+ o.write(f"*SURFACE, NAME=C{k}{side[0].upper()}, TYPE=ELEMENT\n")+ _savetxt(o, np.array([(e + 1, FACE_S[int(fk)]) for e, fk in c[side]]), "%d,S%d")+ if law == "exponential":+ o.write(f"*SURFACE INTERACTION, NAME=I{k}\n*SURFACE BEHAVIOR, PRESSURE-OVERCLOSURE=EXPONENTIAL\n"+ f"{c['c0']:.6g},{c['p0']:.6g}\n")+ else:+ o.write(f"*SURFACE INTERACTION, NAME=I{k}\n*SURFACE BEHAVIOR, PRESSURE-OVERCLOSURE=LINEAR\n{c['stiffness']:.6g}\n")+ if c.get("friction"):+ # Haftsteigung: steif genug fuer "haftet", weich genug fuer Konvergenz (zehnmal steifer verdoppelte die Iterationen)+ o.write(f"*FRICTION\n{c['friction']:.6g},{c['stick']:.6g}\n")+ o.write(f"*CONTACT PAIR, INTERACTION=I{k}, TYPE=SURFACE TO SURFACE\nC{k}S,C{k}M\n")+ # Zeitschritte nur mit Kontakt: die ganze Last auf einmal (CalculiX teilt selbst, wenn es nicht konvergiert; mit+ # Viertelschritten brauchte ein Block auf einem Block 18 statt 5 Iterationen), kleinster Schritt 1/1000 (darunter ist+ # es kein Rechenproblem mehr, sondern ein Teil, das nicht haelt), hoechstens 200 Schritte+ o.write("*STEP, INC=200\n*STATIC\n" + ("1,1,0.001,1\n" if nonlinear else ""))+ fd = np.nonzero(fixed)[0]+ if len(fd):+ o.write("*BOUNDARY\n")+ _savetxt(o, np.column_stack([fd // 3 + 1, fd % 3 + 1, fd % 3 + 1]), "%d,%d,%d")+ ld = np.nonzero(f)[0]+ if len(ld):+ o.write("*CLOAD\n")+ _savetxt(o, np.column_stack([ld // 3 + 1, ld % 3 + 1, f[ld]]), "%d,%d,%.12g")+ # Nur der letzte Schritt interessiert: FREQUENCY gross, das Ende des Steps schreibt CalculiX immer. Keine+ # Knotenkraefte (RF): Sie wichen um 5 % vom Gleichgewicht ab, die Lagerkraefte rechnet fem.py selbst+ o.write("*NODE PRINT, NSET=NALL, FREQUENCY=1000\nU\n")+ if contacts:+ o.write("*CONTACT FILE, FREQUENCY=1000\nCDIS,CSTR\n")+ o.write("*END STEP\n")+++# ---- Rechnen ------------------------------------------------------------------------------------------------------++def run(nodes, tets, epart, mats, f, fixed, contacts=(), size=1.0, progress=None, law="linear"):+ """Rechnet und liefert dict(u (n, 3), contact (n, 6) oder None, increments, seconds, threads, version).++ size: Modellgroesse in mm, fuer die Ueberwachung. progress: Rueckruf mit dem Anteil der Lastschritte (0..1).+ law: Kontaktgesetz (siehe write_input)."""+ exe = find()+ if exe is None:+ raise CcxError("CalculiX (ccx) was not found", "missing")+ n_thr = threads()+ nonlinear = bool(contacts)+ tmp = Path(tempfile.mkdtemp(prefix="soma-ccx-"))+ t0 = time.perf_counter()+ try:+ write_input(tmp / "job.inp", nodes, tets, epart, mats, f, fixed, contacts, nonlinear, law)+ log, increments = _execute(exe, tmp, n_thr, size, progress)+ u = read_dat(tmp / "job.dat", len(nodes)) if (tmp / "job.dat").is_file() else None+ if u is None:+ raise CcxError("CalculiX wrote no displacements. " + _errors(log), "failed", log)+ blocks = read_frd(tmp / "job.frd", len(nodes)) if contacts and (tmp / "job.frd").is_file() else {}+ if contacts and "CONTACT" not in blocks:+ raise CcxError("CalculiX wrote no contact results. " + _errors(log), "failed", log)+ if not np.isfinite(u).all():+ raise CcxError("CalculiX gave no numbers (supports insufficient?)", "failed", log)+ return dict(u=u, contact=blocks.get("CONTACT") if contacts else None, increments=increments,+ seconds=time.perf_counter() - t0, threads=n_thr, version=version(exe))+ finally:+ if os.environ.get("SOMA_CCX_KEEP"): # zur Fehlersuche: Eingabe, Ausgabe und Protokoll bleiben liegen+ print(f"CalculiX files kept in {tmp}", file=sys.stderr)+ else:+ shutil.rmtree(tmp, ignore_errors=True)+++def _errors(log):+ errs = []+ lines = log.splitlines()+ for i, line in enumerate(lines):+ if "*ERROR" in line:+ errs.append(" ".join(x.strip() for x in lines[i:i + 3] if x.strip()))+ return ("Messages: " + " | ".join(dict.fromkeys(errs))[:600]) if errs else ""+++# Iterationen je Laststufe und insgesamt, ab denen die Rechnung als pendelnd gilt. Konvergente Kontaktrechnungen brauchten+# 4 bis 6 je Stufe+MAX_ITERATIONS = 12+MAX_TOTAL_ITERATIONS = 80+++def _execute(exe, tmp, n_thr, size, progress):+ """Startet ccx, liest die Ausgabe mit und bricht bei Divergenz ab. Liefert (Ausgabe, Zahl der Lastschritte)."""+ p = subprocess.Popen([str(exe), "-i", "job"], cwd=tmp, env=_env(exe, n_thr), stdout=subprocess.PIPE,+ stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL, creationflags=_NO_WINDOW)+ lines = []+ state = dict(reason=None, increments=0, done=0.0, size=None, iters=0, total=0)+ limit = 10.0 * max(size, 1.0)++ def stop(reason):+ if state["reason"] is None:+ state["reason"] = reason+ p.kill()++ def reader():+ for raw in iter(p.stdout.readline, b""):+ line = raw.decode("latin-1", "replace").rstrip()+ lines.append(line)+ if " attempt " in line and line.lstrip().startswith("increment"):+ state["iters"] = 0+ elif line.startswith(" increment size="):+ try:+ state["size"] = float(line.split("=")[1])+ except ValueError:+ pass+ elif line.startswith(" sum of previous increments="):+ try:+ state["done"] = float(line.split("=")[1])+ except ValueError:+ pass+ elif line.startswith(" convergence") or line.startswith(" convergence;"):+ state["increments"] += 1+ if state["size"] is not None:+ state["done"] += state["size"]+ if progress:+ progress(min(1.0, state["done"]))+ elif line.startswith(" largest increment of disp="):+ try:+ d = float(line.split("=")[1])+ except ValueError:+ continue+ state["iters"] += 1+ state["total"] += 1+ if not d < limit:+ stop("diverged")+ elif state["iters"] > MAX_ITERATIONS or state["total"] > MAX_TOTAL_ITERATIONS:+ # Eine Laststufe, die so lange nicht konvergiert, pendelt (Kontakt springt zwischen offen und zu)+ stop("chatter")++ th = threading.Thread(target=reader, daemon=True)+ th.start()+ p.wait()+ th.join(timeout=10)+ log = "\n".join(lines)+ if os.environ.get("SOMA_CCX_KEEP"):+ (tmp / "job.log").write_text(log, encoding="utf-8")+ if state["reason"] == "chatter":+ raise CcxError(f"The contact does not settle (more than {MAX_ITERATIONS} iterations in one load step).", "chatter", log)+ if state["reason"] == "diverged":+ raise CcxError("A part moves without limit: it is not held, or a contact cannot carry the load (it slides or lifts off).",+ "diverged", log)+ if "increment size smaller than minimum" in log or "too many attempts" in log.lower():+ raise CcxError("The contact calculation does not converge (the load steps became too small).", "increments", log)+ if "maximum number of increments" in log.lower() or "the maximum number of increments" in log:+ raise CcxError("The contact calculation needs more than 200 load steps.", "increments", log)+ if p.returncode not in (0, None) and not (tmp / "job.frd").is_file():+ raise CcxError(f"CalculiX stopped (exit code {p.returncode}). " + _errors(log), "failed", log)+ if "*ERROR" in log and not (tmp / "job.frd").is_file():+ raise CcxError("CalculiX reported an error. " + _errors(log), "failed", log)+ return log, state["increments"]+++# ---- Ergebnis -----------------------------------------------------------------------------------------------------++def read_dat(path, n):+ """Verschiebungen aus der .dat (letzter Block "displacements (vx,vy,vz) for set NALL"): (n, 3) oder None."""+ lines = Path(path).read_text(encoding="latin-1").splitlines()+ starts = [i for i, line in enumerate(lines) if line.lstrip().startswith("displacements (vx,vy,vz)")]+ if not starts:+ return None+ u = np.zeros((n, 3))+ for line in lines[starts[-1] + 1:]:+ parts = line.split()+ if not parts:+ continue+ if len(parts) != 4 or not parts[0].isdigit():+ break+ node = int(parts[0])+ if 0 < node <= n:+ u[node - 1] = [float(x) for x in parts[1:]]+ return u+++def read_frd(path, n):+ """Liest die Knotenbloecke einer ASCII-frd. Je Blockname der letzte (spaetester Lastschritt): (n, Werte)."""+ out = {}+ cur = None+ width = None+ with open(path, "r", encoding="latin-1") as fh:+ for line in fh:+ if line.startswith(" -4"):+ parts = line.split()+ name, ncomp = parts[1], int(parts[2])+ cur = np.zeros((n, max(ncomp, 1)))+ out[name] = cur+ width = None+ continue+ if cur is None:+ continue+ if line.startswith(" -1"):+ rest = line[3:].rstrip("\r\n")+ if width is None:+ width = 10 if (len(rest) - 10) % 12 == 0 else 5+ node = int(rest[:width])+ k = (len(rest) - width) // 12+ if 0 < node <= n:+ vals = [float(rest[width + 12 * i:width + 12 * (i + 1)]) for i in range(min(k, cur.shape[1]))]+ cur[node - 1, :len(vals)] = vals+ elif line.startswith(" -3"):+ cur = None+ return outdiff --git a/python/soma/fem.py b/python/soma/fem.pyindex ce0a724..bf99dc9 100644--- a/python/soma/fem.py+++ b/python/soma/fem.py@@ -1,20 +1,24 @@-"""`soma analyze fem <study.py>`: strength with the finite element method (E58, E65).+"""`soma analyze fem <study.py>`: strength with the finite element method (E58, E65, E84).The agent describes the load case in a Python file (analyses/<name>.py) with `Study`: which parts, of which material,where they are held and which loads act where. Faces are chosen with CadQuery selectors (">X") or a point nearby.-The calculation is linear elastic:+The material is linear elastic:- Mesh: gmsh from the BREP shapes of the build, quadratic tetrahedra (10 nodes) on the real geometry. Several parts- are fused into one continuous mesh (rigidly joined where they touch).-- Stiffness in numpy (four Gauss points per element), solved with pyamg (CG with algebraic multigrid) or directly for- small meshes.+ are meshed together and bonded where they touch, unless `study.contact(a, b)` makes the touching faces a contact+ (can lift off, slides without or with friction).+- Solved with CalculiX (ccx, see soma.ccx). Without ccx, and only without contact, with the built-in solver: stiffness+ in numpy (four Gauss points per element), pyamg (CG with algebraic multigrid) or direct for small meshes.- Result as with the other analyses: .soma/results/<name>.glb with the safety factor as _VALUE, plus the von Mises- stress (_STRESS) and the displacement (_DISP) per vertex, and the load case as markers.+ stress (_STRESS), the displacement (_DISP) and with contact the contact pressure (_PRESS) per vertex, and the load+ case as markers.-Report instead of trust: the calculation checks that every part is held, gives the reaction forces, calculates a second-time with a coarser mesh and says when the peak stress depends on it (sharp inner edge, edge of a fixed support).+Report instead of trust: the calculation checks that every part is held, gives the reaction forces, names the parts+that touch and how they are joined, calculates a second time with a coarser mesh and says when the peak stress depends+on it (sharp inner edge, edge of a fixed support)."""import math+import osimport runpyimport sysimport tempfile@@ -24,7 +28,7 @@ from pathlib import Pathimport numpy as np-from . import analysis, build, gltf, mesher+from . import analysis, build, ccx, gltf, mesherclass FemError(analysis.AnalysisError):@@ -102,6 +106,7 @@ class Study:self.check_mesh = bool(check_mesh)self.materials = {}self.bcs = []+ self.contacts = []self.gravity_vec = Noneself.where = _caller()_STUDIES.append(self)@@ -130,6 +135,24 @@ class Study:self.bcs.append(dict(kind="pressure", part=str(part), faces=faces, near=near, value=float(MPa), where=_caller()))return self+ def contact(self, part_a, part_b, friction=0.0):+ """The faces where the two parts touch become a contact instead of bonded: they press on each other, can lift+ off and slide. friction=0: slides freely; friction=0.2 (steel on steel, dry) to 0.5 (plastic, rubber): sticks+ until the sideways force exceeds friction times the pressing force."""+ a, b = str(part_a), str(part_b)+ if a == b:+ raise FemError(f"contact({a!r}, {b!r}): two different parts are needed")+ try:+ mu = float(friction)+ except (TypeError, ValueError):+ raise FemError(f"contact(friction={friction!r}): a number, e.g. 0.2") from None+ if not 0 <= mu <= 2:+ raise FemError(f"contact(friction={mu:g}): between 0 and 2 (dry steel about 0.2, plastics 0.3 to 0.5)")+ if any({c["a"], c["b"]} == {a, b} for c in self.contacts):+ raise FemError(f"contact({a!r}, {b!r}) is given twice")+ self.contacts.append(dict(a=a, b=b, friction=mu, where=_caller()))+ return self+def gravity(self, g=9.81, direction="-z"):"""Self-weight from the density, g in m/s²."""d = str(direction).lower()@@ -546,17 +569,25 @@ def _rigid_modes(x):return R / scale-def _check_held(nodes, tets, fixed, part_names, epart):- """Jeder zusammenhaengende Körper muss gegen alle sechs Starrkoerperbewegungen gehalten sein."""+def _check_held(nodes, tets, fixed, part_names, epart, links=None, normals=None):+ """Jeder zusammenhaengende Körper muss gegen alle sechs Starrkoerperbewegungen gehalten sein.++ links: Knotenpaare (m, 2), die als verbunden gelten (Kontakt mit Reibung haelt wie verklebt, solange er nicht+ rutscht, das zeigt erst die Rechnung). normals: (Knoten, Normale) an Kontaktflaechen ohne Reibung, sie halten+ nur senkrecht zur Flaeche."""from scipy.sparse import coo_matrixfrom scipy.sparse.csgraph import connected_componentsn = len(nodes)c4 = tets[:, :4]rows = np.repeat(c4[:, 0], 3)cols = c4[:, 1:].reshape(-1)+ if links is not None and len(links):+ rows = np.concatenate([rows, links[:, 0]])+ cols = np.concatenate([cols, links[:, 1]])_k, lab = connected_components(coo_matrix((np.ones(len(rows)), (rows, cols)), shape=(n, n)), directed=False)used = np.zeros(n, bool)used[tets.reshape(-1)] = True+ nn_nodes, nn_vec = normals if normals is not None else (np.zeros(0, int), np.zeros((0, 3)))problems = []for comp in np.unique(lab[tets[:, 0]]):nd = np.nonzero((lab == comp) & used)[0]@@ -564,19 +595,115 @@ def _check_held(nodes, tets, fixed, part_names, epart):R = _rigid_modes(nodes[nd])dofs = (3 * nd[:, None] + np.arange(3)).reshape(-1)mask = fixed[dofs]- if not mask.any():+ C = [R[mask]]+ # Kontakt ohne Reibung: je Knoten der Flaeche eine Zeile n . (Bewegung des Knotens)+ sel = np.isin(nn_nodes, nd)+ if sel.any():+ pos = np.searchsorted(nd, nn_nodes[sel])+ Rn = R.reshape(len(nd), 3, 6)[pos]+ C.append(np.einsum("ij,ijk->ik", nn_vec[sel], Rn))+ C = np.concatenate(C)+ if not len(C):problems.append(f"{', '.join(names)}: not held (no fix() on this body; do the parts really touch?)")continue- _u, sv, vt = np.linalg.svd(R[mask], full_matrices=True)+ _u, sv, vt = np.linalg.svd(C, full_matrices=True)rank = int((sv > sv[0] * 1e-6).sum())if rank < 6:free = []for v in vt[rank:]:free.append(MODE_NAMES[int(np.argmax(np.abs(v)))])- problems.append(f"{', '.join(names)}: still free ({', '.join(dict.fromkeys(free))})")+ via = " (held only by a contact without friction, which does not hold sideways)" if sel.any() and not mask.any() else ""+ problems.append(f"{', '.join(names)}: still free ({', '.join(dict.fromkeys(free))}){via}")if problems:- raise FemError("The supports are not enough, the parts can move as a whole: " + "; ".join(problems)- + ". Fix more faces or use axes=\"xyz\".")+ hint = (" Fix more faces, use axes=\"xyz\", give the contact friction= or leave the parts bonded."+ if len(nn_nodes) else " Fix more faces or use axes=\"xyz\".")+ raise FemError("The supports are not enough, the parts can move as a whole: " + "; ".join(problems) + "." + hint)+++# ---- Beruehrflaechen und Kontakt ----------------------------------------------------------------------------------++def _interfaces(tets, epart):+ """Elementflaechen, die zwei Teile gemeinsam haben (gemeinsames Netz aus fragment): {(pa, pb): (el_a, k_a, el_b, k_b)}+ mit pa < pb."""+ key = np.sort(tets[:, FACES].reshape(-1, 3), axis=1)+ _u, inv, cnt = np.unique(key, axis=0, return_inverse=True, return_counts=True)+ inv = inv.reshape(-1)+ shared = np.nonzero(cnt[inv] == 2)[0]+ if not len(shared):+ return {}+ pairs = shared[np.argsort(inv[shared], kind="stable")].reshape(-1, 2)+ e1, e2 = pairs[:, 0] // 4, pairs[:, 1] // 4+ p1, p2 = epart[e1], epart[e2]+ out = {}+ diff = p1 != p2+ pairs, p1, p2 = pairs[diff], p1[diff], p2[diff]+ swap = p1 > p2+ pairs[swap] = pairs[swap][:, ::-1]+ lo, hi = np.minimum(p1, p2), np.maximum(p1, p2)+ for a, b in set(zip(lo.tolist(), hi.tolist())):+ s = np.nonzero((lo == a) & (hi == b))[0]+ fa, fb = pairs[s, 0], pairs[s, 1]+ out[(a, b)] = (fa // 4, fa % 4, fb // 4, fb % 4)+ return out+++def _face6(tets, el, k):+ """Knoten einer Elementflaeche (Ecken, dann Kantenmitten), ohne Ausrichtung: (n, 6)."""+ edge_of = {frozenset(e): 4 + i for i, e in enumerate(EDGES)}+ out = np.zeros((len(el), 6), dtype=np.int64)+ for f in range(4):+ s = k == f+ a, b, c = FACES[f]+ out[s] = tets[el[s]][:, [a, b, c, edge_of[frozenset((a, b))], edge_of[frozenset((b, c))], edge_of[frozenset((c, a))]]]+ return out+++def _split(nodes, tets, epart, faces, side):+ """Trennt die Knoten einer Beruehrflaeche: Die Elemente von Teil `side` bekommen eigene Kopien aller Knoten auf den+ Flaechen `faces` (el, k dieses Teils). Liefert (nodes, tets, remap fuer die Knoten des Teils, Paare alt/neu)."""+ dup = np.unique(_face6(tets, faces[0], faces[1]).reshape(-1))+ new = np.arange(len(nodes), len(nodes) + len(dup))+ remap = np.arange(len(nodes) + len(dup))+ remap[dup] = new+ tets = tets.copy()+ own = epart == side+ tets[own] = remap[tets[own]]+ # Ein drittes Teil, das an einer Kante der Beruehrflaeche mit `side` verklebt ist, haengt danach dort am anderen+ # Teil. Selten (drei Teile an einer Kante) und an einer Linie, das aendert das Ergebnis kaum+ return np.vstack([nodes, nodes[dup]]), tets, remap, np.column_stack([dup, new])+++def _reactions(nodes, tets, D, u, f, fixed):+ """Lagerkraefte K u - f, nur aus den Elementen an gehaltenen Knoten (an freien Knoten ist es 0). Die Knotenkraefte, die+ CalculiX in die frd schreibt, taugen dafuer nicht: Am Winkel aus dem Skill fehlten 5 % (45.4 statt 49 N), obwohl seine+ Verschiebungen mit K u = f bis auf 1e-6 stimmten."""+ held = np.zeros(len(nodes), bool)+ held[np.nonzero(fixed)[0] // 3] = True+ sel = np.nonzero(held[tets].any(axis=1))[0]+ r = np.zeros(3 * len(nodes))+ if len(sel):+ t = tets[sel]+ x = nodes[t]+ ue = u.reshape(-1, 3)[t].reshape(len(t), 30)+ fe = np.zeros((len(t), 30))+ for xi, w in GAUSS:+ g, det = _gradients(x, xi)+ B = _bmat(g)+ sig = np.einsum("eij,ejk,ek->ei", D[sel], B, ue)+ fe += np.einsum("eki,ek->ei", B, sig) * (w * det)[:, None]+ np.add.at(r, (3 * t[:, :, None] + np.arange(3)).reshape(-1), fe.reshape(-1))+ return np.where(fixed, r - f, 0.0)+++def _check_jacobians(nodes, tets, chunk=20000):+ """Zahl verzerrter Elemente (det J <= 0 an einem Gausspunkt), wie in _assemble, ohne die Matrix zu bauen."""+ bad = 0+ for s in range(0, len(tets), chunk):+ x = nodes[tets[s:s + chunk]]+ for xi, _w in GAUSS:+ dn = _dshape(xi)+ bad += int((np.linalg.det(np.einsum("eni,nj->eij", x, dn)) <= 0).sum())+ return baddef _solve(K, f, fixed, nodes):@@ -636,14 +763,56 @@ def _gravity_load(nodes, tets, dens_e, g):def _analyse(parts, study, mats, h, progress=None):"""Eine Rechnung mit Netzgroesse h. Liefert alles, was fuer Ergebnis und Pruefungen nötig ist."""names = [p["name"] for p in parts]+ index = {n: i for i, n in enumerate(names)}nodes, tets, epart, bc_faces = _mesh(parts, study.bcs, h, progress)_check_edges(nodes, tets)+ diag = float(np.linalg.norm(nodes.max(axis=0) - nodes.min(axis=0)))+ # Beruehrflaechen fuer den Bericht, vor dem Trennen+ inter = _interfaces(tets, epart)+ touching = {key: _faces_area(nodes, _tri6(nodes, tets, v[0], v[1])) for key, v in inter.items()}+ # Kontakt: Knoten der Beruehrflaeche trennen. Das weichere Teil ist die Slave-Seite (bei gleichem E das zweite)+ contacts, splits, links = [], [], []+ for c in study.contacts:+ a, b = index[c["a"]], index[c["b"]]+ key = (min(a, b), max(a, b))+ if key not in inter:+ raise FemError(f"contact({c['a']!r}, {c['b']!r}): the parts do not touch (no common faces in the mesh). A contact "+ "needs faces that lie on each other; a gap or a part that only touches along a line or a point "+ "does not count. Model the parts touching, or leave out contact().")+ ea, ka, eb, kb = inter[key]+ lo, hi = key+ if mats[lo]["E"] < mats[hi]["E"]:+ slave, sf, master, mf = lo, (ea, ka), hi, (eb, kb)+ else:+ slave, sf, master, mf = hi, (eb, kb), lo, (ea, ka)+ nodes, tets, remap, pairs = _split(nodes, tets, epart, sf, slave)+ splits.append((slave, remap))+ if c["friction"] > 0:+ links.append(pairs)+ e_min = min(mats[lo]["E"], mats[hi]["E"])+ # Kontaktgesetz (soma.ccx.write_input): linear mit dem Zehnfachen des E-Moduls (Empfehlung von CalculiX: 5 bis 50),+ # Rueckfall exponentiell mit Spalt c0 und Druck p0 bei Spalt 0 (beides klein gegen Netz und Werkstoff)+ contacts.append(dict(spec=c, slave=slave, master=master, sf=sf, mf=mf, key=key, stiffness=10.0 * e_min, stick=e_min,+ c0=min(0.05, max(0.002, 1e-4 * diag)), p0=max(0.1, e_min / 70000)))+ # Flaechen der Randbedingungen in der Nummerierung nach dem Trennen+ for bc, bf in zip(study.bcs, bc_faces):+ for side, remap in splits:+ if bc["part_index"] == side and len(bf["tris"]):+ bf["tris"] = np.sort(remap[bf["tris"]], axis=1)E = np.array([mats[p]["E"] for p in epart])nu = np.array([mats[p]["nu"] for p in epart])D = _dmat(E, nu)if progress:progress.total += len(tets) # Fortschritt nach Elementen, sobald ihre Zahl bekannt ist- K, bad = _assemble(nodes, tets, D, progress)+ use_ccx = os.environ.get("SOMA_FEM_SOLVER", "").lower() != "builtin" and ccx.find() is not None+ if contacts and not use_ccx:+ raise FemError("contact() needs CalculiX (ccx), which was not found. It comes with SOMA (in the folder ccx next to "+ "the Python environment); without it, leave out contact() so the parts are bonded.")+ K = None+ if use_ccx:+ bad = _check_jacobians(nodes, tets)+ else:+ K, bad = _assemble(nodes, tets, D, progress)if bad:raise FemError(f"{bad} distorted elements in the mesh (very small or sharp spots). Choose a finer mesh: Study(mesh=...)")el, k = _boundary(tets)@@ -690,17 +859,167 @@ def _analyse(parts, study, mats, h, progress=None):fg = _gravity_load(nodes, tets, dens, study.gravity_vec)f += fgapplied += fg.reshape(-1, 3).sum(axis=0)- _check_held(nodes, tets, fixed, names, epart)- u, method = _solve(K, f, fixed, nodes)- r = K @ u - f+ # Kontaktflaechen als Dreiecke mit 6 Knoten (nach dem Trennen), ohne Reibung halten sie nur senkrecht+ nrm_nodes, nrm_vec = [], []+ for c in contacts:+ c["s6"] = _tri6(nodes, tets, *c["sf"])+ c["m6"] = _tri6(nodes, tets, *c["mf"])+ if c["spec"]["friction"] == 0:+ for t6 in (c["s6"], c["m6"]):+ p = nodes[t6[:, :3]]+ nv = np.cross(p[:, 1] - p[:, 0], p[:, 2] - p[:, 0])+ nv /= np.maximum(np.linalg.norm(nv, axis=1, keepdims=True), 1e-30)+ nrm_nodes.append(t6[:, :3].reshape(-1))+ nrm_vec.append(np.repeat(nv, 3, axis=0))+ _check_held(nodes, tets, fixed, names, epart, links=np.concatenate(links) if links else None,+ normals=(np.concatenate(nrm_nodes), np.concatenate(nrm_vec)) if nrm_nodes else None)+ notes = []+ cvals = None+ u = None+ if use_ccx:+ done = [0]++ def step(frac):+ if progress:+ n = int(frac * len(tets)) - done[0]+ if n > 0:+ progress.step(n)+ done[0] += n+ spec = [dict(slave=list(zip(*c["sf"])), master=list(zip(*c["mf"])), friction=c["spec"]["friction"],+ stiffness=c["stiffness"], stick=c["stick"], c0=c["c0"], p0=c["p0"]) for c in contacts]+ try:+ try:+ res = ccx.run(nodes, tets, epart, mats, f, fixed, spec, size=diag, progress=step)+ except ccx.CcxError as e:+ if e.kind != "chatter":+ raise+ # Pendelt der Kontakt, mit dem weichen Gesetz noch einmal (gemeldet, nicht still)+ res = ccx.run(nodes, tets, epart, mats, f, fixed, spec, size=diag, progress=step, law="exponential")+ notes.append(f"The contact did not settle with the stiff contact law and was calculated with a softer one "+ f"(pressure starts {contacts[0]['c0']:.2g} mm before touching). Pressure peaks at edges are "+ "smoothed, the parts sink into each other by about that much.")+ u, cvals = res["u"].reshape(-1), res["contact"]+ r = _reactions(nodes, tets, D, u, f, fixed)+ method = f"CalculiX {res['version'] or ''}".strip() + f", {res['threads']} threads"+ if contacts:+ method += f", {res['increments']} load step{'s' if res['increments'] != 1 else ''}"+ except ccx.CcxError as e:+ if contacts:+ raise FemError(_contact_failure(e, study.contacts)) from None+ notes.append(f"CalculiX failed ({e}). Calculated with the built-in solver instead.")+ K, _bad = _assemble(nodes, tets, D, None)+ if progress and done[0] < len(tets):+ progress.step(len(tets) - done[0])+ elif os.environ.get("SOMA_FEM_SOLVER", "").lower() != "builtin":+ notes.append("CalculiX (ccx) was not found, calculated with the built-in solver (no contact possible).")+ if u is None:+ u, method = _solve(K, f, fixed, nodes)+ r = K @ u - ffor b in bcs_out:if b["kind"] == "fix":nd = np.unique(b["tri"].reshape(-1))b["reaction"] = r.reshape(-1, 3)[nd].sum(axis=0) * np.array([a in b["axes"] for a in "xyz"])vm = _stresses(nodes, tets, epart, D, u, len(parts))+ press, contacts_out = (_contact_results(nodes, u.reshape(-1, 3), contacts, cvals, f, np.where(fixed, r, 0.0), tets, epart) if contacts+ else (None, []))return dict(nodes=nodes, tets=tets, epart=epart, tri6=tri6, el=el, u=u.reshape(-1, 3), vm=vm, bcs=bcs_out,applied=applied, reaction=np.where(fixed, r, 0.0).reshape(-1, 3).sum(axis=0), method=method,- dofs=int((~fixed).sum()))+ dofs=int((~fixed).sum()), touching=touching, contacts=contacts_out, press=press, notes=notes)+++def _contact_failure(e, specs):+ pairs = ", ".join(f"{c['a']} – {c['b']}" + (f" (friction {c['friction']:g})" if c["friction"] else " (no friction)") for c in specs)+ if e.kind == "diverged":+ return (f"The calculation with contact does not hold together: a part moves without limit. Contact: {pairs}. "+ "Usually a part held only by a contact slides (friction too low for the sideways force, or no friction) "+ "or lifts off (the load pulls it away). Add friction, a fix() on that part, or leave the parts bonded.")+ if e.kind == "increments":+ return (f"The calculation with contact does not converge ({e}). Contact: {pairs}. Check that the parts are held, "+ "try friction=, a coarser mesh (Study(mesh=...)), or leave the parts bonded.")+ return f"CalculiX failed with contact ({pairs}): {e}"+++def _face_integrals(nodes, tri6, vals):+ """Je Dreieck mit 6 Knoten die Flaeche und das Integral eines Knotenwerts (vals: (n, 6)), mit den Formfunktionen."""+ x = nodes[tri6]+ area = np.zeros(len(tri6))+ integral = np.zeros(len(tri6))+ for (r, s), w in TRI_GAUSS:+ n, d = _tri_shape(r, s)+ tr = np.einsum("eni,nj->eji", x, d)+ ja = np.linalg.norm(np.cross(tr[:, 0], tr[:, 1]), axis=1)+ area += w * ja+ integral += w * ja * (vals @ n)+ return area, integral+++def _contact_results(nodes, u, contacts, cvals, f, r, tets, epart):+ """Je Kontaktpaar Kennzahlen, dazu die Pressung je Knoten auf beiden Seiten (fuer das Bild).++ Pressung und Schub kommen von CalculiX (an der Slave-Seite). Spalt und Gleitweg rechnet soma selbst aus den+ Verschiebungen: Gegenueberliegende Knoten liegen am Anfang an derselben Stelle (das Netz ist dort deckungsgleich).+ CalculiX gibt die Oeffnung nur nahe der Flaeche aus, bei 16 mm Spalt stand dort 0.++ Getragene Kraft: Hat das Slave-Teil nur diesen Kontakt, ist sie genau das Negative aus Lasten und Lagerkraeften auf+ diesem Teil (Gleichgewicht). Die Pressungen an den Knoten haben an Kanten Spitzen, ihr Integral lag 6 % daneben+ (1058 statt 1000 N). Sonst bleibt nur dieses Integral."""+ from scipy.spatial import cKDTree+ press = np.zeros(len(nodes))+ p_all, tau_all = np.maximum(cvals[:, 3], 0.0), np.hypot(cvals[:, 4], cvals[:, 5])+ out = []+ for c in contacts:+ s6, m6 = c["s6"], c["m6"]+ nd = np.unique(s6.reshape(-1))+ mn = np.unique(m6.reshape(-1))+ _d, near = cKDTree(nodes[mn]).query(nodes[nd])+ partner = mn[near]+ # Normale je Knoten der Slave-Seite (zur Gegenseite hin), gemittelt ueber die anliegenden Flaechen+ pts = nodes[s6[:, :3]]+ fn = np.cross(pts[:, 1] - pts[:, 0], pts[:, 2] - pts[:, 0])+ acc = np.zeros((len(nodes), 3))+ np.add.at(acc, s6.reshape(-1), np.repeat(fn, 6, axis=0))+ nrm = acc[nd] / np.maximum(np.linalg.norm(acc[nd], axis=1, keepdims=True), 1e-30)+ rel = u[partner] - u[nd]+ gap_n = np.einsum("ij,ij->i", rel, nrm) # positiv: die Gegenseite entfernt sich+ slip_n = np.linalg.norm(rel - gap_n[:, None] * nrm, axis=1)+ gap = np.zeros(len(nodes))+ gap[nd] = gap_n+ p = np.zeros(len(nodes))+ p[nd] = p_all[nd]+ press[nd] = p[nd]+ _d, back = cKDTree(nodes[nd]).query(nodes[mn])+ press[mn] = np.maximum(press[mn], p[nd][back])+ area, total = _face_integrals(nodes, s6, p[s6])+ a_all = float(area.sum())+ force, shear = float(total.sum()), None+ if sum(1 for o in contacts if c["slave"] in (o["slave"], o["master"])) == 1:+ own = np.unique(tets[epart == c["slave"]].reshape(-1))+ F = -(f.reshape(-1, 3)[own].sum(axis=0) + r.reshape(-1, 3)[own].sum(axis=0)) # Kraft der Gegenseite auf das Teil+ n_avg = (fn / 2).sum(axis=0)+ n_avg /= max(np.linalg.norm(n_avg), 1e-30)+ fn_n = float(-F @ n_avg)+ # Nur fuer ebene Beruehrflaechen ist die Richtung eindeutig: sonst (Welle in Bohrung) bleibt das Integral+ if np.linalg.norm(fn / 2, axis=1).sum() * 0.98 <= np.linalg.norm((fn / 2).sum(axis=0)):+ force, shear = fn_n, float(np.linalg.norm(F + fn_n * n_avg))+ # Offen: deutlich getrennt (mehr als die Weichheit des Kontaktgesetzes) an allen Ecken der Flaeche+ open_f = (gap[s6[:, :3]] > 2 * c["c0"]).all(axis=1)+ w = nd[int(np.argmax(p[nd]))]+ mu = c["spec"]["friction"]+ entry = dict(a=c["spec"]["a"], b=c["spec"]["b"], friction=mu, area=round(a_all, 3), force=round(force, 3),+ maxPressure=dict(value=round(float(p[w]), 4), at=[round(float(x), 3) for x in nodes[w]]),+ openArea=round(float(area[open_f].sum()), 3), maxGap=round(float(max(gap_n.max(initial=0.0), 0.0)), 5),+ maxSlip=round(float(slip_n.max(initial=0.0)), 5))+ if mu > 0:+ # Gleiten, wo die Schubspannung die Haftgrenze erreicht (die Haftsteigung laesst auch beim Haften kleine Wege zu)+ tau = np.zeros(len(nodes))+ tau[nd] = tau_all[nd]+ pc = p[s6]+ sl = (pc > 1e-3) & (tau[s6] >= 0.95 * mu * pc)+ entry["slideArea"] = round(float(area[sl.mean(axis=1) >= 0.5].sum()), 3)+ if shear is not None:+ entry["shear"] = round(shear, 3)+ out.append(entry)+ return press, out# ---- Ablauf -------------------------------------------------------------------------------------------------------@@ -725,7 +1044,8 @@ def run(root, study_rel, name=None, threshold=None):by_name = {}for p in all_parts:by_name.setdefault(p["name"], p)- wanted = list(dict.fromkeys([b["part"] for b in study.bcs] + [str(x) for x in study.extra_parts] + list(study.materials)))+ wanted = list(dict.fromkeys([b["part"] for b in study.bcs] + [c[x] for c in study.contacts for x in ("a", "b")]+ + [str(x) for x in study.extra_parts] + list(study.materials)))missing = [w for w in wanted if w not in by_name]if missing:names = ", ".join(repr(p["name"]) for p in all_parts[:30])@@ -765,7 +1085,8 @@ def _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safefixed_nodes = np.unique(np.concatenate([b["tri"].reshape(-1) for b in fine["bcs"] if b["kind"] == "fix"]))load_nodes = [np.unique(b["tri"].reshape(-1)) for b in fine["bcs"] if b["kind"] != "fix"]load_nodes = np.unique(np.concatenate(load_nodes)) if load_nodes else np.zeros(0, int)- notes, part_fem = [], []+ notes, part_fem = list(fine.get("notes") or []), []+ press = fine.get("press")for pi, p in enumerate(parts):t6 = fine["tri6"][epart_b == pi]if not len(t6):@@ -826,6 +1147,8 @@ def _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safePOSITION=g.accessor(nodes[flat], "VEC3", gltf.ARRAY_BUFFER, True), NORMAL=g.accessor(np.repeat(inrm, 3, axis=0), "VEC3", gltf.ARRAY_BUFFER),_VALUE=g.accessor(sf[flat], "SCALAR", gltf.ARRAY_BUFFER), _STRESS=g.accessor(node_vm[flat], "SCALAR", gltf.ARRAY_BUFFER),_DISP=g.accessor(u[flat], "VEC3", gltf.ARRAY_BUFFER)))]+ if press is not None:+ prims[0]["attributes"]["_PRESS"] = g.accessor(press[flat], "SCALAR", gltf.ARRAY_BUFFER)m = mesher.mesh_shape(p["shape"], tol, 0.3)if m["lines"].size:lines = np.asarray(m["lines"], dtype=float).reshape(-1, 3)@@ -860,6 +1183,23 @@ def _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safeif b["kind"] == "pressure":entry["value"] = b["value"]bcs_meta.append(entry)+ # Beruehrende Teile: verklebt, sofern die Studie keinen Kontakt nennt+ joined = {frozenset((c["a"], c["b"])): c for c in study.contacts}+ touching = []+ for (a, b), area in sorted(fine["touching"].items()):+ c = joined.get(frozenset((parts[a]["name"], parts[b]["name"])))+ touching.append(dict(a=parts[a]["name"], b=parts[b]["name"], area=round(float(area), 2),+ joint="contact" if c else "bonded", friction=c["friction"] if c else None))+ if any(t["joint"] == "bonded" for t in touching):+ notes.append("Touching parts are bonded (glued) where they touch. If a part only rests on another or can slide, "+ "add study.contact(\"A\", \"B\") (with friction=0.2 or so when clamped or screwed together).")+ for c in fine["contacts"]:+ if c["openArea"] > 0.5 * c["area"]:+ notes.append(f"Contact {c['a']} – {c['b']}: open on {100 * c['openArea'] / c['area']:.0f} % of the touching faces "+ "(lifts off there). Check that the load really presses the parts together.")+ if c.get("slideArea", 0) > 0.5 * c["area"]:+ notes.append(f"Contact {c['a']} – {c['b']}: slides on {100 * c['slideArea'] / c['area']:.0f} % of the touching "+ f"faces (friction {c['friction']:g} at its limit). It may start to slip in reality.")max_disp = float(disp.max())if max_disp > 0.05 * diag: # ab etwa 5 % der Modellgröße aendert die Verformung die Lastwege merklichnotes.append(f"The deformation is large ({max_disp:.2f} mm, {100 * max_disp / diag:.1f} % of the model size). The linear "@@ -867,11 +1207,13 @@ def _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safefor st in stats:ch = st.get("coarse", {}).get("change")where = " and ".join(st["peakAt"])- if ch is not None and abs(ch) > 0.15:+ # Kaum belastete Teile (etwa die Gegenseite eines Kontakts, der abhebt) nicht melden: Dort ist jede Spitze klein+ loaded = st["stress"]["value"] > 0.02 * st["material"]["strength"]+ if ch is not None and abs(ch) > 0.15 and loaded:notes.append(f"{st['name']}: The peak stress depends on the mesh ({st['coarse']['stress']:.1f} MPa coarse, {st['stress']['value']:.1f} MPa fine)"+ (f", it is {where}" if where else ", probably at a sharp inner edge")+ ". The value is not reliable there: add a radius or judge the spot from a little distance.")- elif where:+ elif where and loaded:notes.append(f"{st['name']}: The peak stress is {where}. A rigid fixed support or a load on a small face ""often gives too high a peak there: judge values a little away from it.")if st["material"]["note"]:@@ -891,6 +1233,7 @@ def _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safefem=dict(study=rel, bcs=bcs_meta, applied=[round(float(x), 3) + 0.0 for x in applied],reaction=[round(float(x), 3) + 0.0 for x in reaction], balance=round(balance, 6),gravity=study.gravity_vec, maxDisp=round(max_disp, 4), diag=round(diag, 3),+ touching=touching, contacts=fine["contacts"],# auto: Die Studie gibt keine Netzgroesse vor, sie folgt der Modellgroesse (der Bericht sagt das dazu)mesh=dict(size=h, auto=not study.mesh, nodes=int(len(nodes)), elements=int(len(fine["tets"])), dofs=fine["dofs"],solver=fine["method"]),@@ -913,7 +1256,8 @@ def _v(v, unit="N"):def summary_text(meta):fe = meta["fem"]safety = meta["threshold"]- lines = [f"FEM \"{meta['title']}\" ({fe['study']}, linear elastic, safety factor {safety:g} required, critical below):"]+ kind = "linear elastic, with contact" if fe.get("contacts") else "linear elastic"+ lines = [f"FEM \"{meta['title']}\" ({fe['study']}, {kind}, safety factor {safety:g} required, critical below):"]for b in fe["bcs"]:if b["kind"] == "fix":ax = "" if b["axes"] == "xyz" else f" only in {', '.join(b['axes'].upper())}"@@ -924,6 +1268,21 @@ def summary_text(meta):lines.append(f" pressure: {b['label']} ({b['area']:.1f} mm²), {b['value']:g} MPa, in total {_v(b['total'])}")if fe.get("gravity"):lines.append(f" self-weight: {_v(fe['gravity'], 'm/s²')}")+ contacts = {frozenset((c["a"], c["b"])): c for c in fe.get("contacts") or []}+ for t in fe.get("touching") or []:+ c = contacts.get(frozenset((t["a"], t["b"])))+ if c is None:+ lines.append(f" touching: {t['a']} – {t['b']} ({t['area']:.1f} mm²), bonded")+ continue+ fr = f"friction {c['friction']:g}" if c["friction"] else "no friction"+ mp = c["maxPressure"]+ state = [f"open on {100 * c['openArea'] / max(c['area'], 1e-12):.0f} % (gap up to {c['maxGap']:.3f} mm)" if c["openArea"] > 0 else "closed everywhere"]+ if c.get("slideArea"):+ state.append(f"slides on {100 * c['slideArea'] / max(c['area'], 1e-12):.0f} %")+ state.append(f"slip up to {c['maxSlip']:.3f} mm")+ side = f" and {c['shear']:.1f} N sideways" if c.get("shear") is not None and c["friction"] else ""+ lines.append(f" contact: {c['a']} – {c['b']} ({c['area']:.1f} mm², {fr}), carries {c['force']:.1f} N{side}, highest pressure "+ f"{mp['value']:.2f} MPa at {analysis._where(mp['at'])}, {', '.join(state)}")for st in meta["parts"]:mat = st["material"]s, d, w = st["stress"], st["displacement"], st["worst"]