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

    // start_demo_open_new_close_PerceptMesh_3
    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();

    EXPECT_CATCH( eMesh.commit() , commit_again);

    // 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);

    // save
    eMesh.save_as(output_files_loc+"cube_with_pressure_3.e");
    eMesh.close();

    EXPECT_CATCH( eMesh.print_info("bad", 1) , mesh_closed_try_print);

    // end_demo

}
STKUNIT_UNIT_TEST(geom, volume)
{
  dw().m(LOG_GEOMETRY_VERIFIER) << "TEST::geom::volume " << stk_classic::diag::dendl;

  const size_t num_x = 3;
  const size_t num_y = 3;
  const size_t num_z = 3;
  std::string config_mesh =
    Ioss::Utils::to_string(num_x) + "x" +
    Ioss::Utils::to_string(num_y) + "x" +
    Ioss::Utils::to_string(num_z) + "|bbox:0,0,0,1,1,1";
	
  PerceptMesh eMesh(3u);
  eMesh.new_mesh(GMeshSpec(config_mesh));
  eMesh.commit();
  // no need for this in create mode: eMesh.readBulkData();

  //FEMMetaData& metaData = *eMesh.get_fem_meta_data();
  mesh::BulkData& bulkData = *eMesh.get_bulk_data();

  eMesh.dump();
  GeometryVerifier geomVerifier(false);
  geomVerifier.isGeometryBad(bulkData, true);
  //setDoPause(true);
  //pause();
}
//======================================================================================================================
//======================================================================================================================
//======================================================================================================================
STKUNIT_UNIT_TEST(perceptMesh, open_new_reopen_PerceptMesh)
{
    EXCEPTWATCH;

    // start_demo_open_new_reopen_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;

    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();

    /// reopen the mesh to allow for more fields to be added - note that this involves a db write/read operation
    eMesh.reopen(output_files_loc+"optional_temp_filename.e");
    mesh::FieldBase* momentum_field = eMesh.add_field("momentum", stk::mesh::fem::FEMMetaData::NODE_RANK, vectorDimension);
    eMesh.commit();

    // create a field function from the new pressure field
    mesh::FieldBase *pressure_field = eMesh.get_field("pressure");
    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);

    // set the momentum field
    std::vector<double> momentum_values(3);
    momentum_values[0] = 2034.5;
    momentum_values[1] = 2134.5;
    momentum_values[2] = 2234.5;
    ConstantFunctionVec initMomentumValues(momentum_values, "initMomVal");

    // create a field function from the new momentum field
    FieldFunction ff_momentum("ff_momentum", momentum_field, eMesh, 3, 3);
    ff_momentum.interpolateFrom(initMomentumValues);

    // save
    eMesh.save_as(output_files_loc+"cube_with_pressure_and_momentum.e");
    eMesh.close();


    // end_demo

}
//======================================================================================================================
//======================================================================================================================
//======================================================================================================================
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

}
Esempio n. 5
0
/// This test uses a back door to the function that passes in the element to avoid the lookup of the element when the
///  StringFunction contains references to FieldFunctions
void TEST_norm_string_function_turbo_timings(TurboOption turboOpt)
{
  EXCEPTWATCH;
  //stk::diag::WriterThrowSafe _write_throw_safe(dw());
  //dw().setPrintMask(dw_option_mask.parse(vm["dw"].as<std::string>().c_str()));
  //dw().setPrintMask(LOG_NORM+LOG_ALWAYS);

  dw().m(LOG_NORM) << "TEST.norm.string_function " << stk::diag::dendl;

  /// create a meta data/bulk data empty pair
  PerceptMesh eMesh(3u);

  if (1)
  {
    // Need a symmetric mesh around the origin for some of the tests below to work correctly (i.e. have analytic solutions)
    const size_t nxyz = 4;
    const size_t num_x = nxyz;
    const size_t num_y = nxyz;
    const size_t num_z = nxyz;
    std::string config_mesh =
      Ioss::Utils::to_string(num_x) + "x" +
      Ioss::Utils::to_string(num_y) + "x" +
      Ioss::Utils::to_string(num_z) + "|bbox:-0.5,-0.5,-0.5,0.5,0.5,0.5";
	
    eMesh.new_mesh(GMeshSpec(config_mesh));

    eMesh.commit();
  }

  mesh::fem::FEMMetaData& metaData = *eMesh.get_fem_meta_data();
  mesh::BulkData& bulkData = *eMesh.get_bulk_data();

  /// the coordinates field is always created by the PerceptMesh read operation, here we just get the field
  mesh::FieldBase *coords_field = metaData.get_field<mesh::FieldBase>("coordinates");

  /// create a field function from the existing coordinates field
  FieldFunction ff_coords("ff_coords", coords_field, &bulkData,
                          Dimensions(3), Dimensions(3), FieldFunction::SIMPLE_SEARCH );

  /// the function to be integrated:  sqrt(Integral[x^2, dxdydz]) =?= sqrt(x^3/3 @ [-0.5, 0.5]) ==> sqrt(0.25/3)
  StringFunction sfx("x", Name("sfx"), Dimensions(3), Dimensions(1) );

  ff_coords.add_alias("mc");
  //StringFunction sfcm("sqrt(mc[0]*mc[0]+mc[1]*mc[1]+mc[2]*mc[2])", Name("sfcm"), Dimensions(3), Dimensions(1));
  StringFunction sfx_mc("mc[0]", Name("sfx_mc"), Dimensions(3), Dimensions(1) );

  /// the function to be integrated:  sqrt(Integral[x^2, dxdydz]) =?= sqrt(x^3/3 @ [-0.5, 0.5]) ==> sqrt(0.25/3)

  /// A place to hold the result.
  /// This is a "writable" function (we may want to make this explicit - StringFunctions are not writable; FieldFunctions are
  /// since we interpolate values to them from other functions).
  ConstantFunction sfx_res(0.0, "sfx_res");
  ConstantFunction sfx_res_turbo(0.0, "sfx_res_turbo");
  ConstantFunction sfx_res_slow(0.0, "sfx_res_slow");
  ConstantFunction sfx_res_fast(0.0, "sfx_res_fast");

#define COL_SEP "|"
#define EXPR_CELL_WIDTH (80)

#define TIME_IT2(expr_none,expr_turbo,msg,topt)                         \
  {                                                                     \
    double TURBO_NONE_time    = 0;                                      \
    double TURBO_ON_time = 0;                                           \
    TIME_IT(expr_none,TURBO_NONE_time);                                 \
    TIME_IT(expr_turbo,TURBO_ON_time);                                  \
    if (1) std::cout << msg << #topt << "for expression= " << QUOTE(expr_none) << " timings= " << std::endl; \
    if (1) std::cout << "TURBO_NONE_time= " << TURBO_NONE_time << " "   \
                     << ( turboOpt==TURBO_ELEMENT?"TURBO_ELEMENT_time":"TURBO_BUCKET_time") <<"= " << TURBO_ON_time \
                     << " ratio= " << TURBO_NONE_time/TURBO_ON_time << std::endl; \
  }

  int numIter = 1;
  for (int iter = 0; iter < numIter; iter++)
  {
    /// Create the operator that will do the work
    /// get the l2 norm
    Norm<2> l2Norm      (bulkData, &metaData.universal_part(), TURBO_NONE);
    Norm<2> l2Norm_turbo(bulkData, &metaData.universal_part(), turboOpt);

    //double TURBO_ELEMENT_time=0;
    TIME_IT2(l2Norm(sfx, sfx_res); , l2Norm_turbo(sfx, sfx_res_turbo);, "Should be the same turboOpt= ", turboOpt );