正文

OF描述具量纲变量的模板类——dimesioned<Type>2008-09-04 21:29:00

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

分享到:

dimensioned<Type>默板类 该类是实现具有量纲变量的类,在给变量值加量纲的同时还给变量加了名字。OF通过该类模板实现标量,矢量,张量三种类型变量的量纲化。相应的在OF中具体实例化的类分别为dimensionedScalar,dimensionedVector和dimensionedTensor。   该类模板中提供了三个私有变量: word name_;             //保存变量的名字 dimesionSet dimensions_;    //保存变量的量纲 Type value_;            //保存变量值,类型由模板参数Type指出   构造函数(4个):   dimensioned(const word&, const dimensionSet&, const Type);  //给定名字,量纲和变量值进行构造   dimensioned(const word&, const dimensioned<Type>&);   //量纲和值取自给定的具量纲变量对象,并给其重新命名   dimensioned(const Type& t)   : name_(::Foam::name(t)),     dimensions_(dimless),     value_(t)   {}            //仅给出变量值,将构造一个无量纲名字为变量值的类对象   dimensioned(Istream&); //从输入流中读入数据进行对象的构造   成员访问函数对外接口(3*2个): const word& name() const;       //返回变量的名字,只读 word& name();               //返回变量的名字,可读写,可以更改变量的名字 const dimensionSet& dimensions() const; //返回变量的量纲,只读 dimensionSet& dimensions();     //返回变量的量纲,可读写 const Type& value() const;      //返回变量的值,只读 Type& value();          //返回变量的值,可读写   针对矢量和张量的成员函数: dimensioned<cmptType> component(const direction d) const;   //返回direction指名的矢量或张量具量纲变量的相应元素,量纲同矢量 void replace(const direction,const dimensioned<cmptType>& dcomp);   //对direction对应的元素用dcomp代替 dimensioned<Type> T() const;    //对变量进行转秩操作,并返回转秩结果,只对特例化的类适用,即只在特例化类中进行定义   成员操作符函数(5个): dimensioned<cmptType> operator[](const direction d) const;  //调用comonent(d) void operator+=(const dimensioned<Type>&);  //为该模板类实例化类提供+=操作符 void operator-=(const dimensioned<Type>&);  //为该模板类实例化类提供-=操作符 void operator*=(const scalar);      //为该模板类实例化类提供其与无量纲标量的*=操作 void operator/=(const scalar);      //为该模板类实例化类提供其与无量纲标量的/=操作   友元输入/输出函数(2个): friend Istream& operator>><Type>(Istream& is,dimensioned<Type>& dt);        //实现从输入流is中读入并构造dt的>>输入操作符 frient Ostream& operator<<<Type>(Ostream& os,const dimensioned<Type>& dt);  //实现将dt输出到输出流os中的>>输出操作符   全局[操作符]函数(13个): template<class Type, int r> dimensioned<typename powProduct<Type, r>::type> pow (     const dimensioned<Type>& dt,     typename powProduct<Type, r>::type   = pTraits<typename powProduct<Type, r>::type>::zero );          //求dt的r次方幂,源代码中该函数实现中,返回的变量值为pow(dt,2),我觉得这可能是个错误,2应该是r   template<class Type> dimensioned<typename outerProduct<Type, Type>::type> sqr(const dimensioned<Type>& dt);   //求dt的开方,这里用到了dimensionSet的sqr,即变量值及量纲都进行开方运算   template<class Type> dimensioned<scalar> magSqr(const dimensioned<Type>& dt);    //求dt的magSqr,这里分别对其量纲及变量数值进行magSqr运算,得到的结果是一个dimensionedScalar   template<class Type> dimensioned<scalar> mag(const dimensioned<Type>& dt);       //求dt的mag,这里变量量纲不变,变量值进行mag运算,返回一个dimensionedScalar   template<class Type> dimensioned<Type> scale(const dimensioned<Type>&, const dimensioned<Type>&);    //??未发现其实现代码   template<class Type> dimensioned<Type> max(const dimensioned<Type>&, const dimensioned<Type>&);      //在两变量量纲相同前提下返回两个变量的值中较大的一个   template<class Type> dimensioned<Type> min(const dimensioned<Type>&, const dimensioned<Type>&);      //在两变量量纲相同前提下返回两个变量的值中较小的一个   template<class Type> bool operator>(const dimensioned<Type>&, const dimensioned<Type>&);         //不论量纲是否相同,只比较两变量数值大小   template<class Type> bool operator<(const dimensioned<Type>&, const dimensioned<Type>&);         //不论量纲是否相同,只比较两变量数值大小   template<class Type> dimensioned<Type> operator+(const dimensioned<Type>&, const dimensioned<Type>&);    //量纲相加(dimensionSet的功能),变量值相加   template<class Type> dimensioned<Type> operator-(const dimensioned<Type>&);      //量纲取负运算(dimensionSet的功能),变量值取负   template<class Type> dimensioned<Type> operator-(const dimensioned<Type>&, const dimensioned<Type>&);    //量纲相减(dimensionSet的功能),变量值相减   template<class Type> dimensioned<Type> operator/ (     const dimensioned<Type>&,     const dimensioned<scalar>& );                  //量纲相除(dimensionSet的功能),变量值相除   template<class Type> dimensioned<Type> operator* (     const dimensioned<scalar>&,     const dimensioned<Type>& );                  //量纲相乘(dimensionSet的功能),变量值相乘     //以下是为该模板类提供的宏,用于实现dimensionedType与dimensionedType,dimensionedType与VectorSpace,VectorSpace与deimensionedType之间的各类乘积运算的模板函数,如外积(*),叉积(^),点积(&),和双点积(&&),相应的运算法则可以参考OF的programer guide #define PRODUCT_OPERATOR(product, op, opFunc)                                 \                                                                               \ template<class Type1, class Type2>                                            \ dimensioned<typename product<Type1, Type2>::type>                             \ operator op(const dimensioned<Type1>&, const dimensioned<Type2>&);            \                                                                               \ template<class Type, class Form, class Cmpt, int nCmpt>                       \ dimensioned<typename product<Type, Form>::type>                               \ operator op                                                                   \ (                                                                             \     const dimensioned<Type>&,                                                 \     const VectorSpace<Form,Cmpt,nCmpt>&                                       \ );                                                                            \                                                                               \ template<class Type, class Form, class Cmpt, int nCmpt>                       \ dimensioned<typename product<Form, Type>::type>                               \ operator op                                                                   \ (                                                                             \     const VectorSpace<Form,Cmpt,nCmpt>&,                                      \     const dimensioned<Type>&                                                  \ );   PRODUCT_OPERATOR(outerProduct, *, outer) PRODUCT_OPERATOR(crossProduct, ^, cross) PRODUCT_OPERATOR(innerProduct, &, dot) PRODUCT_OPERATOR(scalarProduct, &&, dotdot)      

阅读(5794) | 评论(1)


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

评论

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