I don't like the way we are handling nmat here. The number of materials can be obtained from the materials vector, and new materials can be dynamically added with push_back. Not sure why we need nmat. Let's go ahead with this for now, but I will open an issue to clean this file.