Comments (2)
import felupe as fem
import numpy as np
from threading import Thread
mesh = fem.Cube(n=51)
region = fem.RegionHexahedron(mesh)
field = fem.FieldContainer([fem.Field(region, dim=3)])
boundaries, loadcase = fem.dof.uniaxial(field, clamped=True)
class Threaded(fem.constitution.ConstitutiveMaterial):
def __init__(self, material, axis, nthreads, **kwargs):
self.material = material
self.x = self.material.x
self.axis = axis
self._axes = [slice(None)] * abs(self.axis + 1)
self.nthreads = nthreads
self._P = None
self._A = None
for key, value in kwargs.items():
setattr(self, key, value)
def _kernel_grad(self, F, chunk):
self.material.gradient([F, None], out=self._P[..., slice(*chunk), *self._axes])
def _kernel_hess(self, F, chunk):
self.material.hessian([F, None], out=self._A[..., slice(*chunk), *self._axes])
def chunks(self, F):
defgrads = np.array_split(F, self.nthreads, axis=self.axis)
chunksizes = np.cumsum([chunk.shape[self.axis] for chunk in defgrads])
chunks = np.vstack([np.append(0, chunksizes[:-1]), chunksizes]).T
return defgrads, chunksizes, chunks
def evaluate(self, target, defgrads, chunks):
threads = [
Thread(target=target, args=(F, chunk)) for F, chunk in zip(defgrads, chunks)
]
for t in threads:
t.start()
for t in threads:
t.join()
def gradient(self, x):
F, statevars = x
defgrads, chunks, chunksizes = self.chunks(F)
if self._P is None:
self._P = np.zeros_like(F)
self.evaluate(self._kernel_grad, defgrads, chunks)
return [self._P, None]
def hessian(self, x):
F, statevars = x
defgrads, chunks, chunksizes = self.chunks(F)
if self._A is None:
self._A = np.zeros((*F.shape[:2], *F.shape))
self.evaluate(self._kernel_hess, defgrads, chunks)
return [self._A]
umat_threaded = Threaded(fem.NeoHooke(mu=1, bulk=2), axis=-1, nthreads=16)
solid_threaded = fem.SolidBody(umat_threaded, field)
umat = fem.NeoHooke(mu=1, bulk=2)
solid = fem.SolidBody(umat, field)
gives
>>> %timeit -r1 -n1 solid.assemble.matrix(field)
12.6 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
>>> %timeit -r1 -n1 solid_threaded.assemble.matrix(field)
10 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
from felupe.
Nice idea but it seems that the effort is not worth it.
from felupe.
Related Issues (20)
- Sort principal values before plotting
- Launch Binder badge does not start
- Add `SolidBody.assemble.mass(density=1.0)`
- Fix the doctests and add it to the code coverage report
- Implement optimization of constitutive material parameters HOT 1
- `MaterialStrain.plot()` shows wrong preview
- `NeoHookeCompressible(mu=1, lmbda=None)`
- The `constitution` module should be splitted and re-organized
- The Alexander-model is incomplete
- The Ogden-Roxburgh model should be also implemented with automatic differentiation
- The optimization of material parameters should optionally be based on relative residuals
- Collection of Ideas
- Deprecate `MaterialAD` and use `Hyperelastic` everywhere instead HOT 2
- The material parameters are overwritten in-place in `ConstitutiveMaterial.optimize()`
- Material-parameter scalars are converted to arrays in `ConstitutiveMaterial.optimize()`
- Add the MORPH X material model
- The state variables are not-resetted after finishing a load case in the plot of a constitutive material
- Prepare for Numpy 2.0
- Enhance the performance of uniform grid meshes
- `FormItem()` needs an `update()` method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from felupe.