Skip to content

Commit ed78795

Browse files
committed
Add some 1d changes
1 parent ee1a7be commit ed78795

5 files changed

Lines changed: 32 additions & 33 deletions

File tree

diffmpm/element.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,13 @@ class Linear1D(_Element):
339339
def __init__(
340340
self,
341341
nelements: int,
342+
total_elements,
342343
el_len: float,
343344
constraints: List[Tuple[jnp.ndarray, Constraint]],
344345
nodes: Nodes = None,
346+
concentrated_nodal_forces=[],
347+
initialized=None,
348+
volume=None,
345349
):
346350
"""Initialize Linear1D.
347351
@@ -367,6 +371,12 @@ def __init__(
367371

368372
# self.boundary_nodes = boundary_nodes
369373
self.constraints = constraints
374+
self.concentrated_nodal_forces = concentrated_nodal_forces
375+
if initialized is None:
376+
self.volume = jnp.ones((self.total_elements, 1, 1))
377+
else:
378+
self.volume = volume
379+
self.initialized = True
370380

371381
def id_to_node_ids(self, id: int):
372382
"""
@@ -498,7 +508,7 @@ def f(x):
498508
ids[0] == ids[1], ids[0], jnp.ones_like(ids[0]) * -1
499509
)
500510

501-
def compute_volume(self):
511+
def compute_volume(self, *args):
502512
vol = jnp.ediff1d(self.nodes.loc)
503513
self.volume = jnp.ones((self.total_elements, 1, 1)) * vol
504514

diffmpm/particle.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,6 @@ def _compute_strain_rate(self, dn_dx: jnp.ndarray, elements: _Element):
272272
self.element_ids
273273
) # (nparticles, 2, 1)
274274

275-
# db.print(f"mapped_val[3]: {mapped_vel[3, :, 0]}")
276-
# TODO: This will need to change to be more general for ndim.
277-
# breakpoint()
278-
# L = jnp.einsum("ijk, ikj -> ijk", dn_dx, mapped_vel.squeeze(-1)).sum(
279-
# axis=2
280-
# )
281-
# strain_rate = strain_rate.at[:, 0, :].add(L)
282-
283-
# For 2d
284275
temp = mapped_vel.squeeze(2)
285276

286277
def _step(pid, args):

examples/input_1d.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ dimension = 1
1717
scheme = "usl"
1818
dt = 0.01
1919
nsteps = 1000
20-
gravity = 0
21-
velocity_update = True
20+
velocity_update = true
2221

2322
[output]
24-
type = "hdf5"
25-
file = "results/example_1d_out.hdf5"
26-
step_frequency = 5
23+
format = "npz"
24+
folder = "examples/1d_ex/results/"
25+
step_frequency = 1
2726

2827
[mesh]
2928
# type = "file"
@@ -52,3 +51,6 @@ type = "SimpleMaterial"
5251
file = "examples/particles-1d.json"
5352
material_id = 0
5453
init_velocity = 0.1
54+
55+
[external_loading]
56+
gravity = 0

examples/optim_1d.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
from diffmpm.material import SimpleMaterial
66
from diffmpm.mesh import Mesh1D
77
from diffmpm.particle import Particles
8+
from diffmpm.constraint import Constraint
89
from diffmpm.solver import MPMExplicit
910
from jax import value_and_grad, grad, jit
1011
from tqdm import tqdm
1112

1213
E_true = 100
1314
material = SimpleMaterial({"E": E_true, "density": 1})
14-
elements = Linear1D(1, 1, jnp.array([0]))
15-
particles = Particles(
16-
jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0])
17-
)
15+
cons = [(jnp.array([0]), Constraint(0, 0.0))]
16+
elements = Linear1D(1, 1, 1, cons)
17+
particles = Particles(jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0]))
1818
b1 = jnp.pi * 0.5
1919
particles.velocity += 0.1
20-
particles.set_mass_volume(1.0)
20+
# particles.set_mass_volume(1.0)
2121
dt = 0.01
2222
nsteps = 1000
2323
mesh = Mesh1D({"particles": [particles], "elements": elements})
@@ -60,20 +60,17 @@ def optax_adam(params, niter, mpm, target_vel):
6060
return param_list, loss_list
6161

6262

63-
params = 107.5
63+
params = 105.0
6464
material = SimpleMaterial({"E": params, "density": 1})
65-
elements = Linear1D(1, 1, jnp.array([0]))
66-
particles = Particles(
67-
jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0])
68-
)
65+
cons = [(jnp.array([0]), Constraint(0, 0.0))]
66+
elements = Linear1D(1, 1, 1, cons)
67+
particles = Particles(jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0]))
6968
particles.velocity += 0.1
7069
particles.set_mass_volume(1.0)
7170
mesh = Mesh1D({"particles": [particles], "elements": elements})
7271

7372
mpm = MPMExplicit(mesh, dt, scheme="usl")
74-
param_list, loss_list = optax_adam(
75-
params, 400, mpm, target_vel
76-
) # ADAM optimizer
73+
param_list, loss_list = optax_adam(params, 400, mpm, target_vel) # ADAM optimizer
7774
# print("E: {}".format(result))
7875

7976
fig, ax = plt.subplots(1, 2, figsize=(16, 6))

examples/simple_1d.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
E = 100
1111
material = SimpleMaterial({"E": E, "density": 1})
1212
cons = [(jnp.array([0]), Constraint(0, 0.0))]
13-
elements = Linear1D(1, 1, cons)
14-
particles = Particles(
15-
jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0])
16-
)
13+
elements = Linear1D(1, 1, 1, cons)
14+
particles = Particles(jnp.array([0.5]).reshape(1, 1, 1), material, jnp.array([0]))
1715
# b1 = jnp.pi * 0.5
1816
# velocity = 0.1 * jnp.sin(b1 * particles.loc)
1917
# particles.velocity = velocity
@@ -24,7 +22,7 @@
2422
mesh = Mesh1D({"particles": [particles], "elements": elements})
2523

2624
mpm = MPMExplicit(mesh, dt, scheme="usf", velocity_update=True)
27-
result = mpm.solve(nsteps, 0)
25+
result = mpm.solve_jit(nsteps, 0)
2826

2927

3028
def analytical_vibration(E, rho, v0, x_loc, L, dt, nsteps):
@@ -45,6 +43,7 @@ def analytical_vibration(E, rho, v0, x_loc, L, dt, nsteps):
4543
ta, va, xa = analytical_vibration(
4644
E=E, rho=1, v0=0.1, x_loc=0.5, nsteps=nsteps, dt=dt, L=1.0
4745
)
46+
breakpoint()
4847
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
4948
ax[0].plot(ta, va, "r", label="analytical")
5049
ax[0].plot(ta, result["velocity"].squeeze(), "ob", markersize=2, label="mpm")

0 commit comments

Comments
 (0)