CFD中文网

    CFD中文网

    • 登录
    • 搜索
    • 最新

    在使用codedFixedValue边界条件时,如何获得网格中心坐标?

    OpenFOAM
    4
    9
    432
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 知
      知白 最后由 编辑

      大家好,我在学习一个shotSleeveModeling的动网格案例,几何模型如下,运动边界为movingWalls;

      Pasted image 20220816214329.png

      我采用codedFixedValue边界条件,实现了movingWalls边界的往复运动,pointDisplacement和dynamicMeshDict文件的部分内容如下:

      //pointDisplacement
      boundaryField
      {
      
          "(outlet|fixedWalls)"
          {
              type       fixedValue;
              value     uniform (0 0 0);
          }
      
          movingWalls
          {
              type    codedFixedValue;
              value   uniform (0 0 0);
              name    name_of_BC;
      
              code
              #{
                  //const fvPatch & boundaryPatch = patch();
                  //const Patch & boundaryPatch = patch();
                  //const vectorField & Cf = boundaryPatch.Cf(); 
      
                  vectorField & field = *this;
                  scalar U_0 = 0.1, p_r = 0.03;
                  scalar t = this->db().time().value();
      
                  forAll(field, faceI)
                  {
                      //const scalar x = boundaryPatch.Cf()[faceI][0];
                      //const scalar y = boundaryPatch.Cf()[faceI][1];
                      //const scalar z = boundaryPatch.Cf()[faceI][2];
      
                      field[faceI].x() = 0;
                      field[faceI].y() = 0;
                      field[faceI].z() = U_0*sin(t);
                      //field[faceI].z() = U_0*sin(t)*pow(field[faceI].y(),2)/(p_r*p_r);
                  }
              #};
          }
          slipWalls
          {
      
              type            calculated;
          }
      
          frontAndBack
          {
              //type            empty;
              type            calculated;
          }
      }
      
      //dynamicMeshDict
      dynamicFvMesh       dynamicMotionSolverFvMesh; 
      motionSolverLibs ( "libfvMotionSolvers.so" );
      motionSolver          displacementLayeredMotion;
      cellZone movingCells;
      displacementLayeredMotionCoeffs
      {
          regions
          {
              movingCells
              {
                  interpolationScheme linear;
                  boundaryField
                  {
                      movingFaces { type follow; }
                      boundaryMovingFixed { type  fixedValue; value uniform (0 0 0); }
                  }
              }
          }
      }
      

      movingWalls边界的往复运动如下图所示:

      shot2.gif

      在实现往复运动的基础上,我想让movingWalls沿y轴方向从中间到两边的网格运动速度递减,呈一个抛物线型,因此我需要获得网格中心坐标;

      首先,我在pointDisplacement->movingWalls->code#{ #}中增加如下语句,会报错:

      const fvPatch & boundaryPatch = patch();
      

      不过按照提示将上述语句中的fvPatch改成Patch,就可以解决错误了。

      但是当我在pointDisplacement->movingWalls->code#{ #}中再增加如下语句,用来获得网格中心坐标时,也会报错:

      const vectorField & Cf = boundaryPatch.Cf(); //网格中心
      

      报错内容为:
      /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement.boundaryField.movingWalls:40:52: error: ‘const Patch’ {aka ‘const class Foam::pointPatch’} has no member named ‘Cf’

      我用了OpenFOAM9和OpenFOAM v2112,都会出错。按照提示,应该是Patch下面没有Cf这个成员,但是我看了很多教程获取网格坐标都是用的Cf。我卡在这很久了,都不知道要怎么修改,希望有知道的大神能指导下,多谢!!!

      1 条回复 最后回复 回复 引用
      • 李东岳
        李东岳 管理员 最后由 编辑

        mesh.Cf()是带边界场的,这个含有边界的信息,应该是这样:

        mesh.Cf().boundaryField()[你的patch编号];
        

        CFD高性能服务器 http://dyfluid.com/servers.html

        知 1 条回复 最后回复 回复 引用
        • 知
          知白 @李东岳 最后由 编辑

          @李东岳 李老师好!
          OpenFOAM我刚开始学,所以不是很懂,不知道您说的是不是这样使用,可以获得网格x方向的坐标:

           forAll(field, faceI)
          {
                const scalar x = mesh.Cf().boundaryField()[faceI][0];
          
                 field[faceI].x() = 0;
                 field[faceI].y() = 0;
                 field[faceI].z() = U_0*sin(t);
           }
          

          在这样使用时会报错:
          error: ‘mesh’ was not declared in this scope

          请问这是为什么。

          李东岳 1 条回复 最后回复 回复 引用
          • 李东岳
            李东岳 管理员 @知白 最后由 编辑

            @知白 你这一行代码可以在求解器层面写。你要是在src的库里面写,你要加个类似这种:

            const fvMesh& mesh = U.mesh();
            
            

            CFD高性能服务器 http://dyfluid.com/servers.html

            知 1 条回复 最后回复 回复 引用
            • 知
              知白 @李东岳 最后由 编辑

              @李东岳 李老师您意思是要使用下面代码,需要重新编译求解器,不能只在pointDisplacement里直接修改边界条件吗?

               forAll(field, faceI)
              {
                    const scalar x = mesh.Cf().boundaryField()[faceI][0];
              
                     field[faceI].x() = 0;
                     field[faceI].y() = 0;
                     field[faceI].z() = U_0*sin(t);
               }
              

              我看教程里,只是修改了边界条件,没有重新编译求解器:
              4d93a015-754c-462a-915c-00b3007f470c-image.png

              1 条回复 最后回复 回复 引用
              • 李东岳
                李东岳 管理员 最后由 编辑

                你要在边界条件里面写么,那这样:

                const fvPatch& boundaryPatch = patch();
                const fvBoundaryMesh& boundaryMesh = boundaryPatch.boundaryMesh();
                const fvMesh& mesh = boundaryMesh.mesh();
                

                这样可以调用mesh

                CFD高性能服务器 http://dyfluid.com/servers.html

                知 1 条回复 最后回复 回复 引用
                • 知
                  知白 @李东岳 最后由 编辑

                  @李东岳 请问李老师,codedFixedValue边界条件在U文件和pointDisplacement文件中获得网格坐标的方法是不是不通用,同样的代码,如:

                          type    codedFixedValue;
                          value   uniform (0 0 0);
                          name    name_of_BC;
                          code
                          #{
                              const Patch& boundaryPatch = patch();
                              const vectorField& Cf = patch().Cf();       
                          #};
                  

                  在U文件里可以使用,在pointDisplacement文件里就会出错。

                  出错内容如下:

                  Create time
                  
                  Create mesh for time = 0
                  
                  Selecting dynamicFvMesh dynamicMotionSolverFvMesh
                  Selecting motion solver: displacementLayeredMotion
                  Using dynamicCode for codedFixedValue name_of_BC at line 32 in "/mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls"
                  Creating new library in "dynamicCode/name_of_BC/platforms/linux64GccDPInt32Opt/lib/libname_of_BC_2749dd0d9becc7c4794a34f8159e9b4cbe66efce.so"
                  "/home/dyfluid/OpenFOAM/OpenFOAM-9/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.C" "/mnt/hgfs/share/shotSleeveModeling3/dynamicCode/name_of_BC/codedFixedValuePointPatchFieldTemplate.C"
                  "/home/dyfluid/OpenFOAM/OpenFOAM-9/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.H" "/mnt/hgfs/share/shotSleeveModeling3/dynamicCode/name_of_BC/codedFixedValuePointPatchFieldTemplate.H"
                  Invoking "wmake -s libso /mnt/hgfs/share/shotSleeveModeling3/dynamicCode/name_of_BC"
                  wmake libso /mnt/hgfs/share/shotSleeveModeling3/dynamicCode/name_of_BC
                      ln: ./lnInclude
                  ln: failed to create symbolic link './codedFixedValuePointPatchFieldTemplate.C': Operation not supported
                  ln: failed to create symbolic link './codedFixedValuePointPatchFieldTemplate.H': Operation not supported
                      wmkdep: codedFixedValuePointPatchFieldTemplate.C
                      Ctoo: codedFixedValuePointPatchFieldTemplate.C
                  /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls: In member function ‘virtual void Foam::name_of_BCFixedValuePointPatchVectorField::updateCoeffs()’:
                  /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls:39:45: error: ‘const class Foam::pointPatch’ has no member named ‘Cf’
                  /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls:37:14: warning: unused variable ‘boundaryPatch’ [-Wunused-variable]
                  /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls:39:32: warning: unused variable ‘Cf’ [-Wunused-variable]
                  make: *** [/home/dyfluid/OpenFOAM/OpenFOAM-9/wmake/rules/General/transform:26: Make/linux64GccDPInt32Opt/codedFixedValuePointPatchFieldTemplate.o] Error 1
                  
                  
                  --> FOAM FATAL IO ERROR: 
                  Failed wmake "dynamicCode/name_of_BC/platforms/linux64GccDPInt32Opt/lib/libname_of_BC_2749dd0d9becc7c4794a34f8159e9b4cbe66efce.so"
                  
                  
                  file: /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls from line 32 to line 36.
                  
                      From function void Foam::codedBase::createLibrary(Foam::dynamicCode&, const Foam::dynamicCodeContext&) const
                      in file db/dynamicLibrary/codedBase/codedBase.C at line 209.
                  
                  FOAM exiting
                  

                  其中最关键的感觉是这几处:

                  ln: failed to create symbolic link './codedFixedValuePointPatchFieldTemplate.C': Operation not supported
                  ln: failed to create symbolic link './codedFixedValuePointPatchFieldTemplate.H': Operation not supported
                  
                  /mnt/hgfs/share/shotSleeveModeling3/0/pointDisplacement/boundaryField/movingWalls:39:45: error: ‘const class Foam::pointPatch’ has no member named ‘Cf’
                  

                  在U文件使用上述代码,只会出现第一处提示,不会出现第二处错误。

                  Z 1 条回复 最后回复 回复 引用
                  • Z
                    Z.Weng @知白 最后由 李东岳 编辑

                    @知白

                    const fvMesh& mesh = refCast<const fvMesh>(this->db());
                    

                    赋值建议采用

                    operator==(field);
                    

                    具体可以参考codedFixedValuePointPatchField.H的注释

                    Example:
                    
                    movingWall
                    {
                        type            codedFixedValue;
                        value           uniform 0;
                        name    rampedFixedValue;   // name of generated bc
                    
                        code
                        #{
                            operator==
                            (
                                vector(0,0,1)
                                *min(10, 0.1*this->db().time().value())
                            );
                        #};
                    
                        // codeInclude
                        //#{
                        //    #include "fvCFD.H"
                        //#};
                    
                        // codeOptions
                        //#{
                        //    -I$(LIB_SRC)/finiteVolume/lnInclude
                        //#};
                    }
                    1 条回复 最后回复 回复 引用
                    • 田畔的风
                      田畔的风 讲师 最后由 编辑

                      pointMotionU和pointDisplacement这些控制动网格节点的边界条件文件的数据类型是pointVectorField,对应的patch类是Foam::pointPatch而不是Foam::fvPatch,这应该就是报错的根本原因。

                      我的思路如下:

                      // 获取pointPatch的ID,它和相同边界上的fvPatch的ID是一致的
                      label patchIndex = patch().index();  
                      
                      // 在objectRegistry随意获取一个存在的物理场
                      const volScalarField & p
                      (
                          this->db().objectRegistry::lookupObject<volScalarField>("p");
                      );
                      
                      // 从上述物理场中访问fvMesh中的体心/面心场
                      const vectorField& Cfp = p.mesh().Cf().boundaryField()[patchIndex];
                      
                      1 条回复 最后回复 回复 引用
                      • First post
                        Last post