Skip to content

Commit 3f29aa6

Browse files
committed
Define Rigid solver error codes as enum.
1 parent f5261d2 commit 3f29aa6

File tree

8 files changed

+74
-62
lines changed

8 files changed

+74
-62
lines changed

genesis/engine/solvers/rigid/abd/diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def func_copy_next_to_curr(
5151
for i_q in range(n_qs):
5252
rigid_global_info.qpos[i_q, i_b] = rigid_global_info.qpos_next[i_q, i_b]
5353
else:
54-
errno[i_b] = errno[i_b] | 0b00000000000000000000000000001000
54+
errno[i_b] = errno[i_b] | array_class.ErrorCode.INVALID_ACC_NAN
5555

5656

5757
@ti.func

genesis/engine/solvers/rigid/abd/forward_kinematics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ def func_hibernate__for_all_awake_islands_either_hiberanate_or_update_aabb_sort_
12411241

12421242
# Invariant check: ensure entity_id access won't exceed buffer
12431243
if entity_ref_start + entity_ref_n > contact_island_state.entity_id.shape[0]:
1244-
errno[i_b] = errno[i_b] | 0b00000000000000000000000000010000
1244+
errno[i_b] = errno[i_b] | array_class.ErrorCode.OVERFLOW_HIBERNATION_ISLANDS
12451245
continue
12461246

12471247
for i_entity_ref_offset_ in range(entity_ref_n):

genesis/engine/solvers/rigid/collider/broadphase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def func_broad_phase(
294294
continue
295295

296296
if n_broad == collider_info.max_collision_pairs_broad[None]:
297-
errno[i_b] = errno[i_b] | 0b00000000000000000000000000000001
297+
errno[i_b] = errno[i_b] | array_class.ErrorCode.OVERFLOW_CANDIDATE_CONTACTS
298298
break
299299
collider_state.broad_collision_pairs[n_broad, i_b][0] = i_ga
300300
collider_state.broad_collision_pairs[n_broad, i_b][1] = i_gb

genesis/engine/solvers/rigid/collider/contact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def func_add_contact(
196196

197197
collider_state.n_contacts[i_b] = i_c + 1
198198
else:
199-
errno[i_b] = errno[i_b] | 0b00000000000000000000000000000010
199+
errno[i_b] = errno[i_b] | array_class.ErrorCode.OVERFLOW_COLLISION_PAIRS
200200

201201

202202
@ti.func

genesis/engine/solvers/rigid/collider/contact_island.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def add_edge(self, link_a, link_b, i_b):
100100
self.entity_edge.n[eb, i_b] = self.entity_edge.n[eb, i_b] + 1
101101
else:
102102
# Signal buffer overflow via errno bit 4 (0b00010000)
103-
self.errno[i_b] = self.errno[i_b] | 0b00000000000000000000000000010000
103+
self.errno[i_b] = self.errno[i_b] | array_class.ErrorCode.OVERFLOW_HIBERNATION_ISLANDS
104104

105105
@ti.kernel
106106
def add_contact_edges_to_islands(self):
@@ -205,7 +205,7 @@ def preprocess_island_and_map_entities_to_edges(self):
205205

206206
# Invariant check: ensure total half-edges don't exceed edge_id buffer
207207
if entity_list_start > self.edge_id.shape[0]:
208-
self.errno[i_b] = self.errno[i_b] | 0b00000000000000000000000000010000
208+
self.errno[i_b] = self.errno[i_b] | array_class.ErrorCode.OVERFLOW_HIBERNATION_ISLANDS
209209

210210
# process added collider-info edges
211211
for i in range(self.n_edges[i_b]):

genesis/engine/solvers/rigid/constraint/solver.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import genesis as gs
88
import genesis.utils.geom as gu
99
import genesis.utils.array_class as array_class
10+
from genesis.engine.solvers.rigid.abd import func_solve_mass_batch
11+
from genesis.utils.misc import ti_to_torch
12+
1013
from . import backward as backward_constraint_solver
11-
import genesis.engine.solvers.rigid.rigid_solver as rigid_solver
1214
from . import noslip as constraint_noslip
1315
from ..collider.contact_island import ContactIsland
14-
from genesis.utils.misc import ti_to_torch
1516

1617
if TYPE_CHECKING:
1718
from genesis.engine.solvers.rigid.rigid_solver import RigidSolver
@@ -2352,7 +2353,7 @@ def func_update_gradient_batch(
23522353
)
23532354

23542355
if ti.static(static_rigid_sim_config.solver_type == gs.constraint_solver.CG):
2355-
rigid_solver.func_solve_mass_batch(
2356+
func_solve_mass_batch(
23562357
i_b,
23572358
constraint_state.grad,
23582359
constraint_state.Mgrad,
@@ -2388,7 +2389,7 @@ def func_update_gradient_tiled(
23882389
if ti.static(static_rigid_sim_config.solver_type == gs.constraint_solver.CG):
23892390
ti.loop_config(serialize=static_rigid_sim_config.para_level < gs.PARA_LEVEL.ALL, block_dim=32)
23902391
for i_b in range(_B):
2391-
rigid_solver.func_solve_mass_batch(
2392+
func_solve_mass_batch(
23922393
i_b,
23932394
constraint_state.grad,
23942395
constraint_state.Mgrad,
@@ -2838,7 +2839,7 @@ def func_update_qacc(
28382839
dofs_state.force[i_d, i_b] = dofs_state.qf_smooth[i_d, i_b] + constraint_state.qfrc_constraint[i_d, i_b]
28392840
constraint_state.qacc_ws[i_d, i_b] = constraint_state.qacc[i_d, i_b]
28402841
if ti.math.isnan(constraint_state.qacc[i_d, i_b]):
2841-
errno[i_b] = errno[i_b] | 0b00000000000000000000000000000100
2842+
errno[i_b] = errno[i_b] | array_class.ErrorCode.INVALID_FORCE_NAN
28422843

28432844
ti.loop_config(serialize=static_rigid_sim_config.para_level < gs.PARA_LEVEL.ALL)
28442845
for i_b in range(_B):

genesis/engine/solvers/rigid/rigid_solver.py

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import genesis as gs
99
import genesis.utils.array_class as array_class
10-
import genesis.utils.geom as gu
1110
from genesis.engine.entities import DroneEntity, RigidEntity
1211
from genesis.engine.entities.base_entity import Entity
1312
from genesis.engine.states import QueriedStates, RigidSolverState
@@ -27,7 +26,18 @@
2726
from .collider import Collider
2827
from .constraint import ConstraintSolver, ConstraintSolverIsland
2928
from .abd.misc import (
29+
func_add_safe_backward,
30+
func_apply_coupling_force,
31+
func_apply_link_external_force,
32+
func_apply_external_torque,
33+
func_apply_link_external_torque,
34+
func_atomic_add_if,
35+
func_check_index_range,
36+
func_clear_external_force,
37+
func_read_field_if,
3038
func_wakeup_entity_and_its_temp_island,
39+
func_write_field_if,
40+
func_write_and_read_field_if,
3141
kernel_init_invweight,
3242
kernel_init_meaninertia,
3343
kernel_init_dof_fields,
@@ -43,77 +53,66 @@
4353
kernel_init_equality_fields,
4454
kernel_apply_links_external_force,
4555
kernel_apply_links_external_torque,
46-
func_apply_coupling_force,
47-
func_apply_link_external_force,
48-
func_apply_external_torque,
49-
func_apply_link_external_torque,
50-
func_clear_external_force,
5156
kernel_update_geoms_render_T,
5257
kernel_update_vgeoms_render_T,
5358
kernel_bit_reduction,
5459
kernel_set_zero,
55-
func_atomic_add_if,
56-
func_add_safe_backward,
57-
func_read_field_if,
58-
func_write_field_if,
59-
func_write_and_read_field_if,
60-
func_check_index_range,
6160
kernel_clear_external_force,
6261
)
6362
from .abd.forward_kinematics import (
64-
kernel_forward_kinematics_links_geoms,
65-
kernel_masked_forward_kinematics_links_geoms,
66-
kernel_forward_velocity,
67-
kernel_masked_forward_velocity,
63+
func_aggregate_awake_entities,
6864
func_COM_links,
6965
func_COM_links_entity,
7066
func_forward_kinematics_entity,
7167
func_forward_kinematics_batch,
72-
kernel_forward_kinematics_entity,
68+
func_forward_velocity_entity,
69+
func_forward_velocity_batch,
70+
func_forward_velocity,
71+
func_hibernate_entity_and_zero_dof_velocities,
72+
func_hibernate__for_all_awake_islands_either_hiberanate_or_update_aabb_sort_buffer,
7373
func_update_geoms_entity,
7474
func_update_geoms_batch,
75+
func_update_all_verts,
76+
func_update_cartesian_space,
77+
func_update_cartesian_space_entity,
78+
func_update_cartesian_space_batch,
7579
func_update_geoms,
80+
func_update_verts_for_geom,
81+
kernel_forward_kinematics_links_geoms,
82+
kernel_masked_forward_kinematics_links_geoms,
83+
kernel_forward_velocity,
84+
kernel_masked_forward_velocity,
85+
kernel_forward_kinematics_entity,
7686
kernel_update_geoms,
77-
func_forward_velocity_entity,
78-
func_forward_velocity_batch,
79-
func_forward_velocity,
8087
kernel_update_verts_for_geoms,
81-
func_update_verts_for_geom,
82-
func_update_all_verts,
8388
kernel_update_all_verts,
8489
kernel_update_geom_aabbs,
8590
kernel_update_vgeoms,
86-
func_hibernate__for_all_awake_islands_either_hiberanate_or_update_aabb_sort_buffer,
87-
func_aggregate_awake_entities,
88-
func_hibernate_entity_and_zero_dof_velocities,
89-
func_update_cartesian_space_entity,
90-
func_update_cartesian_space_batch,
91-
func_update_cartesian_space,
9291
kernel_update_cartesian_space,
9392
)
9493
from .abd.forward_dynamics import (
95-
update_qacc_from_qvel_delta,
96-
update_qvel,
97-
kernel_compute_mass_matrix,
98-
func_forward_dynamics,
99-
kernel_forward_dynamics,
100-
kernel_update_acc,
101-
func_vel_at_point,
94+
func_actuation,
95+
func_bias_force,
10296
func_compute_mass_matrix,
97+
func_compute_qacc,
10398
func_factor_mass,
99+
func_forward_dynamics,
104100
func_solve_mass_entity,
105101
func_solve_mass_batch,
106102
func_solve_mass,
107103
func_torque_and_passive_force,
108104
func_update_acc,
109105
func_update_force,
110-
func_actuation,
111-
func_bias_force,
112-
kernel_compute_qacc,
113-
func_compute_qacc,
114106
func_integrate,
115-
kernel_forward_dynamics_without_qacc,
116107
func_implicit_damping,
108+
func_vel_at_point,
109+
kernel_compute_mass_matrix,
110+
kernel_forward_dynamics,
111+
kernel_update_acc,
112+
kernel_compute_qacc,
113+
kernel_forward_dynamics_without_qacc,
114+
update_qacc_from_qvel_delta,
115+
update_qvel,
117116
)
118117
from .abd.accessor import (
119118
kernel_get_state,
@@ -156,21 +155,20 @@
156155
kernel_set_geoms_friction,
157156
)
158157
from .abd.diff import (
158+
func_copy_cartesian_space,
159159
func_copy_next_to_curr,
160160
func_copy_next_to_curr_grad,
161-
kernel_save_adjoint_cache,
162-
func_save_adjoint_cache,
161+
func_integrate_dq_entity,
162+
func_is_grad_valid,
163163
func_load_adjoint_cache,
164+
func_save_adjoint_cache,
165+
kernel_save_adjoint_cache,
164166
kernel_prepare_backward_substep,
165167
kernel_begin_backward_substep,
166-
func_is_grad_valid,
167-
func_copy_cartesian_space,
168168
kernel_copy_acc,
169-
func_integrate_dq_entity,
170169
)
171170

172171
if TYPE_CHECKING:
173-
import genesis.engine.solvers.rigid.array_class
174172
from genesis.engine.scene import Scene
175173
from genesis.engine.simulator import Simulator
176174
from genesis.engine.entities.rigid_entity import RigidJoint, RigidLink, RigidGeom, RigidVisGeom
@@ -1198,23 +1196,23 @@ def check_errno(self):
11981196
else:
11991197
errno = kernel_bit_reduction(self._errno)
12001198

1201-
if errno & 0b00000000000000000000000000000001:
1199+
if errno & array_class.ErrorCode.OVERFLOW_CANDIDATE_CONTACTS:
12021200
max_collision_pairs_broad = self.collider._collider_info.max_collision_pairs_broad[None]
12031201
gs.raise_exception(
12041202
f"Exceeding max number of broad phase candidate contact pairs ({max_collision_pairs_broad}). "
12051203
f"Please increase the value of RigidSolver's option 'multiplier_collision_broad_phase'."
12061204
)
1207-
if errno & 0b00000000000000000000000000000010:
1205+
if errno & array_class.ErrorCode.OVERFLOW_COLLISION_PAIRS:
12081206
max_contact_pairs = self.collider._collider_info.max_contact_pairs[None]
12091207
gs.raise_exception(
12101208
f"Exceeding max number of contact pairs ({max_contact_pairs}). Please increase the value of "
12111209
"RigidSolver's option 'max_collision_pairs'."
12121210
)
1213-
if errno & 0b00000000000000000000000000000100:
1211+
if errno & array_class.ErrorCode.INVALID_FORCE_NAN:
12141212
gs.raise_exception("Invalid constraint forces causing 'nan'. Please decrease Rigid simulation timestep.")
1215-
if errno & 0b00000000000000000000000000001000:
1213+
if errno & array_class.ErrorCode.INVALID_ACC_NAN:
12161214
gs.raise_exception("Invalid accelerations causing 'nan'. Please decrease Rigid simulation timestep.")
1217-
if errno & 0b00000000000000000000000000010000:
1215+
if errno & array_class.ErrorCode.OVERFLOW_HIBERNATION_ISLANDS:
12181216
gs.raise_exception("Contact island buffer overflow. Please increase RigidOptions 'max_collision_pairs'.")
12191217

12201218
def _kernel_detect_collision(self):

genesis/utils/array_class.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import dataclasses
3+
from enum import IntEnum
34
from functools import partial
45
from typing_extensions import dataclass_transform # Made it into standard lib from Python 3.12
56

@@ -73,6 +74,18 @@ def V_SCALAR_FROM(dtype, value):
7374
return data
7475

7576

77+
# =========================================== ErrorCode ===========================================
78+
79+
80+
class ErrorCode(IntEnum):
81+
SUCCESS = 0b000000000000000000000000000000000
82+
OVERFLOW_CANDIDATE_CONTACTS = 0b00000000000000000000000000000001
83+
OVERFLOW_COLLISION_PAIRS = 0b00000000000000000000000000000010
84+
OVERFLOW_HIBERNATION_ISLANDS = 0b00000000000000000000000000000100
85+
INVALID_FORCE_NAN = 0b00000000000000000000000000001000
86+
INVALID_ACC_NAN = 0b00000000000000000000000000010000
87+
88+
7689
# =========================================== RigidGlobalInfo ===========================================
7790

7891

0 commit comments

Comments
 (0)