Skip to content

fast_cd_sim

fast_cd_sim

Fast Complementary Dynamics Simulation, implementation of https://www.dgp.toronto.edu/projects/fast_complementary_dynamics_site/

Source code in src\fast_cody\fast_cd_sim.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class fast_cd_sim():
    """
    Fast Complementary Dynamics Simulation, implementation of https://www.dgp.toronto.edu/projects/fast_complementary_dynamics_site/
    """
    def __init__(self, V, T, B, l, J, mu=1e4, rho=1e3, h=1e-2, max_iters=30, threshold=1e-8,
                 read_cache=False, cache_dir="", Aeq=None, write_cache=False):
        """
        Initializes a Fast Complementary Dynamics Simulation.

        Parameters
        ----------
        V : (n, 3) float numpy array
            Vertex positions
        T : (F, 4) int numpy array
            Tet indices
        B : (3n, m) float numpy array
            Subspace matrix
        l : (F, 1) int numpy array
            Cluster labels
        J : (3n, 12b) float numpy array
            LBS rig jacobian
        mu : float 
            First lame parameter (default=1e5)
        rho : float
            Density (default=1)
        h : float
            Timestep (default=1e-2)
        max_iters : int
            Maximum number of iterations for the local-global solver (default=30)
        threshold : float
            Convergence threshold for local global solver (default=1e-8)
        read_cache : bool
            Whether to read simulation precomp from cache (default=False)
        cache_dir : str
            Directory to read/write cache from/to (default="")
        Aeq : (c, m) float numpy array
            Constraint matrix (default empty, untested yet)
        write_cache : bool
            Whether to write simulation precomp to cache (default=False) 
        """
        #These parameters need to be global member variables otherwise their memory is destroyed
        self.solver_params = fcd.local_global_solver_params(False, max_iters, threshold)
        if Aeq is None:
            self.Aeq = sp.sparse.csc_matrix((0, 0))
        self.Jsp = sp.sparse.csc_matrix(J)
        self.sim_params = fcd.fast_cd_arap_sim_params(V, T, B, l, self.Jsp, self.Aeq, mu, h, rho)

        write_cache = write_cache
        self.sim = fcd.fast_cd_arap_sim(cache_dir, self.sim_params, self.solver_params, read_cache, write_cache)


        return

    '''
    Steps simulation state forward
    Inputs:
        p - 12b x 1 next timestep rig parameters
        state - current fast_cd_state object
    Optional:
        f_ext - m x 1 external force (default=0)
        bc - c x 1 boundary constraints (default=None). Only valid if fast_cd_sim.Aeq is non-empty
        z - m x 1 first guess for local global solver
    Returns:
        z_next - m x 1 next timestep reduced space coefficients for sim   
    '''
    def step(self,  p, state, z=None, f_ext=None, bc=None):
        """
        Steps simulation state forward

        Parameters
        ----------
        p : (12b, 1) float numpy array
            Next state of the rig parameters
        state : fast_cd_state
            Current state of the simulation
        f_ext : (m, 1) float numpy array
            External force (default=0)
        bc : (c, 1) float numpy array
            Boundary constraints (default=None). Only valid if fast_cd_sim.Aeq is non-empty

        Returns
        -------
        z_next : (m, 1) float numpy array
            Next state of the reduced secondary motion sim
        """


        if f_ext is None:
            f_ext = np.zeros((state.z_curr.shape[0], 1))
        if bc is None:
            bc = np.array([[]], dtype=np.float64).T
        else:
            assert(bc.shape[0] == self.Aeq.shape[0])
        if z is None:
            z = state.z_curr
        assert(bc.shape[0] == self.Aeq.shape[0] and "Constraint rhs and matrix must have same number of rows")

        z = self.sim.step(z, p, state, f_ext, bc)[:, None]

        return z

__init__(V, T, B, l, J, mu=10000.0, rho=1000.0, h=0.01, max_iters=30, threshold=1e-08, read_cache=False, cache_dir='', Aeq=None, write_cache=False)

Initializes a Fast Complementary Dynamics Simulation.

Parameters:

Name Type Description Default
V (n, 3) float numpy array

Vertex positions

required
T (F, 4) int numpy array

Tet indices

required
B (3n, m) float numpy array

Subspace matrix

required
l (F, 1) int numpy array

Cluster labels

required
J (3n, 12b) float numpy array

LBS rig jacobian

required
mu float

First lame parameter (default=1e5)

10000.0
rho float

Density (default=1)

1000.0
h float

Timestep (default=1e-2)

0.01
max_iters int

Maximum number of iterations for the local-global solver (default=30)

30
threshold float

Convergence threshold for local global solver (default=1e-8)

1e-08
read_cache bool

Whether to read simulation precomp from cache (default=False)

False
cache_dir str

Directory to read/write cache from/to (default="")

''
Aeq (c, m) float numpy array

Constraint matrix (default empty, untested yet)

None
write_cache bool

Whether to write simulation precomp to cache (default=False)

False
Source code in src\fast_cody\fast_cd_sim.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def __init__(self, V, T, B, l, J, mu=1e4, rho=1e3, h=1e-2, max_iters=30, threshold=1e-8,
             read_cache=False, cache_dir="", Aeq=None, write_cache=False):
    """
    Initializes a Fast Complementary Dynamics Simulation.

    Parameters
    ----------
    V : (n, 3) float numpy array
        Vertex positions
    T : (F, 4) int numpy array
        Tet indices
    B : (3n, m) float numpy array
        Subspace matrix
    l : (F, 1) int numpy array
        Cluster labels
    J : (3n, 12b) float numpy array
        LBS rig jacobian
    mu : float 
        First lame parameter (default=1e5)
    rho : float
        Density (default=1)
    h : float
        Timestep (default=1e-2)
    max_iters : int
        Maximum number of iterations for the local-global solver (default=30)
    threshold : float
        Convergence threshold for local global solver (default=1e-8)
    read_cache : bool
        Whether to read simulation precomp from cache (default=False)
    cache_dir : str
        Directory to read/write cache from/to (default="")
    Aeq : (c, m) float numpy array
        Constraint matrix (default empty, untested yet)
    write_cache : bool
        Whether to write simulation precomp to cache (default=False) 
    """
    #These parameters need to be global member variables otherwise their memory is destroyed
    self.solver_params = fcd.local_global_solver_params(False, max_iters, threshold)
    if Aeq is None:
        self.Aeq = sp.sparse.csc_matrix((0, 0))
    self.Jsp = sp.sparse.csc_matrix(J)
    self.sim_params = fcd.fast_cd_arap_sim_params(V, T, B, l, self.Jsp, self.Aeq, mu, h, rho)

    write_cache = write_cache
    self.sim = fcd.fast_cd_arap_sim(cache_dir, self.sim_params, self.solver_params, read_cache, write_cache)


    return

step(p, state, z=None, f_ext=None, bc=None)

Steps simulation state forward

Parameters:

Name Type Description Default
p (12b, 1) float numpy array

Next state of the rig parameters

required
state fast_cd_state

Current state of the simulation

required
f_ext (m, 1) float numpy array

External force (default=0)

None
bc (c, 1) float numpy array

Boundary constraints (default=None). Only valid if fast_cd_sim.Aeq is non-empty

None

Returns:

Name Type Description
z_next (m, 1) float numpy array

Next state of the reduced secondary motion sim

Source code in src\fast_cody\fast_cd_sim.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def step(self,  p, state, z=None, f_ext=None, bc=None):
    """
    Steps simulation state forward

    Parameters
    ----------
    p : (12b, 1) float numpy array
        Next state of the rig parameters
    state : fast_cd_state
        Current state of the simulation
    f_ext : (m, 1) float numpy array
        External force (default=0)
    bc : (c, 1) float numpy array
        Boundary constraints (default=None). Only valid if fast_cd_sim.Aeq is non-empty

    Returns
    -------
    z_next : (m, 1) float numpy array
        Next state of the reduced secondary motion sim
    """


    if f_ext is None:
        f_ext = np.zeros((state.z_curr.shape[0], 1))
    if bc is None:
        bc = np.array([[]], dtype=np.float64).T
    else:
        assert(bc.shape[0] == self.Aeq.shape[0])
    if z is None:
        z = state.z_curr
    assert(bc.shape[0] == self.Aeq.shape[0] and "Constraint rhs and matrix must have same number of rows")

    z = self.sim.step(z, p, state, f_ext, bc)[:, None]

    return z

fast_cd_state

Bases: cd_sim_state

Simulation state for a Fast Complementary Dynamics Simulation

For Fast CD, the state is comprised of 4 quantities: z_curr: the current state of the reduced secondary motion p_curr: the current state of the rig parameters z_prev: the previous state of the reduced secondary motion p_prev: the previous state of the rig parameters

Source code in src\fast_cody\fast_cd_sim.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class fast_cd_state(fcd.cd_sim_state):
    """
    Simulation state for a Fast Complementary Dynamics Simulation

    For Fast CD, the state is comprised of 4 quantities:
        z_curr: the current state of the reduced secondary motion
        p_curr: the current state of the rig parameters
        z_prev: the previous state of the reduced secondary motion
        p_prev: the previous state of the rig parameters
    """
    def __init__(self, z_curr, p_curr, z_prev=None, p_prev=None):
        """
        Sets current and previous simulation states

        Parameters
        ----------
        z_curr : (m, 1) float numpy array
            Current state of the reduced secondary motion sim
        p_curr : (12b, 1) float numpy array
            Current state of the rig parameters
        z_prev : (m, 1) float numpy array
            Previous state of the reduced secondary motion sim. If None, set to z_curr
        p_prev : (12b, 1) float numpy array
            Previous state of the rig parameter. If None, set to p_curr
        """
        z_curr = z_curr.copy()
        p_curr = p_curr.copy()

        if z_prev is None:
            z_prev = z_curr.copy()
        if p_prev is None:
            p_prev = p_curr.copy()

        super().__init__(z_curr, z_prev, p_curr, p_prev)


    '''
    Updates the simulation state
    Inputs:
    z - m x 1 reduced space coefficients for subspace sim
    p - 12b x 1 rig parameters
    '''
    def update(self, z, p):
        """
        Updates the simulation state

        Parameters
        ----------
        z : (m, 1) float numpy array
            Next state of the reduced secondary motion sim
        p : (12b, 1) float numpy array
            Next state of the rig parameters
        """
        self.z_prev = self.z_curr.copy()
        self.p_prev = self.p_curr.copy()

        self.z_curr = z.copy()
        self.p_curr = p.copy()

__init__(z_curr, p_curr, z_prev=None, p_prev=None)

Sets current and previous simulation states

Parameters:

Name Type Description Default
z_curr (m, 1) float numpy array

Current state of the reduced secondary motion sim

required
p_curr (12b, 1) float numpy array

Current state of the rig parameters

required
z_prev (m, 1) float numpy array

Previous state of the reduced secondary motion sim. If None, set to z_curr

None
p_prev (12b, 1) float numpy array

Previous state of the rig parameter. If None, set to p_curr

None
Source code in src\fast_cody\fast_cd_sim.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def __init__(self, z_curr, p_curr, z_prev=None, p_prev=None):
    """
    Sets current and previous simulation states

    Parameters
    ----------
    z_curr : (m, 1) float numpy array
        Current state of the reduced secondary motion sim
    p_curr : (12b, 1) float numpy array
        Current state of the rig parameters
    z_prev : (m, 1) float numpy array
        Previous state of the reduced secondary motion sim. If None, set to z_curr
    p_prev : (12b, 1) float numpy array
        Previous state of the rig parameter. If None, set to p_curr
    """
    z_curr = z_curr.copy()
    p_curr = p_curr.copy()

    if z_prev is None:
        z_prev = z_curr.copy()
    if p_prev is None:
        p_prev = p_curr.copy()

    super().__init__(z_curr, z_prev, p_curr, p_prev)

update(z, p)

Updates the simulation state

Parameters:

Name Type Description Default
z (m, 1) float numpy array

Next state of the reduced secondary motion sim

required
p (12b, 1) float numpy array

Next state of the rig parameters

required
Source code in src\fast_cody\fast_cd_sim.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def update(self, z, p):
    """
    Updates the simulation state

    Parameters
    ----------
    z : (m, 1) float numpy array
        Next state of the reduced secondary motion sim
    p : (12b, 1) float numpy array
        Next state of the rig parameters
    """
    self.z_prev = self.z_curr.copy()
    self.p_prev = self.p_curr.copy()

    self.z_curr = z.copy()
    self.p_curr = p.copy()