MeshKit  1.0
createblelements.cpp
Go to the documentation of this file.
00001 #include "meshkit/PostBL.hpp"
00002 namespace MeshKit
00003 {
00004   int PostBL::create_bl_elements(VerdictWrapper vw)
00005   // ---------------------------------------------------------------------------
00009   // ---------------------------------------------------------------------------
00010   {
00011     int qcount = 0;
00012     
00013     // Now start creating New elements
00014     for (Range::iterator kter = quads.begin(); kter != quads.end(); ++kter){
00015         qcount++;        
00016         std::vector<moab::EntityHandle> old_hex;
00017         MBERRCHK(mb->get_adjacencies(&(*kter), 1, m_GD, false, old_hex),mb);
00018         //      int this_quad_mat = -1;
00019         //      MBERRCHK(mb->tag_get_data(MatIDTag, &old_hex[0], 1, &this_quad_mat), mb);
00020         double jac = 0;
00021         
00022         MBERRCHK(mb->get_connectivity(&(*kter), 1, qconn),mb);
00023         double one_node_in_quad[3];
00024         
00025         for (int i=0; i<m_BElemNodes; i++){
00026             int node_tag_id = 0;
00027             MBERRCHK(mb->tag_get_data(BLNodeIDTag, &qconn[i], 1, &node_tag_id) ,mb);
00028             MBERRCHK(mb->get_coords(&qconn[i], 1, one_node_in_quad),mb);
00029             m_LogFile << std::setprecision (3) << std::scientific << " new nodes:- coords: " << one_node_in_quad[0]
00030                       << ", " << one_node_in_quad[1] << ", " << one_node_in_quad[2]  << std::endl;
00031             
00032             //populate the connectivity after creating nodes for this BL node
00033             for(int j=0; j< m_Intervals; j++){
00034                 
00035                 if(m_Conn == 8 && m_BElemNodes == 4){ // hex
00036                     int nid = node_tag_id*m_Intervals + j;
00037                     if(j == 0){
00038                         conn[m_Conn*j +i] = qconn[i];
00039                         conn[m_Conn*j + i+m_BElemNodes] = new_vert[nid];
00040                       }
00041                     else if(j==(m_Intervals-1)){
00042                         conn[m_Conn*j +i] = new_vert[nid];
00043                         conn[m_Conn*j + i+m_BElemNodes] = new_vert[nid -1];
00044                       }
00045                     else {
00046                         conn[m_Conn*j +i] = new_vert[nid + m_Intervals - 2*j -1];
00047                         conn[m_Conn*j + i+m_BElemNodes] = new_vert[nid + m_Intervals - 2*j -2];
00048                       }
00049                   }
00050                 else if(m_Conn == 4 && m_BElemNodes == 2){ // Quads
00051                     int nid = node_tag_id*m_Intervals+j;
00052                     if(m_Intervals == 1){
00053                         conn[m_Conn*j +i] = new_vert[nid];
00054                         conn[m_Conn*j + i+m_BElemNodes] = qconn[m_BElemNodes-i-1];
00055                       }
00056                     else if(j==0){
00057                         conn[m_Conn*j +i] = qconn[m_BElemNodes-i-1];
00058                         conn[m_Conn*j + i+m_BElemNodes] = new_vert[nid + m_Intervals - 2];
00059                       }
00060                     else if(j==(m_Intervals-1)){
00061                         conn[m_Conn*j +i] = new_vert[nid];
00062                         conn[m_Conn*j + m_BElemNodes + 1 -i] = new_vert[nid - m_Intervals + 1];
00063                       }
00064                     else {
00065                         conn[m_Conn*j +i] = new_vert[nid + m_Intervals - 2*j -2];
00066                         conn[m_Conn*j + m_BElemNodes + 1 -i] = new_vert[nid + m_Intervals - 2*j -1];
00067                       }
00068                   }
00069                 else if(m_Conn == 4 && m_BElemNodes == 3){ // && hybrid == true make wedges aka prisms for tet mesh
00070                     int nid = node_tag_id*m_Intervals+j;
00071                     if(j==0){
00072                         conn[m_HConn*j +i] = qconn[i];
00073                         conn[m_HConn*j + i+m_BElemNodes] = new_vert[nid];
00074                       }
00075                     else if(j==(m_Intervals-1)){
00076                         conn[m_HConn*j +i] = new_vert[nid];
00077                         conn[m_HConn*j + i+m_BElemNodes] = new_vert[nid-1];
00078                       }
00079                     else {
00080                         conn[m_HConn*j +i] = new_vert[nid + m_Intervals - 2*j -1];
00081                         conn[m_HConn*j + i+m_BElemNodes] = new_vert[nid + m_Intervals - 2*j -2];
00082                       }
00083                   }
00084                 else if(m_Conn == 3 && m_BElemNodes == 2){ // make quads for tri mesh
00085                     int nid = node_tag_id*m_Intervals+j;
00086                     if(m_Intervals == 1){
00087                         conn[m_Conn*j +i] = new_vert[nid];
00088                         conn[m_Conn*j + i+m_BElemNodes] = qconn[m_BElemNodes-i-1];
00089                       }
00090                     else if(j==0){
00091                         conn[m_HConn*j +i] = qconn[m_BElemNodes-i-1];
00092                         conn[m_HConn*j + i+m_BElemNodes] = new_vert[nid + m_Intervals - 2];
00093                       }
00094                     else if(j==(m_Intervals-1)){
00095                         conn[m_HConn*j +i] = new_vert[nid];
00096                         conn[m_HConn*j + m_BElemNodes + 1 - i] = new_vert[nid - m_Intervals + 1];
00097                       }
00098                     else {
00099                         conn[m_HConn*j +i] = new_vert[nid + m_Intervals - 2*j -2];
00100                         conn[m_HConn*j + m_BElemNodes + 1 - i] = new_vert[nid + m_Intervals - 2*j -1];
00101                       }
00102                   }
00103                 else {
00104                     std::cout << "ERROR: cannot create BL elements: element type not supported." << std::endl;
00105                     exit(0);
00106                   }
00107               }
00108           }
00109         
00110         //TODO: Set Connectivity of tet's, break prisms into 3 tets, Another loop is required.
00111         if(m_Conn == 4 && m_BElemNodes == 3 && hybrid == false){
00112             for(int c=0; c<m_Intervals; c++){
00113                 // first tet
00114                 tet_conn[m_Conn*c*3] =     conn[c*m_HConn + 0];
00115                 tet_conn[m_Conn*c*3 + 1] = conn[c*m_HConn + 4];
00116                 tet_conn[m_Conn*c*3 + 2] = conn[c*m_HConn + 5];
00117                 tet_conn[m_Conn*c*3 + 3] = conn[c*m_HConn + 3];
00118                 // middle tet
00119                 tet_conn[m_Conn*c*3 + 4] = conn[c*m_HConn + 0];
00120                 tet_conn[m_Conn*c*3 + 5] = conn[c*m_HConn + 1];
00121                 tet_conn[m_Conn*c*3 + 6] = conn[c*m_HConn + 2];
00122                 tet_conn[m_Conn*c*3 + 7] = conn[c*m_HConn + 5];
00123                 //last tet
00124                 tet_conn[m_Conn*c*3 + 8] = conn[c*m_HConn + 0];
00125                 tet_conn[m_Conn*c*3 + 9] = conn[c*m_HConn + 1];
00126                 tet_conn[m_Conn*c*3 + 10] = conn[c*m_HConn + 5];
00127                 tet_conn[m_Conn*c*3 + 11] = conn[c*m_HConn + 4];
00128               }
00129           }
00130         
00131         if(m_Conn == 3 && m_BElemNodes == 2 && hybrid == false){
00132             for(int c=0; c<m_Intervals; c++){
00133                 if(tri_sch == 1){
00134                     // lower triangle
00135                     tri_conn[m_Conn*c*2] =     conn[c*m_HConn + 0];
00136                     tri_conn[m_Conn*c*2 + 1] = conn[c*m_HConn + 1];
00137                     tri_conn[m_Conn*c*2 + 2] = conn[c*m_HConn + 2];
00138                     // upper triangl
00139                     tri_conn[m_Conn*c*2 + 3] = conn[c*m_HConn + 2];
00140                     tri_conn[m_Conn*c*2 + 4] = conn[c*m_HConn + 3];
00141                     tri_conn[m_Conn*c*2 + 5] = conn[c*m_HConn + 0];
00142                   }
00143                 else if(tri_sch == 2){
00144                     // lower triangle
00145                     tri_conn[m_Conn*c*2] =     conn[c*m_HConn + 0];
00146                     tri_conn[m_Conn*c*2 + 1] = conn[c*m_HConn + 1];
00147                     tri_conn[m_Conn*c*2 + 2] = conn[c*m_HConn + 2];
00148                     // upper triangle
00149                     tri_conn[m_Conn*c*2 + 3] = conn[c*m_HConn + 2];
00150                     tri_conn[m_Conn*c*2 + 4] = conn[c*m_HConn + 3];
00151                     tri_conn[m_Conn*c*2 + 5] = conn[c*m_HConn + 0];
00152                   }
00153               }
00154           }
00155         
00156         // create boundary layer hexes
00157         // hex material will be found from the innermost hex adjacency. Let's create that first.       
00158         for(int j=0; j < m_Intervals; j++){
00159             if(m_Conn == 8){
00160                 MBERRCHK(mb->create_element(moab::MBHEX, &conn[j*m_Conn], m_Conn, hex),mb);
00161               }
00162             else if(m_Conn==4 && m_GD ==3 && hybrid == true){
00163                 MBERRCHK(mb->create_element(MBPRISM, &conn[j*6], 6, hex),mb);
00164               }
00165             else if(m_Conn==4 && m_GD ==2){
00166                 MBERRCHK(mb->create_element(MBQUAD, &conn[j*m_Conn], m_Conn, hex),mb);
00167               }
00168             else if(m_Conn==3 && m_GD ==2 && hybrid == true){
00169                 MBERRCHK(mb->create_element(MBQUAD, &conn[j*m_HConn], m_HConn, hex),mb);
00170               }
00171             else if(m_Conn==3 && m_GD ==2 && hybrid == false){
00172                 MBERRCHK(mb->create_element(MBTRI, &tri_conn[j*m_Conn*2], m_Conn, hex),mb);
00173                 MBERRCHK(mb->create_element(MBTRI, &tri_conn[j*m_Conn*2+3], m_Conn, hex1),mb);
00174                 //       MBERRCHK(mb->add_entities(mthis_set, &hex1, 1), mb);
00175                 //        MBERRCHK(mb->add_entities(smooth_set, &hex1, 1), mb);
00176               }
00177             else if(m_Conn==4 && m_GD ==3 && hybrid == false){
00178                 MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3], m_Conn, hex),mb);
00179                 MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3+4], m_Conn, hex1),mb);
00180                 MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3+8], m_Conn, hex2),mb);
00181                 //       MBERRCHK(mb->add_entities(mthis_set, &hex1, 1), mb);
00182                 //        MBERRCHK(mb->add_entities(smooth_set, &hex1, 1), mb);
00183               }
00184             
00185             vw.quality_measure(hex, MB_JACOBIAN, jac);
00186             if(jac < 0){
00187                 m_LogFile <<  "New BL elements have negative jacobian, trying to fix.. " << jac << std::endl;
00188                 // swap 0-4 1-5 2-6 3-7 for positive jacobian -> posibly a multi material case
00189                 moab::EntityHandle temp;
00190                 for(int ii = j*m_Conn; ii < j*m_Conn + m_BElemNodes; ii++){
00191                     temp = conn[ii];
00192                     conn[ii] = conn[ii+m_BElemNodes];
00193                     conn[ii+m_BElemNodes] = temp;
00194                   }
00195                 
00196                 //reverse the tet connectivity based on the prism reverse
00197                 if(m_Conn == 4 && m_BElemNodes == 3 && hybrid == false){
00198                     for(int c=0; c<m_Intervals; c++){
00199                         // first tet
00200                         tet_conn[m_Conn*c*3] =     conn[c*m_HConn + 0];
00201                         tet_conn[m_Conn*c*3 + 1] = conn[c*m_HConn + 4];
00202                         tet_conn[m_Conn*c*3 + 2] = conn[c*m_HConn + 5];
00203                         tet_conn[m_Conn*c*3 + 3] = conn[c*m_HConn + 3];
00204                         // middle tet
00205                         tet_conn[m_Conn*c*3 + 4] = conn[c*m_HConn + 0];
00206                         tet_conn[m_Conn*c*3 + 5] = conn[c*m_HConn + 1];
00207                         tet_conn[m_Conn*c*3 + 6] = conn[c*m_HConn + 2];
00208                         tet_conn[m_Conn*c*3 + 7] = conn[c*m_HConn + 5];
00209                         //last tet
00210                         tet_conn[m_Conn*c*3 + 8] = conn[c*m_HConn + 0];
00211                         tet_conn[m_Conn*c*3 + 9] = conn[c*m_HConn + 1];
00212                         tet_conn[m_Conn*c*3 + 10] = conn[c*m_HConn + 5];
00213                         tet_conn[m_Conn*c*3 + 11] = conn[c*m_HConn + 4];
00214                       }
00215                   }
00216                 
00217                 // recreate this element 
00218                 if(m_Conn == 8){
00219                     MBERRCHK(mb->create_element(moab::MBHEX, &conn[j*m_Conn], m_Conn, hex),mb);
00220                   }
00221                 else if(m_Conn==4 && m_GD ==3 && hybrid == false){
00222                     MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3], m_Conn, hex),mb);
00223                     MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3+4], m_Conn, hex1),mb);
00224                     MBERRCHK(mb->create_element(MBTET, &tet_conn[j*m_Conn*3+8], m_Conn, hex2),mb);
00225                   }
00226                 else if(m_Conn==4 && m_GD ==3 && hybrid == true){
00227                     MBERRCHK(mb->create_element(MBPRISM, &conn[j*6], 6, hex),mb);
00228                   }
00229                 else if(m_Conn==4 && m_GD ==2){
00230                     MBERRCHK(mb->create_element(MBQUAD, &conn[j*m_Conn], m_Conn, hex),mb);
00231                   }
00232                 else if(m_Conn==3 && m_GD ==2 && hybrid == true){
00233                     MBERRCHK(mb->create_element(MBQUAD, &conn[j*m_HConn], m_HConn, hex),mb);
00234                   }
00235                 // check jacobian again
00236                 vw.quality_measure(hex, MB_JACOBIAN, jac);              
00237                 if(jac < 0){
00238                     m_LogFile << "Negative jacobian still found for the Bl elements, invalid mesh!"  << jac << std::endl;
00239                   }      
00240                 // end recreation of element -> negative hex was encontered while creating Bl elements
00241               }
00242             
00243             if(mthis_set == 0){ // No material specified for BL hexes find the material to append the hex             
00244                 moab::EntityHandle mat_set = 0;
00245                 for (set_it = m_sets.begin(); set_it != m_sets.end(); set_it++)  {
00246                     int set_id = -1;
00247                     mat_set = *set_it;
00248                     MBERRCHK(mb->tag_get_data(MTag, &mat_set, 1, &set_id), mb);
00249                     if(set_id == blmaterial_id[qcount -1])
00250                       break;
00251                   }
00252                 if(mat_set != 0){
00253                     if(hex!=0)
00254                       MBERRCHK(mb->add_entities(mat_set,&hex,1), mb);
00255                     if(hex1!=0)
00256                       MBERRCHK(mb->add_entities(mat_set,&hex1,1), mb);
00257                     if(hex2!=0)
00258                       MBERRCHK(mb->add_entities(mat_set,&hex2,1), mb);
00259                   }
00260                 else{
00261                     exit(0);
00262                   }
00263               }
00264             else {
00265                 MBERRCHK(mb->add_entities(mthis_set, &hex, 1), mb);
00266                 if(m_Conn==3 && m_GD ==2 && hybrid == false)
00267                   MBERRCHK(mb->add_entities(mthis_set, &hex1, 1), mb);
00268                 if(m_Conn==4 && m_GD ==3 && hybrid == false){
00269                     MBERRCHK(mb->add_entities(mthis_set, &hex1, 1), mb);
00270                     MBERRCHK(mb->add_entities(mthis_set, &hex2, 1), mb);
00271                   }
00272               }
00273             // mark entities for smoothing - careful: smoothing BL elements will cause loss of biased elements
00274             //                MBERRCHK(mb->add_entities(smooth_set, &hex, 1), mb);
00275             // add geom dim tag
00276             MBERRCHK(mb->add_entities(geom_set, &hex, 1), mb);
00277             //              // TODO: Add Local Smooting
00278           }
00279       }
00280     //    all_elems.clear();
00281     //    moab::Range skin_verts;
00282     //    MBERRCHK(mb->get_entities_by_dimension(0, 3, all_elems,true),mb);
00283     //    moab::Skinner skinner(mb);
00284     //    skinner.find_skin(0, all_elems, 0, skin_verts);
00285     //    m_LogFile << "setting 'fixed'' tag = 1 on verts in the skin = " <<  skin_verts.size() << std::endl;
00286     //    // set fixed tag = 1 on all skin verts
00287     //    std::vector<int> all_skin_data(skin_verts.size(), 1);
00288     //    MBERRCHK(mb->tag_set_data(FTag, skin_verts, &all_skin_data[0]), mb);
00289     return 0;
00290   }
00291 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines