I was tired of writing MatSetOption(Ap,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE) into my code to find preallocation issues, so I added -mat_new_nonzero_allocation_err to MatSetFromOptions(). Now I actually call that function. In particular, I do things like

PetscOptionsList("-Ap_mat_type","Matrix type for velocity operator","",MatList,mattype_Ap,mattype_Ap,sizeof(mattype_Ap),NULL);
...
DMGetMatrix(fsu,mattype_Ap,&Ap);
MatSetOptionsPrefix(Ap,"Ap_");
MatSetOption(Ap,MAT_SYMMETRIC,PETSC_TRUE);
MatSetFromOptions(Ap);
// Check type and set MAT_IGNORE_LOWER_TRIANGULAR if necessary


Three problems:

1. This is too damn much work.

2. Checking the type is an ugly hack. Should MAT_IGNORE_LOWER_TRIANGULAR be default, or should nonsymmetric matrices ignore it, or should there be a new option with those semantics?

3. It's wrong to use "-Ap_mat_type sbaij" because the actual type becomes seqsbaij, and then preallocation is thrown away [*] when MatSetFromOptions finds "sbaij", calls MatSetType which compares it to "seqsbaij", and resets the type. This means that I have to use a different prefix to choose which type to pass to DMGetMatrix() than the prefix I subsequently set on the matrix.


Any ideas how to make this work in a sane way?

[*] I was getting a seg-fault, but I set mat->preallocated to false in MatSetType. It doesn't crash any more, but preallocation is still thrown away.