write2dm function