nablaengine 1.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. nablaengine/__init__.py +4 -0
  2. nablaengine/_fixtures/README.md +19 -0
  3. nablaengine/_fixtures/data/mf_s0.npz +0 -0
  4. nablaengine/_fixtures/mjlab/LICENSE +27 -0
  5. nablaengine/_fixtures/mjlab/README.md +113 -0
  6. nablaengine/_fixtures/mjlab/g1_meta.json +32 -0
  7. nablaengine/_fixtures/mjlab/g1_train.mjb +0 -0
  8. nablaengine/_optional.py +30 -0
  9. nablaengine/_paths.py +38 -0
  10. nablaengine/actuators/__init__.py +169 -0
  11. nablaengine/actuators/bam.py +306 -0
  12. nablaengine/actuators/bam_params/LICENSE +202 -0
  13. nablaengine/actuators/bam_params/xl330/m6.json +19 -0
  14. nablaengine/boxbox.py +259 -0
  15. nablaengine/ccd.py +1122 -0
  16. nablaengine/checks.py +376 -0
  17. nablaengine/cuda.py +483 -0
  18. nablaengine/dist_query.py +628 -0
  19. nablaengine/dual.py +223 -0
  20. nablaengine/dyn_query.py +685 -0
  21. nablaengine/engines.py +114 -0
  22. nablaengine/envs/__init__.py +24 -0
  23. nablaengine/envs/brax.py +165 -0
  24. nablaengine/envs/convert.py +771 -0
  25. nablaengine/envs/dmc.py +523 -0
  26. nablaengine/envs/env.py +233 -0
  27. nablaengine/envs/kin.py +371 -0
  28. nablaengine/envs/ops.py +134 -0
  29. nablaengine/envs/rewards.py +57 -0
  30. nablaengine/fixtures.py +243 -0
  31. nablaengine/forest_engine/__init__.py +0 -0
  32. nablaengine/forest_engine/adjoint.py +1726 -0
  33. nablaengine/forest_engine/adjoint_contact.py +493 -0
  34. nablaengine/forest_engine/bam_hooks.py +248 -0
  35. nablaengine/forest_engine/body_params.py +1007 -0
  36. nablaengine/forest_engine/capacity_diag.py +116 -0
  37. nablaengine/forest_engine/ccd_coop.py +250 -0
  38. nablaengine/forest_engine/chol_blk.py +80 -0
  39. nablaengine/forest_engine/contact_forces.py +142 -0
  40. nablaengine/forest_engine/contact_vjp.py +462 -0
  41. nablaengine/forest_engine/convex.py +1331 -0
  42. nablaengine/forest_engine/cylinder.py +73 -0
  43. nablaengine/forest_engine/dist_device.py +923 -0
  44. nablaengine/forest_engine/dyn_device.py +696 -0
  45. nablaengine/forest_engine/ellipsoid.py +179 -0
  46. nablaengine/forest_engine/elliptic.py +272 -0
  47. nablaengine/forest_engine/equality.py +842 -0
  48. nablaengine/forest_engine/floss.py +148 -0
  49. nablaengine/forest_engine/foot_offset.py +368 -0
  50. nablaengine/forest_engine/free_gyro.py +288 -0
  51. nablaengine/forest_engine/host.py +530 -0
  52. nablaengine/forest_engine/kdof64.py +127 -0
  53. nablaengine/forest_engine/kinematics.py +320 -0
  54. nablaengine/forest_engine/margin.py +167 -0
  55. nablaengine/forest_engine/margin_convex.py +326 -0
  56. nablaengine/forest_engine/midphase.py +439 -0
  57. nablaengine/forest_engine/model_hooks.py +913 -0
  58. nablaengine/forest_engine/motor_hooks.py +50 -0
  59. nablaengine/forest_engine/muscle.py +485 -0
  60. nablaengine/forest_engine/nb128.py +118 -0
  61. nablaengine/forest_engine/nb64.py +163 -0
  62. nablaengine/forest_engine/noslip.py +1124 -0
  63. nablaengine/forest_engine/nv128.py +1944 -0
  64. nablaengine/forest_engine/object_size.py +283 -0
  65. nablaengine/forest_engine/replay_guard.py +40 -0
  66. nablaengine/forest_engine/root_spread.py +126 -0
  67. nablaengine/forest_engine/solref.py +74 -0
  68. nablaengine/forest_engine/source.py +1464 -0
  69. nablaengine/forest_engine/sparse_pattern.py +527 -0
  70. nablaengine/forest_engine/spatial.py +1045 -0
  71. nablaengine/forest_engine/step.py +3083 -0
  72. nablaengine/forest_engine/surrogate.py +498 -0
  73. nablaengine/forest_engine/tape.py +199 -0
  74. nablaengine/forest_engine/tendon.py +845 -0
  75. nablaengine/forest_engine/terrain.py +1059 -0
  76. nablaengine/forest_engine/world_forces.py +166 -0
  77. nablaengine/forest_engine/world_params.py +230 -0
  78. nablaengine/g1_engine/__init__.py +0 -0
  79. nablaengine/g1_engine/adjoint.py +1661 -0
  80. nablaengine/g1_engine/adjoint_contact.py +577 -0
  81. nablaengine/g1_engine/bam_hooks.py +596 -0
  82. nablaengine/g1_engine/condim4.py +245 -0
  83. nablaengine/g1_engine/contact_forces.py +500 -0
  84. nablaengine/g1_engine/cylccd.py +1158 -0
  85. nablaengine/g1_engine/cylinder.py +216 -0
  86. nablaengine/g1_engine/elliptic.py +565 -0
  87. nablaengine/g1_engine/exact_replay.py +61 -0
  88. nablaengine/g1_engine/host.py +358 -0
  89. nablaengine/g1_engine/kinematics.py +320 -0
  90. nablaengine/g1_engine/mesh.py +305 -0
  91. nablaengine/g1_engine/mesh_tables.py +79 -0
  92. nablaengine/g1_engine/model_hooks.py +919 -0
  93. nablaengine/g1_engine/motor_hooks.py +70 -0
  94. nablaengine/g1_engine/replay_guard.py +55 -0
  95. nablaengine/g1_engine/source.py +1107 -0
  96. nablaengine/g1_engine/sphbox.py +93 -0
  97. nablaengine/g1_engine/step.py +3716 -0
  98. nablaengine/g1_engine/tape.py +173 -0
  99. nablaengine/g1_engine/world_params.py +447 -0
  100. nablaengine/identity.py +401 -0
  101. nablaengine/jax_ffi.py +2536 -0
  102. nablaengine/model/__init__.py +1 -0
  103. nablaengine/model/capability.py +694 -0
  104. nablaengine/model/fields.py +317 -0
  105. nablaengine/model/gravcomp.py +64 -0
  106. nablaengine/model/invweight.py +145 -0
  107. nablaengine/motor.py +143 -0
  108. nablaengine/object_geometry.py +609 -0
  109. nablaengine/params.py +258 -0
  110. nablaengine/provenance.py +196 -0
  111. nablaengine/reference/__init__.py +0 -0
  112. nablaengine/reference/adjoint_groups.py +226 -0
  113. nablaengine/reference/jax64.py +690 -0
  114. nablaengine/reference/warp_physics.py +262 -0
  115. nablaengine/sensors/__init__.py +4 -0
  116. nablaengine/sensors/cameras.py +285 -0
  117. nablaengine/sensors/data/LIVOX_MID360_LICENSE +21 -0
  118. nablaengine/sensors/data/livox_mid360.npz +0 -0
  119. nablaengine/sensors/g1.py +521 -0
  120. nablaengine/sensors/jax_rays.py +364 -0
  121. nablaengine/sensors/optix_rays.py +725 -0
  122. nablaengine/sensors/raycore.py +630 -0
  123. nablaengine/sensors/raygrad.py +374 -0
  124. nablaengine/sensors/raygrad_cuda.py +191 -0
  125. nablaengine/sensors/robot_mesh.py +273 -0
  126. nablaengine/sensors/torch_rays.py +275 -0
  127. nablaengine/terrain.py +424 -0
  128. nablaengine/torch_ffi.py +1328 -0
  129. nablaengine/workdir.py +37 -0
  130. nablaengine-1.0.2.dist-info/METADATA +236 -0
  131. nablaengine-1.0.2.dist-info/RECORD +136 -0
  132. nablaengine-1.0.2.dist-info/WHEEL +5 -0
  133. nablaengine-1.0.2.dist-info/licenses/LICENSE +202 -0
  134. nablaengine-1.0.2.dist-info/licenses/NOTICE +23 -0
  135. nablaengine-1.0.2.dist-info/licenses/THIRD_PARTY_NOTICES.md +176 -0
  136. nablaengine-1.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,288 @@
1
+ """The gyroscopic velocity derivative of standalone free bodies in implicitfast (MuJoCo 3.17)."""
2
+ import mujoco
3
+ import numpy as np
4
+
5
+ FWD_ANCHOR = " if (rec) { // tape: L_Q = chol(M + h kv), a* and a_adv"
6
+ DEF_ANCHOR = "template <bool NARROW> struct ZWord { typedef unsigned T; };"
7
+ ADJ_Y_ANCHOR = " for (int i = lane; i < NV; i += 32) s.y[i] = s.abar[i];\n"
8
+ ADJ_COMPOSE_ANCHOR = "__device__ void adj_compose(Adj& s, int lane) {\n"
9
+ ADJ_COMPOSE_MOTOR = "__device__ void adj_compose(Adj& s, int lane, const float* rec) {\n"
10
+ ADJ_DEF_ANCHOR = "// A1 / A2, from abar = a_adv bar: y = Q^-1 a_adv bar (L_Q)"
11
+
12
+
13
+ def bodies(m):
14
+ """The standalone free bodies (free joint, tree of 6 dofs) of an implicitfast model, ascending; [] otherwise."""
15
+ if int(m.opt.integrator) != int(mujoco.mjtIntegrator.mjINT_IMPLICITFAST):
16
+ return []
17
+ out = []
18
+ for b in range(1, m.nbody):
19
+ if int(m.body_jntnum[b]) != 1:
20
+ continue
21
+ j = int(m.body_jntadr[b])
22
+ if int(m.jnt_type[j]) != int(mujoco.mjtJoint.mjJNT_FREE):
23
+ continue
24
+ if int(m.tree_dofnum[int(m.dof_treeid[int(m.jnt_dofadr[j])])]) == 6:
25
+ out.append(b)
26
+ return out
27
+
28
+
29
+ def applies(m):
30
+ return bool(bodies(m))
31
+
32
+
33
+ def check(m):
34
+ """ValueError for a tendon with damping, or with an actuator whose kv is not 0, whose support includes a standalone
35
+ free body's dofs (their implicit terms are added outside M; the block would miss them)."""
36
+ if not applies(m):
37
+ return
38
+ for b in bodies(m):
39
+ kids = [k for k in range(1, m.nbody) if int(m.body_parentid[k]) == b]
40
+ if kids:
41
+ raise ValueError(f"body {b}: a standalone free body with jointless child bodies {kids} (forest_engine."
42
+ "free_gyro takes the body's own inertials; fuse them, nablaengine.envs.convert)")
43
+ from nablaengine.forest_engine import tendon
44
+ dofs = set()
45
+ for b in bodies(m):
46
+ d0 = int(m.body_dofadr[b])
47
+ dofs |= set(range(d0, d0 + 6))
48
+ from nablaengine.forest_engine import muscle
49
+ acts = set()
50
+ for a, t in tendon.actuators(m):
51
+ if muscle.is_general(m, a):
52
+ kv = muscle.plain_constants(m, a)[2]
53
+ else:
54
+ kv = -m.actuator_biasprm[a, 2] if int(m.actuator_biastype[a]) == int(mujoco.mjtBias.mjBIAS_AFFINE) else 0.0
55
+ if kv != 0:
56
+ acts.add(t)
57
+ for t in range(m.ntendon):
58
+ if not (tendon.damping(m, t) > 0 or t in acts):
59
+ continue
60
+ sup = {d for d, _ in tendon.support_list(m, t)}
61
+ if sup & dofs:
62
+ raise ValueError(f"tendon {t}: damping or an actuator on a standalone free body's dofs under implicitfast "
63
+ "(forest_engine.free_gyro: the body's implicit block does not include it)")
64
+
65
+
66
+ def _f(x):
67
+ return f"{float(np.float32(x))!r}f"
68
+
69
+
70
+ def constants(m):
71
+ """Per body: mass, COM offset (3, body frame), inertia about the COM in the body frame (xx yy zz xy xz yz)."""
72
+ out = []
73
+ for b in bodies(m):
74
+ q = m.body_iquat[b]
75
+ R = np.zeros(9)
76
+ mujoco.mju_quat2Mat(R, q)
77
+ R = R.reshape(3, 3)
78
+ I = R @ np.diag(m.body_inertia[b]) @ R.T
79
+ out.append([m.body_mass[b], *m.body_ipos[b], I[0, 0], I[1, 1], I[2, 2], I[0, 1], I[0, 2], I[1, 2]])
80
+ return np.asarray(out, np.float64)
81
+
82
+
83
+ def _defs(m, damp):
84
+ bs = bodies(m)
85
+ c = constants(m)
86
+ dof = ", ".join(str(int(m.body_dofadr[b])) for b in bs)
87
+ qad = ", ".join(str(int(m.jnt_qposadr[m.body_jntadr[b]])) for b in bs)
88
+ vals = ",\n ".join(", ".join(_f(x) for x in row) for row in c)
89
+ return rf"""#ifndef DK_FREE_GYRO
90
+ #define DK_FREE_GYRO 1 // (forest_engine.free_gyro; sm_90: nablaengine.cuda.forest_hold_defines)
91
+ #endif
92
+ #if DK_FREE_GYRO
93
+ // forest_engine.free_gyro: the standalone free bodies (MuJoCo 3.17's implicit free-body block): dof and qpos
94
+ // addresses, mass, COM offset, inertia about the COM (body frame: xx yy zz xy xz yz)
95
+ #define NFG {len(bs)}
96
+ __device__ const int c_fg_dof[NFG] = {{{dof}}}, c_fg_qadr[NFG] = {{{qad}}};
97
+ __device__ const float c_fg[NFG * 10] = {{
98
+ {vals}}};
99
+ #define FG_DAMP(s, d) ({damp})
100
+
101
+ // R (row-major) of the normalized quaternion q
102
+ __device__ __forceinline__ void fg_rot(const float* q, float* R) {{
103
+ const float n = rsqrtf(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
104
+ const float Q[4] = {{q[0] * n, q[1] * n, q[2] * n, q[3] * n}};
105
+ for (int k = 0; k < 3; k++) {{
106
+ float e[3] = {{0.f, 0.f, 0.f}}, c[3];
107
+ e[k] = 1.f;
108
+ qrot(Q, e, c);
109
+ R[k] = c[0]; R[3 + k] = c[1]; R[6 + k] = c[2];
110
+ }}
111
+ }}
112
+
113
+ // body f's K(w) = s w' - (w . s) I + [w x s]x and C(w) = -m [s]x K + [w]x I_c - [I_c w]x (body frame, row-major)
114
+ __device__ __forceinline__ void fg_kc(int f, const float* w, float* K, float* C) {{
115
+ const float* p = c_fg + 10 * f;
116
+ const float m = p[0], s[3] = {{p[1], p[2], p[3]}};
117
+ const float I[9] = {{p[4], p[7], p[8], p[7], p[5], p[9], p[8], p[9], p[6]}};
118
+ const float ws = w[0] * s[0] + w[1] * s[1] + w[2] * s[2];
119
+ const float x[3] = {{w[1] * s[2] - w[2] * s[1], w[2] * s[0] - w[0] * s[2], w[0] * s[1] - w[1] * s[0]}}; // w x s
120
+ for (int i = 0; i < 3; i++)
121
+ for (int j = 0; j < 3; j++) K[3 * i + j] = s[i] * w[j] - (i == j ? ws : 0.f);
122
+ K[1] -= x[2]; K[2] += x[1]; K[3] += x[2]; K[5] -= x[0]; K[6] -= x[1]; K[7] += x[0];
123
+ float Iw[3];
124
+ for (int i = 0; i < 3; i++) Iw[i] = I[3 * i] * w[0] + I[3 * i + 1] * w[1] + I[3 * i + 2] * w[2];
125
+ for (int j = 0; j < 3; j++) {{
126
+ const float k0 = K[j], k1 = K[3 + j], k2 = K[6 + j];
127
+ const float i0 = I[j], i1 = I[3 + j], i2 = I[6 + j];
128
+ C[j] = -m * (s[1] * k2 - s[2] * k1) + (w[1] * i2 - w[2] * i1);
129
+ C[3 + j] = -m * (s[2] * k0 - s[0] * k2) + (w[2] * i0 - w[0] * i2);
130
+ C[6 + j] = -m * (s[0] * k1 - s[1] * k0) + (w[0] * i1 - w[1] * i0);
131
+ }}
132
+ C[1] += Iw[2]; C[2] -= Iw[1]; C[3] -= Iw[2]; C[5] += Iw[0]; C[6] += Iw[1]; C[7] -= Iw[0]; // - [I_c w]x
133
+ }}
134
+
135
+ // body f's h G columns 3..5 (6 x 3, row-major) at the local angular velocity w and rotation R
136
+ __device__ __forceinline__ void fg_hg(int f, const float* w, const float* R, float h, float* G) {{
137
+ float K[9], C[9];
138
+ fg_kc(f, w, K, C);
139
+ const float hm = -h * c_fg[10 * f];
140
+ for (int i = 0; i < 3; i++)
141
+ for (int j = 0; j < 3; j++) {{
142
+ G[3 * i + j] = hm * (R[3 * i] * K[j] + R[3 * i + 1] * K[3 + j] + R[3 * i + 2] * K[6 + j]);
143
+ G[9 + 3 * i + j] = h * C[3 * i + j];
144
+ }}
145
+ }}
146
+
147
+ // forward: body f's 6 entries of a_adv = A^-1 (M a*), A = M6 + h damping + h G (LU with partial pivoting)
148
+ template <class S>
149
+ __device__ __forceinline__ void fg_solve(S& s, int f, float h) {{
150
+ const int d0 = c_fg_dof[f];
151
+ float A[6][7], R[9], G[18];
152
+ for (int i = 0; i < 6; i++) {{
153
+ for (int j = 0; j < 6; j++) A[i][j] = s.M[i >= j ? TRI(d0 + i, d0 + j) : TRI(d0 + j, d0 + i)];
154
+ A[i][i] += h * FG_DAMP(s, d0 + i);
155
+ A[i][6] = s.Ma[d0 + i];
156
+ }}
157
+ fg_rot(s.qpos + c_fg_qadr[f] + 3, R);
158
+ const float w[3] = {{s.qvel[d0 + 3], s.qvel[d0 + 4], s.qvel[d0 + 5]}};
159
+ fg_hg(f, w, R, h, G);
160
+ for (int i = 0; i < 6; i++)
161
+ for (int j = 0; j < 3; j++) A[i][3 + j] += G[3 * i + j];
162
+ for (int k = 0; k < 6; k++) {{
163
+ int p = k;
164
+ for (int i = k + 1; i < 6; i++)
165
+ if (fabsf(A[i][k]) > fabsf(A[p][k])) p = i;
166
+ if (p != k)
167
+ for (int j = k; j < 7; j++) {{ const float t = A[k][j]; A[k][j] = A[p][j]; A[p][j] = t; }}
168
+ const float inv = 1.f / A[k][k];
169
+ for (int i = k + 1; i < 6; i++) {{
170
+ const float l = A[i][k] * inv;
171
+ for (int j = k + 1; j < 7; j++) A[i][j] -= l * A[k][j];
172
+ }}
173
+ }}
174
+ for (int i = 5; i >= 0; i--) {{
175
+ float x = A[i][6];
176
+ for (int j = i + 1; j < 6; j++) x -= A[i][j] * A[j][6];
177
+ A[i][6] = x / A[i][i];
178
+ }}
179
+ for (int i = 0; i < 6; i++) s.tmp[d0 + i] = A[i][6];
180
+ }}
181
+ #endif
182
+ """
183
+
184
+
185
+ ADJ_DEVICE = r"""#if DK_FREE_GYRO
186
+ // adjoint (forest_engine.free_gyro), A1: columns k of Z = Q0^-1 (h G_c) of every body in zk (k = 0, 1, 2), then
187
+ // u = Q0 y from (I + h Z)' u = a_adv bar, written over a_adv bar's angular entries (lane f: body f)
188
+ __device__ __forceinline__ void fg_adj_cols(Adj& s, float* z0, float* z1, float* z2, int lane) {
189
+ const float h = c_misc[0];
190
+ for (int i = lane; i < NV; i += 32) { z0[i] = 0.f; z1[i] = 0.f; z2[i] = 0.f; }
191
+ __syncwarp();
192
+ for (int f = lane; f < NFG; f += 32) {
193
+ const int d0 = c_fg_dof[f];
194
+ float R[9], G[18];
195
+ fg_rot(s.q + c_fg_qadr[f] + 3, R);
196
+ const float w[3] = {s.v[d0 + 3], s.v[d0 + 4], s.v[d0 + 5]};
197
+ fg_hg(f, w, R, h, G);
198
+ for (int i = 0; i < 6; i++) { z0[d0 + i] = G[3 * i]; z1[d0 + i] = G[3 * i + 1]; z2[d0 + i] = G[3 * i + 2]; }
199
+ }
200
+ __syncwarp();
201
+ }
202
+ __device__ __forceinline__ void fg_adj_u(Adj& s, const float* z0, const float* z1, const float* z2, int lane) {
203
+ for (int f = lane; f < NFG; f += 32) {
204
+ const int d0 = c_fg_dof[f];
205
+ const float* z[3] = {z0 + d0, z1 + d0, z2 + d0};
206
+ float r[3], B[9]; // B = I + Z_r' (3 x 3), r = a_adv bar_r - Z_l' a_adv bar_l
207
+ for (int k = 0; k < 3; k++) {
208
+ r[k] = s.abar[d0 + 3 + k] - (z[k][0] * s.abar[d0] + z[k][1] * s.abar[d0 + 1] + z[k][2] * s.abar[d0 + 2]);
209
+ for (int j = 0; j < 3; j++) B[3 * k + j] = (k == j ? 1.f : 0.f) + z[k][3 + j];
210
+ }
211
+ const float c0 = B[4] * B[8] - B[5] * B[7], c1 = B[5] * B[6] - B[3] * B[8], c2 = B[3] * B[7] - B[4] * B[6];
212
+ const float idet = 1.f / (B[0] * c0 + B[1] * c1 + B[2] * c2);
213
+ const float u0 = (r[0] * c0 + r[1] * (B[2] * B[7] - B[1] * B[8]) + r[2] * (B[1] * B[5] - B[2] * B[4])) * idet;
214
+ const float u1 = (r[0] * c1 + r[1] * (B[0] * B[8] - B[2] * B[6]) + r[2] * (B[2] * B[3] - B[0] * B[5])) * idet;
215
+ const float u2 = (r[0] * c2 + r[1] * (B[1] * B[6] - B[0] * B[7]) + r[2] * (B[0] * B[4] - B[1] * B[3])) * idet;
216
+ s.abar[d0 + 3] = u0; s.abar[d0 + 4] = u1; s.abar[d0 + 5] = u2;
217
+ }
218
+ __syncwarp();
219
+ }
220
+ // adjoint, A8: P_v += h d(y' G a_adv)/dw, P_q += h d(y' G a_adv)/d(theta) of body f (y = -ny, a_adv = a* - a~)
221
+ __device__ __forceinline__ void fg_adj_compose(Adj& s, int lane) {
222
+ const float h = c_misc[0];
223
+ for (int f = lane; f < NFG; f += 32) {
224
+ const int d0 = c_fg_dof[f];
225
+ const float m = c_fg[10 * f];
226
+ float R[9], K[9], C[9], y[6], a[3], u[3];
227
+ fg_rot(s.q + c_fg_qadr[f] + 3, R);
228
+ for (int i = 0; i < 6; i++) y[i] = -s.ny[d0 + i];
229
+ for (int k = 0; k < 3; k++) a[k] = s.a[d0 + 3 + k] - s.at[d0 + 3 + k];
230
+ for (int k = 0; k < 3; k++) u[k] = R[k] * y[0] + R[3 + k] * y[1] + R[6 + k] * y[2]; // R' y_l
231
+ for (int k = 0; k < 3; k++) { // d/dw_k: G is linear in w
232
+ float e[3] = {0.f, 0.f, 0.f};
233
+ e[k] = 1.f;
234
+ fg_kc(f, e, K, C);
235
+ float g = 0.f;
236
+ for (int i = 0; i < 3; i++) {
237
+ const float Ka = K[3 * i] * a[0] + K[3 * i + 1] * a[1] + K[3 * i + 2] * a[2];
238
+ const float Ca = C[3 * i] * a[0] + C[3 * i + 1] * a[1] + C[3 * i + 2] * a[2];
239
+ g += -m * u[i] * Ka + y[3 + i] * Ca;
240
+ }
241
+ s.Pv[d0 + 3 + k] += h * g;
242
+ }
243
+ const float w[3] = {s.v[d0 + 3], s.v[d0 + 4], s.v[d0 + 5]};
244
+ fg_kc(f, w, K, C);
245
+ float Ka[3];
246
+ for (int i = 0; i < 3; i++) Ka[i] = K[3 * i] * a[0] + K[3 * i + 1] * a[1] + K[3 * i + 2] * a[2];
247
+ const float hm = -h * m; // d/d(theta) of -m y_l' R [theta]x K a: -m (K a) x (R' y_l)
248
+ s.Pq[d0 + 3] += hm * (Ka[1] * u[2] - Ka[2] * u[1]);
249
+ s.Pq[d0 + 4] += hm * (Ka[2] * u[0] - Ka[0] * u[2]);
250
+ s.Pq[d0 + 5] += hm * (Ka[0] * u[1] - Ka[1] * u[0]);
251
+ }
252
+ __syncwarp();
253
+ }
254
+ #endif
255
+ """
256
+
257
+
258
+ def _once(src, a, b):
259
+ n = src.count(a)
260
+ if n != 1:
261
+ raise RuntimeError(f"free_gyro: anchor found {n} times: {a[:70]!r}")
262
+ return src.replace(a, b)
263
+
264
+
265
+ def apply(src, m):
266
+ """The edits for a model with standalone free bodies under implicitfast (bodies()); the text unchanged otherwise.
267
+ On the final text (after world_params: a per-world damping build reads s.dampw)."""
268
+ if not applies(m):
269
+ return src
270
+ check(m)
271
+ damp = "s.dampw[d]" if "s.dampw[i]" in src else "c_damp[d]"
272
+ src = _once(src, DEF_ANCHOR, _defs(m, damp.replace("[d]", "[(d)]")) + DEF_ANCHOR)
273
+ src = _once(src, ADJ_DEF_ANCHOR, ADJ_DEVICE + ADJ_DEF_ANCHOR)
274
+ src = _once(src, FWD_ANCHOR, "#if DK_FREE_GYRO // (forest_engine.free_gyro: a_adv of the standalone free bodies)\n"
275
+ " __syncwarp();\n for (int f = lane; f < NFG; f += 32) fg_solve(s, f, h);\n __syncwarp();\n#endif\n"
276
+ + FWD_ANCHOR)
277
+ i = src.index(ADJ_Y_ANCHOR) + len(ADJ_Y_ANCHOR)
278
+ line = src[i:src.index("\n", i) + 1]
279
+ if "s.L" not in line or "s.y" not in line:
280
+ raise RuntimeError(f"free_gyro: adj_solves' solve line: {line!r}")
281
+ solves = "".join(line.replace("s.y", z) for z in ("s.y", "s.lam", "s.ny"))
282
+ src = _once(src, ADJ_Y_ANCHOR, "#if DK_FREE_GYRO // (forest_engine.free_gyro: a_adv bar -> u = Q0 y)\n"
283
+ " fg_adj_cols(s, s.y, s.lam, s.ny, lane);\n" + solves + " __syncwarp();\n"
284
+ " fg_adj_u(s, s.y, s.lam, s.ny, lane);\n#endif\n" + ADJ_Y_ANCHOR)
285
+ head = ADJ_COMPOSE_ANCHOR if ADJ_COMPOSE_ANCHOR in src else ADJ_COMPOSE_MOTOR
286
+ src = _once(src, head, head + "#if DK_FREE_GYRO // (forest_engine.free_gyro)\n"
287
+ " fg_adj_compose(s, lane);\n#endif\n")
288
+ return src