-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
FreeVibration(items, boundaries)
(#909)
* Add `FreeVibration(items, boundaries)` with methods to `evaluate()` and `extract()` the n-th eigenvector to a field * Update _free_vibration.py * Create test_free_vibration.py * add test * Update test_mechanics.py * Update test_mechanics.py * add test, format black * Update test_free_vibration.py * update the free-vibration test * Update mechanics.rst
- Loading branch information
Showing
8 changed files
with
250 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This file is part of FElupe. | ||
FElupe is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
FElupe is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with FElupe. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import numpy as np | ||
from scipy.sparse import csr_matrix | ||
from scipy.sparse.linalg import eigsh | ||
|
||
from ..dof import partition | ||
|
||
|
||
class FreeVibration: | ||
"""A Free-Vibration Step/Job. | ||
Parameters | ||
---------- | ||
items : list of SolidBody or SolidBodyNearlyIncompressible | ||
A list of items with methods for the assembly of sparse stiffness and mass | ||
matrices. | ||
boundaries : dict of Boundary, optional | ||
A dict with :class:`~felupe.Boundary` conditions (default is None). | ||
Notes | ||
----- | ||
.. note:: | ||
Boundary conditions with non-zero values are not supported. | ||
Examples | ||
-------- | ||
.. pyvista-plot:: | ||
>>> import felupe as fem | ||
>>> import numpy as np | ||
>>> | ||
>>> mesh = fem.Rectangle(b=(5, 1), n=(50, 10)) | ||
>>> region = fem.RegionQuad(mesh) | ||
>>> field = fem.FieldContainer([fem.FieldPlaneStrain(region, dim=2)]) | ||
>>> | ||
>>> boundaries = dict(left=fem.Boundary(field[0], fx=0)) | ||
>>> solid = fem.SolidBody(fem.LinearElastic(E=2.5, nu=0.25), field, density=1.0) | ||
>>> modal = fem.FreeVibration(items=[solid], boundaries=boundaries).evaluate() | ||
>>> | ||
>>> eigenvector, frequency = modal.extract(n=4, inplace=True) | ||
>>> solid.plot("Stress", component=0).show() | ||
""" | ||
|
||
def __init__(self, items, boundaries=None): | ||
self.items = items | ||
|
||
if boundaries is None: | ||
boundaries = {} | ||
|
||
self.boundaries = boundaries | ||
self.eigenvalues = None | ||
self.eigenvectors = None | ||
|
||
def evaluate(self, x0=None, solver=eigsh, parallel=False, **kwargs): | ||
if x0 is not None: | ||
x = x0 | ||
else: | ||
x = self.items[0].field | ||
|
||
# link field of items with global field | ||
[item.field.link(x) for item in self.items] | ||
|
||
# init matrix with shape from global field | ||
shape = (np.sum(x.fieldsizes), np.sum(x.fieldsizes)) | ||
stiffness = csr_matrix(shape) | ||
mass = csr_matrix(shape) | ||
|
||
# assemble matrices | ||
for item in self.items: | ||
K = item.assemble.matrix(parallel=parallel) | ||
M = item.assemble.mass() | ||
|
||
if item.assemble.multiplier is not None: | ||
K *= item.assemble.multiplier | ||
|
||
# check and reshape matrices | ||
if K.shape != stiffness.shape: | ||
K.resize(*shape) | ||
|
||
if M.shape != mass.shape: | ||
M.resize(*shape) | ||
|
||
# add matrices | ||
stiffness += K | ||
mass += M | ||
|
||
dof0, self.dof1 = partition(x, self.boundaries) | ||
|
||
K = stiffness[self.dof1][:, self.dof1] | ||
M = mass[self.dof1][:, self.dof1] | ||
|
||
sigma = kwargs.get("sigma", 0) | ||
self.eigenvalues, self.eigenvectors = solver(A=K, M=M, sigma=sigma, **kwargs) | ||
|
||
return self | ||
|
||
def extract(self, n=0, x0=None, inplace=True): | ||
if x0 is not None: | ||
field = x0 | ||
else: | ||
field = self.items[0].field | ||
|
||
if not inplace: | ||
field = field.copy() | ||
|
||
values = np.zeros(sum(field.fieldsizes)) | ||
values[self.dof1] = self.eigenvectors[:, n] | ||
|
||
frequency = np.sqrt(self.eigenvalues[n]) / (2 * np.pi) | ||
|
||
[f.fill(0) for f in field] | ||
field += values | ||
|
||
return field, frequency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
_______ _______ ___ __ __ _______ _______ | ||
| || || | | | | || || | | ||
| ___|| ___|| | | | | || _ || ___| | ||
| |___ | |___ | | | |_| || |_| || |___ | ||
| ___|| ___|| |___ | || ___|| ___| | ||
| | | |___ | || || | | |___ | ||
|___| |_______||_______||_______||___| |_______| | ||
This file is part of felupe. | ||
Felupe is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Felupe is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Felupe. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import numpy as np | ||
|
||
import felupe as fem | ||
|
||
|
||
def test_free_vibration(): | ||
|
||
mesh = fem.Cube(a=(0, 0, 5), b=(50, 100, 30), n=(3, 6, 4)) | ||
region = fem.RegionHexahedron(mesh) | ||
field = fem.FieldContainer([fem.Field(region, dim=3)]) | ||
|
||
umat = fem.NeoHooke(mu=1, bulk=2) | ||
solid = fem.SolidBody(umat=umat, field=field, density=1.5e-9) | ||
|
||
job = fem.FreeVibration([solid]).evaluate() | ||
new_field, frequency = job.extract(n=-1, inplace=False) | ||
|
||
|
||
def test_free_vibration_mixed(): | ||
|
||
meshes = [ | ||
fem.Cube(a=(0, 0, 30), b=(50, 100, 35), n=(3, 6, 2)), | ||
fem.Cube(a=(0, 0, 5), b=(50, 100, 30), n=(3, 6, 4)), | ||
fem.Cube(a=(0, 0, 0), b=(50, 100, 5), n=(3, 6, 2)), | ||
] | ||
container = fem.MeshContainer(meshes, merge=True) | ||
mesh = container.stack() | ||
|
||
regions = [fem.RegionHexahedron(m) for m in container.meshes] | ||
fields = [ | ||
fem.FieldsMixed(regions[0], n=1), | ||
fem.FieldsMixed(regions[1], n=3), | ||
fem.FieldsMixed(regions[2], n=1), | ||
] | ||
|
||
region = fem.RegionHexahedron(mesh) | ||
field = fem.FieldContainer([fem.Field(region, dim=3), *fields[1][1:]]) | ||
|
||
boundaries = dict(left=fem.Boundary(field[0], fx=0)) | ||
rubber = fem.ThreeFieldVariation(fem.NeoHooke(mu=1, bulk=5000)) | ||
steel = fem.LinearElasticLargeStrain(2.1e5, 0.3) | ||
solids = [ | ||
fem.SolidBody(umat=steel, field=fields[0], density=7.85e-9), | ||
fem.SolidBody(umat=rubber, field=fields[1], density=1.5e-9), | ||
fem.SolidBody(umat=steel, field=fields[2], density=7.85e-9), | ||
] | ||
|
||
job = fem.FreeVibration(solids, boundaries).evaluate(x0=field) | ||
new_field, frequency = job.extract(x0=field, n=-1, inplace=False) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_free_vibration() | ||
test_free_vibration_mixed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters