@@ -1128,17 +1128,43 @@ def _submat_callback_body(self):
11281128
11291129 dm_get_info = petsc_call (
11301130 'DMDAGetInfo' , [
1131- sobjs ['callbackdm' ], Null , Byref ( sobjs [ 'M' ]), Byref ( sobjs [ 'N' ]) ,
1131+ sobjs ['callbackdm' ], Null , Null , Null ,
11321132 Null , Null , Null , Null , Byref (objs ['dof' ]), Null , Null , Null , Null , Null
11331133 ]
11341134 )
1135- subblock_rows = DummyExpr (objs ['subblockrows' ], Mul (sobjs ['M' ], sobjs ['N' ]))
1136- subblock_cols = DummyExpr (objs ['subblockcols' ], Mul (sobjs ['M' ], sobjs ['N' ]))
11371135
11381136 ptr = DummyExpr (
11391137 objs ['submat_arr' ]._C_symbol , Deref (objs ['Submats' ]._C_symbol ), init = True
11401138 )
11411139
1140+ i = Dimension (name = 'i' )
1141+ tmpvec = sobjs ['tmpvec' ]
1142+
1143+ row_idx = DummyExpr (objs ['rowidx' ], IntDiv (i , objs ['dof' ]))
1144+ col_idx = DummyExpr (objs ['colidx' ], Mod (i , objs ['dof' ]))
1145+
1146+ # Query constrained global size from each sub-DM via a temporary Vec.
1147+ # For unconstrained sub-DMs this is equivalent to M*N; for constrained
1148+ # (BC-excluded) sub-DMs it returns the reduced size automatically.
1149+ get_row_vec = petsc_call (
1150+ 'DMGetGlobalVector' , [objs ['Subdms' ].indexed [objs ['rowidx' ]], Byref (tmpvec )]
1151+ )
1152+ get_row_size = petsc_call (
1153+ 'VecGetSize' , [tmpvec , Byref (objs ['subblockrows' ])]
1154+ )
1155+ restore_row_vec = petsc_call (
1156+ 'DMRestoreGlobalVector' , [objs ['Subdms' ].indexed [objs ['rowidx' ]], Byref (tmpvec )]
1157+ )
1158+ get_col_vec = petsc_call (
1159+ 'DMGetGlobalVector' , [objs ['Subdms' ].indexed [objs ['colidx' ]], Byref (tmpvec )]
1160+ )
1161+ get_col_size = petsc_call (
1162+ 'VecGetSize' , [tmpvec , Byref (objs ['subblockcols' ])]
1163+ )
1164+ restore_col_vec = petsc_call (
1165+ 'DMRestoreGlobalVector' , [objs ['Subdms' ].indexed [objs ['colidx' ]], Byref (tmpvec )]
1166+ )
1167+
11421168 mat_create = petsc_call ('MatCreate' , [sobjs ['comm' ], Byref (objs ['block' ])])
11431169
11441170 mat_set_sizes = petsc_call (
@@ -1151,10 +1177,6 @@ def _submat_callback_body(self):
11511177 mat_set_type = petsc_call ('MatSetType' , [objs ['block' ], 'MATSHELL' ])
11521178
11531179 malloc = petsc_call ('PetscMalloc1' , [1 , Byref (objs ['subctx' ])])
1154- i = Dimension (name = 'i' )
1155-
1156- row_idx = DummyExpr (objs ['rowidx' ], IntDiv (i , objs ['dof' ]))
1157- col_idx = DummyExpr (objs ['colidx' ], Mod (i , objs ['dof' ]))
11581180
11591181 deref_subdm = Dereference (objs ['Subdms' ], objs ['ljacctx' ])
11601182
@@ -1193,12 +1215,18 @@ def _submat_callback_body(self):
11931215 assign_block = DummyExpr (objs ['submat_arr' ].indexed [i ], objs ['block' ])
11941216
11951217 iter_body = (
1218+ row_idx ,
1219+ col_idx ,
1220+ get_row_vec ,
1221+ get_row_size ,
1222+ restore_row_vec ,
1223+ get_col_vec ,
1224+ get_col_size ,
1225+ restore_col_vec ,
11961226 mat_create ,
11971227 mat_set_sizes ,
11981228 mat_set_type ,
11991229 malloc ,
1200- row_idx ,
1201- col_idx ,
12021230 set_rows ,
12031231 set_cols ,
12041232 dm_set_ctx ,
@@ -1233,8 +1261,6 @@ def _submat_callback_body(self):
12331261 mat_get_dm ,
12341262 dm_get_app ,
12351263 dm_get_info ,
1236- subblock_rows ,
1237- subblock_cols ,
12381264 ptr ,
12391265 BlankLine ,
12401266 iteration ,
0 commit comments