static int make_element(ErlNifEnv* env, struct buf *rbuf, ERL_NIF_TERM el) { ErlNifBinary cdata, name; const ERL_NIF_TERM *tuple; int arity, ret = 0; if (enif_get_tuple(env, el, &arity, &tuple)) { if (arity == 2) { if (!ENIF_COMPARE(tuple[0], atom_xmlcdata)) { if (enif_inspect_iolist_as_binary(env, tuple[1], &cdata)) { crypt(env, rbuf, cdata.data, cdata.size); ret = 1; }; }; }; if (arity == 4) { if (!ENIF_COMPARE(tuple[0], atom_xmlelement)) { if (enif_inspect_iolist_as_binary(env, tuple[1], &name)) { buf_add_char(env, rbuf, '<'); buf_add_str(env, rbuf, (char *)name.data, name.size); ret = make_attrs(env, rbuf, tuple[2]); if (ret) { if (enif_is_empty_list(env, tuple[3])) { buf_add_str(env, rbuf, "/>", 2); } else { buf_add_char(env, rbuf, '>'); ret = make_elements(env, rbuf, tuple[3]); if (ret) { buf_add_str(env, rbuf, "</", 2); buf_add_str(env, rbuf, (char*)name.data, name.size); buf_add_char(env, rbuf, '>'); }; }; }; }; }; }; }; return ret; }
// Reading the MCNP file MCNPError McnpData::read_mcnpfile(bool skip_mesh) { MCNPError result; moab::ErrorCode MBresult; moab::CartVect tvect; std::vector<double> xvec[3]; // Open the file std::ifstream mcnpfile; mcnpfile.open( MCNP_filename.c_str() ); if (!mcnpfile) { std::cout << "Unable to open MCNP data file." << std::endl; return MCNP_FAILURE; } std::cout << std::endl; std::cout << "Reading MCNP input file..." << std::endl; // Prepare for file reading ... char line[10000]; int mode = 0; // Set the file reading mode to read proper data int nv[3]; // Read in the file ... while (! mcnpfile.eof() ) { mcnpfile.getline(line, 10000); // std::cout << line << std::endl; switch(mode) { case 0: // First line is a title mode++; break; case 1: // Coordinate system mode++; result = read_coord_system(line); if (result == MCNP_FAILURE) return MCNP_FAILURE; break; case 2: // Rotation matrix mode++; for (int i = 0; i < 4; i++) { mcnpfile.getline(line, 10000); result = read_rotation_matrix(line, i); if (result == MCNP_FAILURE) return MCNP_FAILURE; } if (skip_mesh) return MCNP_SUCCESS; break; case 3: // Read in vertices and build elements mode++; for (int i = 0; i < 3; i++) { // How many points in the x[i]-direction nv[i] = how_many_numbers(line); if (nv[i] <= 0) return MCNP_FAILURE; // Get space and read in these points result = read_numbers(line , nv[i], xvec[i]); if (result == MCNP_FAILURE) return MCNP_FAILURE; // Update to the next line mcnpfile.getline(line, 10000); } // Make the elements and vertices result = make_elements(xvec, nv); if (result == MCNP_FAILURE) return MCNP_FAILURE; break; case 4: // Read in tally data, make, and tag elements mode++; moab::EntityHandle elemhandle; moab::EntityHandle vstart, vijk; moab::EntityHandle connect[8]; // double d[3]; // vstart = MCNP_vertices.front(); vstart = *(vert_handles.begin()); for (int i=0; i < nv[0]-1; i++) { for (int j=0; j < nv[1]-1; j++) { for (int k=0; k < nv[2]-1; k++) { vijk = vstart + (i + j*nv[0] + k*nv[0]*nv[1]); //std::cout << vijk << std::endl; connect[0] = vijk; connect[1] = vijk + 1; connect[2] = vijk + 1 + nv[0]; connect[3] = vijk + nv[0]; connect[4] = vijk + nv[0]*nv[1]; connect[5] = vijk + 1 + nv[0]*nv[1]; connect[6] = vijk + 1 + nv[0] + nv[0]*nv[1]; connect[7] = vijk + nv[0] + nv[0]*nv[1]; MBresult = MBI->create_element(moab::MBHEX, connect, 8, elemhandle); if (MBresult != moab::MB_SUCCESS) return MCNP_FAILURE; elem_handles.insert(elemhandle); mcnpfile.getline(line, 10000); result = extract_tally_data(line, elemhandle); if (result == MCNP_FAILURE) return MCNP_FAILURE; } } } /* for (MBRange::iterator rit=vert_handles.begin(); rit != vert_handles.end(); ++rit) { std::cout << *rit << std::endl; } for (int i=0; i < nv[0]-1; i++) { for (int j=0; j < nv[1]-1; j++) { for (int k=0; k < nv[2]-1; k++) { vijk = vstart + (i + j*nv[0] + k*nv[0]*nv[1]); connect[0] = vijk; connect[1] = vijk + 1; connect[2] = vijk + 1 + nv[0]; connect[3] = vijk + nv[0]; connect[4] = vijk + nv[0]*nv[1]; connect[5] = vijk + 1 + nv[0]*nv[1]; connect[6] = vijk + 1 + nv[0] + nv[0]*nv[1]; connect[7] = vijk + nv[0] + nv[0]*nv[1]; MBresult = MBI->create_element(MBHEX, connect, 8, elemhandle); if (MBresult != MB_SUCCESS) return MCNP_FAILURE; elem_handles.insert(elemhandle); mcnpfile.getline(line, 10000); result = extract_tally_data(line, elemhandle); if (result == MCNP_FAILURE) return MCNP_FAILURE; } } } */ break; case 5: // Ckeck for weirdness at end of file if (! mcnpfile.eof() ) return MCNP_FAILURE; break; } } std::cout << "SUCCESS! Read in " << elem_handles.size() << " elements!" << std::endl << std::endl; // MCNP_vertices.clear(); vert_handles.clear(); MCNP_elems.clear(); return MCNP_SUCCESS; }