正文

OpenFOAM——primitives——Vector类2006-11-15 11:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/bioexplore/20341.html

分享到:

接着上回介绍的VectorSpace类,今天介绍它的子类——Vector类。
该类也是一个模板类,仅一个模板参数<class Cmpt>
其父类为VectorSpace<Vector<Cmpt>, Cmpt, 3>
下面是其原代码描述

    Templated 3D Vector derived from VectorSpace adding construction from
    3 components, element access using x(), y() and z() member functions and
    the inner-product (dot-product) and cross product operators.

    A centre() member function which returns the Vector for which it is called
    is defined so that point which is a typedef to Vector<scalar> behaves as
    other shapes in the shape hierachy

公有属性(public)
      成员常量
              enum{rank=1};//表明秩为1,这里秩的概念清参阅OpenFOAM的ProgramersGuide
                                          第一章里的张量数学部分
               enum componets{X,Y,Z};//用于下标引用的枚举成员变量
      静态常量
              static const char* const typeName="vector"://矢量类的类名,用于标示该类
              static const char* componetNmaes[]={"x","y","z"};//矢量三个元素的名字
              static const Vector zero(0,0,0); //矢量中的零矢量
              static const Vector one(1,1,1); //矢量中的单位矢量
      构造函数
              Vector();//默认构造函数
              Vector(const VectorSpace<Vector<Cmpt>,Cmpt,3>&);//从父类构造
              Vector(const Cmpt& vx, const Cmpt& vy, const Cmpt& vz);//给定三个元素进行构造
              Vector(Istream&);//从输入流构造
       成员函数
              Cmpt& x() {return this->v_[X];}//返回矢量的x坐标值
              Cmpt& y() {return this->v_[Y];}//返回矢量的y坐标
              Cmpt& z() {return this->v_[Z];}//返回矢量的z坐标
              const Cmpt& x() const;//const版的x()
              const Cmpt& y() const;//const版的y()
              const Cmpt& z() const;//const版的z()
???  const Vector<Cmpt>& centre(const List<Vector<Cmpt> >&) const
                    {return *this;}//没有看懂

全局函数(当然是用在Vector<Cmpt>上的了,呵呵)
             template<class Cmpt>  //模板函数
             inline typename innerProduct<Vector<Cmpt>,Vector<Cmpt> >::type  //返回的类型
             operator&(const Vector<Cmpt>& v1,const Vector<Cmpt>& v2)//重载&实现内积
            {
                 return Cmpt(v1.x()*v2.x()+v1.y()*v2.y()+v1.z()*v2.z());
             }//该模板函数实现两个矢量之间的点乘也即内积
这里提到的innerProduct也是一个模板类,我们将在以后介绍;
             inline Vector<Cmpt> //返回的类型
             operator^(const Vector<Cmpt>& v1,const Vector<Cmpt>& v2)//实现叉乘           
            {
                 return Vector<Cmpt>
                             (
                                 (v1.y()*v2.z()-v1.z()*v2.y()),
                                 (v1.z()*v2.x()-v1.x()*v2z()),
                                 (v1.x()*v2.y-v1.y*v2.x())
                             );
             }//该函数实现两个矢量之间的叉乘
另外,OpenFOAM中还通过typedef定义了我们平常用到和提到的矢量
typedef Vector<Scalar> vector;

好了今天就写到这,该吃饭了,呵呵!



 

阅读(5655) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册