|
| 1 | +import os |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from dotenv import load_dotenv |
| 5 | +from tesseract_core import Tesseract |
| 6 | + |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | +host = os.getenv("MAPDL_HOST") |
| 10 | +if host is None: |
| 11 | + raise ValueError("Unable to read $MAPDL_HOST from the environment.") |
| 12 | +port = os.getenv("MAPDL_PORT") |
| 13 | +if port is None: |
| 14 | + raise ValueError("Unable to read $MAPDL_PORT from the environment.") |
| 15 | + |
| 16 | + |
| 17 | +tess_simp_compliance = Tesseract.from_tesseract_api( |
| 18 | + "tesseract_pymapdl/tess_simp_compiance/tesseract_api.py" |
| 19 | +) |
| 20 | +Lx, Ly, Lz = 3, 2, 1 |
| 21 | +Nx, Ny, Nz = 60, 40, 20 |
| 22 | +# Nx, Ny, Nz = 6, 4, 2 |
| 23 | + |
| 24 | +n_elem = Nx * Ny * Nz |
| 25 | +# Create a test density field varying from 0 to 1 |
| 26 | +rho = (np.arange(0, n_elem, 1) / n_elem).reshape((n_elem, 1)) |
| 27 | +rho = 0.5 * np.ones((n_elem, 1)) |
| 28 | + |
| 29 | + |
| 30 | +inputs = { |
| 31 | + "host": host, |
| 32 | + "port": port, |
| 33 | + "rho": rho, |
| 34 | + "Lx": Lx, |
| 35 | + "Ly": Ly, |
| 36 | + "Lz": Lz, |
| 37 | + "Nx": Nx, |
| 38 | + "Ny": Ny, |
| 39 | + "Nz": Nz, |
| 40 | + "E0": 1.0, |
| 41 | + "rho_min": 1e-6, |
| 42 | + "total_force": 0.1, |
| 43 | + "log_level": "DEBUG", |
| 44 | + "vtk_output": "mesh_density.vtk", |
| 45 | +} |
| 46 | + |
| 47 | +outputs = tess_simp_compliance.apply(inputs) |
| 48 | + |
| 49 | +# Verify relationship between compliance and strain energy |
| 50 | +# For static analysis: Total Strain Energy = 0.5 * Compliance |
| 51 | +strain_energy = outputs["strain_energy"] |
| 52 | +compliance = outputs["compliance"] |
| 53 | +total_strain_energy = np.sum(strain_energy) |
| 54 | +print(f"\nCompliance: {compliance:.6e}") |
| 55 | +print(f"Total Strain Energy: {total_strain_energy:.6e}") |
| 56 | +print(f"0.5 * Compliance: {0.5 * compliance:.6e}") |
| 57 | +print(f"Ratio (should be ~1.0): {total_strain_energy / (0.5 * compliance):.6f}") |
| 58 | + |
| 59 | +# Finite difference check |
| 60 | +num_tests = 0 # set to 0 if you don't want to run this check |
| 61 | +FD_delta = 1.0e-3 |
| 62 | +f0 = outputs["compliance"] |
| 63 | +sensitivity = outputs["sensitivity"] |
| 64 | +FD_sensitivity = 0 * sensitivity |
| 65 | +for i in range(num_tests): |
| 66 | + print(i) |
| 67 | + inputs["rho"][i] += FD_delta |
| 68 | + outputs = tess_simp_compliance.apply(inputs) |
| 69 | + fupp = outputs["compliance"] |
| 70 | + FD_sensitivity[i] = (fupp - f0) / FD_delta |
| 71 | + inputs["rho"][i] -= FD_delta |
| 72 | + |
| 73 | + |
| 74 | +if num_tests > 0: |
| 75 | + sens = sensitivity[0:num_tests] |
| 76 | + FD_sens = FD_sensitivity[0:num_tests] |
| 77 | + print(sens) |
| 78 | + print(FD_sens) |
| 79 | + errors = sens - FD_sens |
| 80 | + print(errors) |
| 81 | + rel_abs_error = np.abs(errors / sens) |
| 82 | + print(rel_abs_error) |
| 83 | + print(f"Should be under 1e-5: {np.max(rel_abs_error)}") |
0 commit comments