用于正弦波生成的C编程

时间:2015-11-14 08:32:20

标签: c math simulation waveform sine

我编写了一个代码,用作ANSYS流畅模拟中的用户定义函数。该代码旨在在通道壁上产生正弦变形。根据以下等式

H(x) = a + b*Sin (2π/λ) (x - ct)

哪里

‘a’ is the average height of the channel 
‘b’ is the amplitude of the wave 
‘c’ is the wave propagation speed 
‘λ’ is the wavelength 
‘x’ is the stream wise direction of the flow ‘
t’ is current time

我面临的问题是"如何编写代码以便根据上面给出的等式移动2D通道墙上的每个节点"

#include "udf.h"
#include "dynamesh_tools.h"
DEFINE_GRID_MOTION(peristaltic, domain, dt, time, dtime)
{

    Thread *tf= DT_THREAD(dt);
    face_t f;
    Node *v;
    real NV_VEC(omega), NV_VEC(axis), NV_VEC(dx);
    real NV_VEC(origin), NV_VEC(rvec);
    real sign;
int n;
SET_DEFORMING_THREAD_FLAG(THREAD_T0(tf));
sign = 0.5 + 2*sin(6.28 * time);
Message ("time = %f, omega = %f\n", time, sign);
NV_S(omega, =, 0.0);
NV_D(axis, =, 1.0, 0.0, 0.0);
NV_D(origin, =, 0.0, 0.0, 0.0);

begin_f_loop(f,tf)
 {
f_node_loop(f,tf,n)
{   

v = F_NODE(f,tf,n);
if (NODE_POS_NEED_UPDATE (v))
{
/* indicate that node position has been update */
/*so that it's not updated more than once */
    NODE_POS_UPDATED(v);
NV_VV(rvec, =, NODE_COORD(v), -, origin);
NV_CROSS(dx, omega, rvec);
NV_S(dx, *=, dtime);
NV_V(NODE_COORD(v), +=, dx);
    }
     }
}   
end_f_loop(f,tf);
}

1 个答案:

答案 0 :(得分:1)

应该是

H(x) = a + b*Sin (2π/λ * x - wt)

H(x) = a + b*Sin ((2π/λ) * (x - ct))

对于每个频道 - 您需要在所有时间循环所有x值。

类似

for (n=0,x=min;n<100;n++;x+=(max-min)/99) 
{
  sign[n] = 0.5 + 2*sin(2*M_PI/lambda *(x - 6.28 * time));
}

其中sign现在是一个数组,你计算不同x值的许多通道的干扰,希望这有帮助

(注意波向+ ve x方向移动需要-ct,这首先是反直觉的)