mirror of
https://gitlab.com/pimnelissen/entropic-spring.git
synced 2025-11-30 19:33:07 +01:00
minimal code for part 1.
This commit is contained in:
35
RubberBand.py
Normal file
35
RubberBand.py
Normal file
@ -0,0 +1,35 @@
|
||||
import numpy as np
|
||||
|
||||
from Link import Link
|
||||
|
||||
class RubberBand:
|
||||
def __init__(self, N, a=1.):
|
||||
"""
|
||||
RubberBand is a class that can simulate a rubber band with N
|
||||
incompressible links of length a.
|
||||
|
||||
Parameters:
|
||||
N (int) The number of links.
|
||||
a (float) The fixed length of links.
|
||||
"""
|
||||
|
||||
self.N = N
|
||||
self.a = a
|
||||
self.links = self.__sample_links()
|
||||
|
||||
@property
|
||||
def length(self):
|
||||
return self.a * np.sum([l.direction for l in self.links])
|
||||
|
||||
def __sample_links(self):
|
||||
"""
|
||||
Sample N Link objects with a random direction.
|
||||
"""
|
||||
|
||||
samples = np.random.random_sample((self.N,))
|
||||
directions = np.ones(samples.shape)
|
||||
directions[samples < 0.5] = -1.
|
||||
directions = directions.astype(int)
|
||||
return [Link(d) for d in directions]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user