diff --git a/AMDiS/src/MeshStructure.cc b/AMDiS/src/MeshStructure.cc
index 05da7b3cb90b34011bad5dd6486b881ac4c4d744..44ce8a8262bc8d9242e2b14bbf174411a94e4390 100644
--- a/AMDiS/src/MeshStructure.cc
+++ b/AMDiS/src/MeshStructure.cc
@@ -299,4 +299,58 @@ namespace AMDiS {
   {
     return (other.getCode() == code);
   }
+
+
+  void MeshStructure::writeMeshFile(Mesh *mesh, std::string filename)
+  {
+    FUNCNAME("MeshStructure::writeMeshFile()");
+
+    int nMacroElements = 0;
+    int macroElIndex = -1;
+    std::ofstream file;
+    file.open(filename.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
+
+    TraverseStack stack;
+    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
+    while (elInfo) {
+      nMacroElements++;
+      elInfo = stack.traverseNext(elInfo);
+    }
+
+    file << nMacroElements;
+    
+    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
+    while (elInfo) {
+      if (elInfo->getLevel() == 0) {
+	if (macroElIndex != -1) {
+	  commit();
+	  writeMacroElement(file, macroElIndex);
+	}
+
+	clear();
+	macroElIndex = elInfo->getElement()->getIndex();
+      }
+
+      insertElement(elInfo->getElement()->isLeaf());
+      elInfo = stack.traverseNext(elInfo);
+    }
+
+    // And write the last macro element to file.
+    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
+    commit();
+    writeMacroElement(file, macroElIndex);
+
+    file.close();
+  }
+
+
+  void MeshStructure::writeMacroElement(std::ofstream& file, int macroElIndex)
+  {
+    file << macroElIndex;
+    file << nElements;
+    file << code.size();
+    for (unsigned int i = 0; i < code.size(); i++)
+      file << code[i];
+  }
+
 }
diff --git a/AMDiS/src/MeshStructure.h b/AMDiS/src/MeshStructure.h
index 2fac234ac06795d7da61b45d3937f441761e7344..b0f64162d1d448c2e10dac69fa145b1cb13051e1 100644
--- a/AMDiS/src/MeshStructure.h
+++ b/AMDiS/src/MeshStructure.h
@@ -156,6 +156,8 @@ namespace AMDiS {
     /// Returns true, if the given mesh structure code is equal to this one.
     bool compare(MeshStructure &other);
 
+    void writeMeshFile(Mesh *mesh, std::string filename);
+
   protected:
     /// Insert a new element to the structure code. Is used by the init function.
     void insertElement(bool isLeaf);
@@ -167,6 +169,8 @@ namespace AMDiS {
 	       MeshStructure *structure2,
 	       MeshStructure *result);
 
+    void writeMacroElement(std::ofstream& file, int macroElIndex);
+
   protected:
     /// Mesh structure code.
     std::vector<unsigned long int> code;