|
| 1 | +# coding: utf8 |
| 2 | + |
| 3 | +import unittest |
| 4 | +import Sofa |
| 5 | +import Sofa.Core |
| 6 | +import Sofa.Helper |
| 7 | +import Sofa.Simulation |
| 8 | +import SofaRuntime |
| 9 | + |
| 10 | +class Test(unittest.TestCase): |
| 11 | + |
| 12 | + @staticmethod |
| 13 | + def simulate_beam(linear_solver_template): |
| 14 | + root = Sofa.Core.Node("rootNode") |
| 15 | + |
| 16 | + loop = root.addObject('DefaultAnimationLoop') |
| 17 | + |
| 18 | + root.addObject('RequiredPlugin', name='Sofa.Component.ODESolver.Backward') |
| 19 | + root.addObject('RequiredPlugin', name='Sofa.Component.LinearSolver.Direct') |
| 20 | + root.addObject('RequiredPlugin', name='Sofa.Component.Engine.Select') |
| 21 | + root.addObject('RequiredPlugin', name='Sofa.Component.Constraint.Projective') |
| 22 | + root.addObject('RequiredPlugin', name='Sofa.Component.SolidMechanics.FEM.Elastic') |
| 23 | + root.addObject('RequiredPlugin', name='Sofa.Component.Mass') |
| 24 | + |
| 25 | + root.addObject('EulerImplicitSolver', rayleighStiffness="0.1", rayleighMass="0.1") |
| 26 | + root.addObject('SparseLDLSolver', applyPermutation="false", template=linear_solver_template) |
| 27 | + |
| 28 | + root.addObject('MechanicalObject', name="DoFs") |
| 29 | + mass = root.addObject('MeshMatrixMass', name="mass", totalMass="320") |
| 30 | + root.addObject('RegularGridTopology', name="grid", nx="4", ny="4", nz="20", xmin="-9", xmax="-6", ymin="0", ymax="3", zmin="0", zmax="19") |
| 31 | + root.addObject('BoxROI', name="box", box="-10 -1 -0.0001 -5 4 0.0001") |
| 32 | + root.addObject('FixedConstraint', indices="@box.indices") |
| 33 | + root.addObject('HexahedronFEMForceField', name="FEM", youngModulus="4000", poissonRatio="0.3", method="large") |
| 34 | + |
| 35 | + matrix_accessor = root.addObject(MatrixAccessController('MatrixAccessor', name='matrixAccessor', mass=mass)) |
| 36 | + |
| 37 | + Sofa.Simulation.init(root) |
| 38 | + Sofa.Simulation.animate(root, 0.0001) |
| 39 | + |
| 40 | + return matrix_accessor.mass_matrix |
| 41 | + |
| 42 | + def test_stiffness_matrix_access_scalar(self): |
| 43 | + |
| 44 | + M = self.simulate_beam("CompressedRowSparseMatrixd") |
| 45 | + |
| 46 | + self.assertEqual(M.ndim, 2) |
| 47 | + self.assertEqual(M.shape, (960, 960)) |
| 48 | + self.assertEqual(M.nnz, 9480) |
| 49 | + |
| 50 | + def test_stiffness_matrix_access_blocks3x3(self): |
| 51 | + |
| 52 | + M = self.simulate_beam("CompressedRowSparseMatrixMat3x3d") |
| 53 | + |
| 54 | + self.assertEqual(M.ndim, 2) |
| 55 | + self.assertEqual(M.shape, (960, 960)) |
| 56 | + self.assertEqual(M.nnz, 9480) |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +class MatrixAccessController(Sofa.Core.Controller): |
| 61 | + |
| 62 | + |
| 63 | + def __init__(self, *args, **kwargs): |
| 64 | + Sofa.Core.Controller.__init__(self, *args, **kwargs) |
| 65 | + self.mass = kwargs.get("mass") |
| 66 | + |
| 67 | + def onAnimateEndEvent(self, event): |
| 68 | + self.mass_matrix = self.mass.assembleMMatrix() |
0 commit comments