I have a legacy DG finite element code for which I would like to write some unit tests. As it is written, for 2D problems, each degree of freedom has associated with it a Jacobian matrix (for later transformation to the master element). All elements are straight-sided.

Is there a good way of sanity checking that these Jacobians are correct? The previous author used some nasty meta-programming to generate a file thousands of lines long which is parsed by python and used to compute the Jacobians. Therefore I prefer to treat it as a black box and test the output.

  • $\begingroup$ Is your local-to-global mapping affine? Are you dealing with triangular elements only? $\endgroup$
    – knl
    Nov 19, 2016 at 10:48

2 Answers 2


Like with any other thing you want to test, you need to come up with a list of situations where you know that something is true, and then check this. In many cases, knowing that something is true does not require being able to actually compute the numerically correct answer, which simplifies things.

For example:

  • As @BlaisB suggestes, if you rotate the reference cell, the determinant of the Jacobian needs to be one. You don't need to know the exact Jacobian matrix.
  • The same is true if you translate the cell.
  • If you scale the cell by a factor, the Jacobian needs to be a multiple of the identity matrix. The multiplication factor is simply the scaling factor.
  • If you randomly generate cells that are (i) convex, and (ii) whose vertices are oriented in the correct way, the determinant needs to have a positive determinant.
  • If you evaluate the determinant at multiple points of an affine triangle, it needs to be the same everywhere.
  • If you compute $\int_T 1 \; dx$ via quadrature, then the result of the quadrature needs to be equal to the area/volume of the cell. Because the area is something that's easy to compute, it's easy to assess that the answer you get is correct up to round-off.
  • ...

All of these make for good, individual testcases that are relatively simple to write. Within an hour, I could probably come up with a dozen or more such testcases.

  • 1
    $\begingroup$ I wish my original answer could have been this clear and precise. I believe applying all such checks should provide a sufficiently large coverage of when things may go wrong. $\endgroup$
    – BlaB
    Nov 21, 2016 at 4:05

Well, the first obvious test is to check if the determinant of the Jacobian matrix is positive. If it not, it means that your element has inverted and your answer is going to be invalid.

Otherwise, I am not sure of any other test that would be good... For a unit test you could rotate a triangle 360 degrees by increment of 1 degree and measure that the jacobian is always of determinant 1...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.