//======================================================================================================================
//======================================================================================================================
//======================================================================================================================
STKUNIT_UNIT_TEST(perceptMesh, open_new_close_PerceptMesh_2)
{
    EXCEPTWATCH;

    double x=0.123, y=0.234, z=0.345, time=0.0;

    // start_demo_open_new_close_PerceptMesh_2
    PerceptMesh eMesh(3u);
    // open the file we previously saved with the new fields
    eMesh.open_read_only(input_files_loc+"cube_with_pressure.e");

    eMesh.print_info("Info after reading mesh");

    //eMesh.print_fields();
    mesh::FieldBase *f_coords = eMesh.get_field("coordinates");

    // create a field function from the existing coordinates field
    FieldFunction ff_coords("ff_coords", f_coords, eMesh, 3, 3);

    // here we could evaluate this field function
    eval_vec3_print(x, y, z, time, ff_coords);

    // get the pressure field
    mesh::FieldBase* pressure_field = eMesh.get_field("pressure");

    // FIXME
    std::vector< const mesh::FieldBase * > sync_fields( 1 , pressure_field );
    mesh::communicate_field_data( eMesh.get_bulk_data()->shared_aura() , sync_fields );
    // FIXME

    //double * pdata = eMesh.node_field_data(pressure_field, 1);

    FieldFunction ff_pressure("ff_pressure", pressure_field, eMesh, 3, 1);
    ff_pressure.add_alias("P");
    StringFunction sf_pressure("P");

    // a point-source at the origin
#define EXACT_SOL log(sqrt( x*x + y*y + z*z) + 1.e-10)

    StringFunction sf_exact_solution(EXPAND_AND_QUOTE(EXACT_SOL), Name("sf_exact_solution"), 3, 1);
    StringFunction sf_error = sf_exact_solution - sf_pressure;

    eval_print(x,y,z,time, sf_error);
    double val_cpp = EXACT_SOL - pressure_value;
    double val_sf  = eval(x,y,z,time, sf_error);
    EXPECT_DOUBLE_EQ(val_sf, val_cpp);
    // end_demo

}
예제 #2
0
    void PMMParallelReferenceMeshSmoother::run_wrapper( Mesh* mesh,
                                                        ParallelMesh* pmesh,
                                                        MeshDomain* domain,
                                                        Settings* settings,
                                                        QualityAssessor* qa,
                                                        MsqError& err )
    {
      std::cout << "\nP[" << Mesquite::get_parallel_rank() << "] tmp srk PMMParallelReferenceMeshSmoother innerIter= " << innerIter << " parallelIterations= " << parallelIterations << std::endl;

      //if (!get_parallel_rank()) 
      std::cout << "\nP[" << get_parallel_rank() << "] tmp srk PMMParallelReferenceMeshSmoother: running shape improver... \n" << std::endl;

      PerceptMesquiteMesh *pmm = dynamic_cast<PerceptMesquiteMesh *>(mesh);
      PerceptMesh *eMesh = pmm->getPerceptMesh();
      m_pmm= pmm;
      m_eMesh = eMesh;
      m_num_nodes = m_eMesh->get_number_nodes();

      print_comm_list(*eMesh->get_bulk_data(), false);

      stk::mesh::FieldBase *coord_field           = eMesh->get_coordinates_field();
      stk::mesh::FieldBase *coord_field_current   = coord_field;
      stk::mesh::FieldBase *coord_field_projected = eMesh->get_field("coordinates_N"); 
      stk::mesh::FieldBase *coord_field_original  = eMesh->get_field("coordinates_NM1");
      stk::mesh::FieldBase *coord_field_lagged    = eMesh->get_field("coordinates_lagged");

      m_coord_field_original  = coord_field_original;
      m_coord_field_projected = coord_field_projected;
      m_coord_field_lagged    = coord_field_lagged;
      m_coord_field_current   = coord_field_current;

      eMesh->copy_field(coord_field_lagged, coord_field_original);

      // untangle
      PMMSmootherMetricUntangle untangle_metric(eMesh);

      // shape-size-orient smooth
      PMMSmootherMetricShapeSizeOrient shape_metric(eMesh);

      // shape
      PMMSmootherMetricShapeB1 shape_b1_metric(eMesh);

      // scaled jacobian
      PMMSmootherMetricScaledJacobianElemental scaled_jac_metric(eMesh);

      // scaled jacobian - nodal
      PMMSmootherMetricScaledJacobianNodal scaled_jac_metric_nodal(eMesh);

      //double omegas[] = {0.0, 0.001, 0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0};
      //double omegas[] = {0.001, 1.0};
      //double omegas[] = { 0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.4, 0.45,0.46,0.47,0.48,0.49,0.5,0.52,0.54,0.56,0.59, 0.6, 0.8, 1.0};
      double omegas[] = { 1.0};
      //double omegas[] = {0.0, 0.001, 0.01, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.4, 0.6, 0.8, 1.0};
      int nomega = sizeof(omegas)/sizeof(omegas[0]);
      
      for (int outer = 0; outer < nomega; outer++)
        {
          double omega = (outer < nomega ? omegas[outer] : 1.0);
          m_omega = omega;
          m_omega_prev = omega;
          if (outer > 0) m_omega_prev = omegas[outer-1];

          // set current state and evaluate mesh validity (current = omega*project + (1-omega)*original)
          eMesh->nodal_field_axpbypgz(omega, coord_field_projected, (1.0-omega), coord_field_original, 0.0, coord_field_current);

          int num_invalid = PMMParallelShapeImprover::parallel_count_invalid_elements(m_eMesh);

          if (!get_parallel_rank()) 
            std::cout << "\ntmp srk PMMParallelReferenceMeshSmoother num_invalid current= " << num_invalid << " for outer_iter= " << outer 
                      << " omega= " << omega
                      << (num_invalid ? " WARNING: invalid elements exist before Mesquite smoothing" : " OK")
                      << std::endl;
          //if (num_invalid) return;
          m_num_invalid = num_invalid;
          m_untangled = (m_num_invalid == 0);

          int iter_all=0;

          int do_anim = 0; // = frequency of anim writes
          if (do_anim)
            {
              eMesh->save_as("anim_all."+toString(iter_all)+".e");
            }

          for (int stage = 0; stage < 2; stage++)
            {
              m_stage = stage;
              if (stage==0) 
                {
                  m_metric = &untangle_metric;
                  //m_metric = &scaled_jac_metric_nodal;
                }
              else 
                {
                  int num_invalid_1 = PMMParallelShapeImprover::parallel_count_invalid_elements(m_eMesh);
                  VERIFY_OP_ON(num_invalid_1, ==, 0, "Invalid elements exist for start of stage 2, aborting");

                  //m_metric = &shape_metric;
                  m_metric = &shape_b1_metric;
                  //m_metric = &scaled_jac_metric;

                }

              for (int iter = 0; iter < innerIter; ++iter, ++iter_all)
                {
                  m_iter = iter;
                  int num_invalid_0 = PMMParallelShapeImprover::parallel_count_invalid_elements(m_eMesh);
                  m_num_invalid = num_invalid_0;

                  //               if (!get_parallel_rank() && num_invalid_0) 
                  //                 std::cout << "\ntmp srk PMMParallelReferenceMeshSmoother num_invalid current= " << num_invalid_0 
                  //                           << (num_invalid ? " WARNING: invalid elements exist before Mesquite smoothing" : "OK")
                  //                           << std::endl;

                  m_global_metric = run_one_iteration(mesh, domain, err);

                  sync_fields(iter);
                  num_invalid_0 = PMMParallelShapeImprover::parallel_count_invalid_elements(m_eMesh);
                  m_num_invalid = num_invalid_0;
                  bool conv = check_convergence();
                  if (!get_parallel_rank())
                  {
                    std::cout << "P[" << get_parallel_rank() << "] " << "tmp srk iter= " << iter << " dmax= " << m_dmax << " m_dnew= " << m_dnew 
                              << " m_d0= " << m_d0 << " m_alpha= " << m_alpha << " m_grad_norm= " << m_grad_norm << " m_scaled_grad_norm = " << m_scaled_grad_norm
                              << " num_invalid= " << num_invalid_0 
                              << " m_global_metric= " << m_global_metric << " m_untangled= " << m_untangled
                              << std::endl;
                  }

                  if (do_anim)
                    {
                       eMesh->save_as("iter_"+toString(outer)+"_"+toString(stage)+"."+toString(iter+1)+".e");
                       if (iter_all % do_anim == 0) eMesh->save_as("anim_all."+toString(iter_all+1)+".e");
                    }

                  if (!m_untangled && m_num_invalid == 0)
                    {
                      m_untangled = true;
                    }
                  if (conv && m_untangled) break;
                  //if (iter == 5) break;
                  //if (iter == 0) exit(1);
                }

              eMesh->save_as("outer_iter_"+toString(outer)+"_"+toString(stage)+"_mesh.e");
            }

          eMesh->copy_field(coord_field_lagged, coord_field);

        }

      //if (!get_parallel_rank()) 

      MPI_Barrier( MPI_COMM_WORLD );

      std::cout << "\nP[" << get_parallel_rank() << "] tmp srk PMMParallelReferenceMeshSmoother: running shape improver... done \n" << std::endl;

      MSQ_ERRRTN(err);
    }
//======================================================================================================================
//======================================================================================================================
//======================================================================================================================
STKUNIT_UNIT_TEST(perceptMesh, open_new_close_PerceptMesh)
{
    EXCEPTWATCH;

    // start_demo_open_new_close_PerceptMesh
    PerceptMesh eMesh(3u);
    eMesh.new_mesh(GMeshSpec("3x3x3|bbox:0,0,0,1,1,1"));  // create a 3x3x3 hex mesh in the unit cube
    int scalarDimension = 0; // a scalar
    int vectorDimension = 3;

    mesh::FieldBase* pressure_field = eMesh.add_field("pressure", stk::mesh::fem::FEMMetaData::NODE_RANK, scalarDimension);
    eMesh.add_field("velocity", stk::mesh::fem::FEMMetaData::NODE_RANK, vectorDimension);
    eMesh.add_field("element_volume", eMesh.element_rank(), scalarDimension);

    eMesh.commit();

    // create a field function from the new pressure field
    FieldFunction ff_pressure("ff_pressure", pressure_field, eMesh, 3, 1);

    // set the value of the pressure field to a constant everywhere
    ConstantFunction initPressureValues(pressure_value, "initPVal");
    ff_pressure.interpolateFrom(initPressureValues);

    //if (eMesh.get_rank()== 0) eMesh.print_fields("Pressure");
    //exit(1);

    // here we could evaluate this field function
    double x=0.123, y=0.234, z=0.345, time=0.0;
    std::cout << "P[" << eMesh.get_rank() << "] "
              << "before write ff_pressure = " << eval(x,y,z,time, ff_pressure) << std::endl;

    //eval_print(x, y, z, time, ff_pressure);

    double pval = eval(x, y, z, time, ff_pressure);
    EXPECT_DOUBLE_EQ(pval, pressure_value);

    eMesh.save_as(output_files_loc+"cube_with_pressure.e");
    eMesh.close();

    // end_demo

    // start_demo_open_new_close_PerceptMesh_1
    // open the file we previously saved with the new fields
    eMesh.open_read_only(input_files_loc+"cube_with_pressure.e");

    // get the pressure field
    pressure_field = eMesh.get_field("pressure");

    // FIXME
    std::vector< const mesh::FieldBase * > sync_fields( 1 , pressure_field );
    mesh::communicate_field_data( eMesh.get_bulk_data()->shared_aura() , sync_fields );
    // FIXME

    //if (1 || eMesh.get_rank()== 0) eMesh.print_fields("Pressure");

    FieldFunction ff_pressure_1("ff_pressure", pressure_field, eMesh, 3, 1);
    ff_pressure_1.add_alias("P");
    StringFunction sf_pressure("P");
    std::cout << "P[" << eMesh.get_rank() << "] "
              << "after read ff_pressure = " << eval(x,y,z,time, ff_pressure_1) << std::endl;

    // a point-source at the origin
#define EXACT_SOL log(sqrt( x*x + y*y + z*z) + 1.e-10)

    StringFunction sf_exact_solution(EXPAND_AND_QUOTE(EXACT_SOL), Name("sf_exact_solution"), 3, 1);
    StringFunction sf_error = sf_exact_solution - sf_pressure;

    std::cout << "P[" << eMesh.get_rank() << "] "
              << "sf_pressure = " << eval(x,y,z,time, sf_pressure) << std::endl;
    //!eval_print(x,y,z,time, sf_error);
    std::cout << "P[" << eMesh.get_rank() << "] "
              << "sf_error = " << eval(x,y,z,time, sf_error) << std::endl;
    double val_cpp = EXACT_SOL - pressure_value;
    double val_sf  = eval(x,y,z,time, sf_error);
    EXPECT_DOUBLE_EQ(val_sf, val_cpp);
    // end_demo

}