|
[Sponsors] |
August 9, 2016, 12:05 |
Simple UDF for piston movement
|
#1 |
New Member
Nicolas Ramirez
Join Date: May 2016
Posts: 1
Rep Power: 0 |
Dear all,
I'm working on an UDF with DEFINE_GRID_MOTION to move a boundary of a piston. This is the UDF: Code:
#include "udf.h" #include "unsteady.h" #include "dynamesh_tools.h" FILE *fp; DEFINE_GRID_MOTION(obliqueWall,domain,dt,time,dtime) { /*Declare Fluent parameters: face threat, nodes*/ Thread *tf= DT_THREAD(dt); face_t f; Node *v; /*Declare Area vector A and d_displacement vector dx*/ real NV_VEC(A); real NV_VEC(dx); /*Declare containers for variables*/ real amplitude; real x; real w; real waveform; /*Declare containers for constants*/ int n; float f1; float f2; float npx[176]; float npy[176]; int v1; int i; /*read node coordinates from the file */ fp = fopen("meshPointsPP.out", "r"); if (!fp) { printf ("Error while opening file %s\n", "meshPointsPP.out"); return 0; } while (fscanf (fp, "%d %f %f\n",&v1, &f1, &f2) == 3 ) { npx[v1]=f1; npy[v1]=f2; printf("begin assignation of values of node %d\n",v1); } fclose(fp); printf("finish: assignation of values\n"); /* calculate displacement vector at the node normal to the surface */ i=1; begin_f_loop(f,tf) { printf("begin loop in faces\n"); /* previous_time=PREVIOUS_TIME;*/ F_AREA(A,f,tf); x=time; w=2*M_PI; amplitude=5e-6; waveform = amplitude*sin(w*x); printf("time = %f, disp = %f\n", time, waveform); dx[0]=A[0]*0;/*/NV_MAG(A)*waveform;*/ dx[1]=A[1]/NV_MAG(A)*waveform;/*A[1]/NV_MAG(A)*sign;*/ f_node_loop(f,tf,n) { printf("begin: loop in nodes\n"); v = F_NODE(f,tf,n); /* update node if the current node has not been previously */ /* visited when looping through previous faces */ 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); NODE_COORD(v)[0]=dx[0]+npx[i]; NODE_COORD(v)[1]=dx[1]+npy[i]; i++; } } } end_f_loop(f,tf); } 1, 0.000000000E+00, 0.000000000E+00 2, 2.000000000E-04, 0.000000000E+00 3, 4.000000000E-04, 0.000000000E+00 4, 6.000000000E-04, 0.000000000E+00 (...) I compile the UDF and assign it to the movingWall zone in dynamic mesh zone. Also enabling "Deform adjacent boundary layer with zone" and using only layering. I set up the transient case with a time step size of 0.001 [s] and 100 steps, [BUT] the mesh deforms oddly and I have negative volume cells in the first time step. My mesh is (initially) a square of 35 by 20 mm. Cheers! |
|
October 7, 2016, 10:36 |
dynamesh/in-cyn/crank-rpm PROBLEME
|
#2 |
New Member
Gabes
Join Date: May 2016
Posts: 10
Rep Power: 10 |
# include "udf.h"
# define RPM RP_Get_Real("dynamesh/in-cyn/crank-rpm") /********************************* User input starts *****************************************/ /* Cell zone ID list for pressure output (cell zone ID for combustion chamber). -1 is a flag so please keep it. */ static int Zone_ID[]={2, -1}; /* Face zone ID list for work output (face zone ID for the piston) */ static int Piston_ID[]={6, -1}; /********************************** User input ends ******************************************/ static real work=0, start_CA=0, end_CA=0; static int counter=0; /* Function to calculate cell volume. It takes care of 2d and 2d axisymmetric case as well. */ static real C_MYVOLUME(cell_t c, Thread *tc) { real factor=1.0; #if RP_2D if (rp_axi) factor=2*M_PI; #endif return C_VOLUME(c,tc)*factor; } /* Function to calculate face area. It takes care of 2d and 2d axisymmetric case as well. */ static void F_MYAREA(real * x, face_t f, Thread *tf) { real factor=1.0; F_AREA(x, f, tf); #if RP_2D if (rp_axi) factor=2*M_PI; #endif NV_S(x, *=, factor); } DEFINE_ON_DEMAND(Indicated_work) { if(counter==0) { Message0("\n\n********************** IC Indicated Work Results *****************************\n"); Message0("\nIndicated work calculation has not started. Please do at least one time step.\n"); Message0("\nStart CA :%5.2f (deg) End CA :%5.2f (deg) Work : %9.3e (J)", start_CA, end_CA, work); Message0("\n\n************************************ ******************************************\n"); } else { Message0("\n\n********************** IC Indicated Work Results *****************************\n"); Message0("\nIndicated work calculation started at :%7.2f (deg)", start_CA); Message0("\nIndicated work calculation finished at :%7.2f (deg)", end_CA); Message0("\nFor the above duration, the indicated work is :%12.4e (J)", work); Message0("\n\n************************************ ******************************************\n"); } } DEFINE_ON_DEMAND(reset) { counter=0; work=0; start_CA=0; end_CA=0; Message0("\n\n******************************** WARNING *************************************\n"); Message0("\nNote that this resets indicated work and the calculation start CA to zero."); Message0("\nPlease save the data file to save the new values."); Message0("\n\n************************************ ******************************************\n"); } DEFINE_EXECUTE_AT_END(output_results) { #if !RP_HOST int i; real pressure, volume, fmf, mass, work_one_dt, x[ND_ND]; Thread *tc, *tf; cell_t c; face_t f; FILE *fp_results; Domain* domain; domain=Get_Domain(1); #if PARALLEL if(I_AM_NODE_ZERO_P) #endif { if(!(fp_results=fopen("work.txt","a"))) { Message0("\nCan not open file-aborting!!"); exit(0); } } if(counter==0) { start_CA = (CURRENT_TIME-CURRENT_TIMESTEP)*RPM*6.0+RP_Get_Real("dynamesh/in-cyn/crank-start-angle"); #if PARALLEL if(I_AM_NODE_ZERO_P) #endif { fprintf(fp_results, " CA Volume Pressure Yfb Work\n"); } counter ++; } end_CA=CURRENT_TIME*RPM*6.0+RP_Get_Real("dynamesh/in-cyn/crank-start-angle"); #endif node_to_host_int_1(counter); node_to_host_real_2(start_CA, end_CA); #if !RP_HOST /* Calculate volume weighted pressure and burnt fuel mass fraction */ pressure=0; volume=0; fmf=0; mass=0; i=0; while(Zone_ID[i]>=0) { tc=Lookup_Thread(domain, Zone_ID[i]); begin_c_loop_int(c, tc) { pressure += C_P(c,tc) * C_MYVOLUME(c,tc); volume += C_MYVOLUME(c,tc); if(sg_premixed) { fmf += C_PREMIXC(c,tc) * C_R(c,tc) * C_MYVOLUME(c,tc); } mass += C_R(c,tc) * C_MYVOLUME(c,tc); } end_c_loop_int(c, tc) i++; } pressure = PRF_GRSUM1(pressure); volume = PRF_GRSUM1(volume); fmf = PRF_GRSUM1(fmf); mass = PRF_GRSUM1(mass); pressure /= volume; fmf /= mass; /* Calcualte work by piston. Only pressure force is accounted for and viscous force is neglected. */ work_one_dt=0; i=0; while(Piston_ID[i]>=0) { tf=Lookup_Thread(domain, Piston_ID[i]); begin_f_loop(f, tf) { F_MYAREA(x, f, tf); work_one_dt += CURRENT_TIMESTEP * F_P(f,tf) * NVD_DOT(x, WALL_F_GRID_VV(f, tf)[0], WALL_F_GRID_VV(f, tf)[1], WALL_F_GRID_VV(f, tf)[2]); } end_f_loop(f, tf) i++; } work_one_dt = PRF_GRSUM1(work_one_dt); work += work_one_dt; #endif node_to_host_real_1(work); #if !RP_HOST /* Output volume, pressure, burnt fuel mass fraction */ #if PARALLEL if(I_AM_NODE_ZERO_P) #endif { fprintf(fp_results, "%8.2f %12.4e %12.4e %12.4e %12.4e\n", end_CA, volume, pressure, fmf, work); fclose(fp_results); } #endif } DEFINE_RW_FILE(write_data, fp) { Message0("\nWriting user defined data to the data file...\n"); #if PARALLEL #if RP_HOST fprintf(fp, "\n%d", counter); fprintf(fp, "\n%e", work); fprintf(fp, "\n%e", start_CA); fprintf(fp, "\n%e", end_CA); #endif #else fprintf(fp, "\n%d", counter); fprintf(fp, "\n%e", work); fprintf(fp, "\n%e", start_CA); fprintf(fp, "\n%e", end_CA); #endif } DEFINE_RW_FILE(read_data, fp) { Message0("\nReading user defined data from the data file...\n"); #if PARALLEL #if RP_HOST fscanf(fp, "%d", &counter); fscanf(fp, "%e", &work); fscanf(fp, "%e", &start_CA); fscanf(fp, "%e", &end_CA); #endif #else fscanf(fp, "%d", &counter); fscanf(fp, "%e", &work); fscanf(fp, "%e", &start_CA); fscanf(fp, "%e", &end_CA); #endif host_to_node_int_1(counter); host_to_node_real_3(work,start_CA,end_CA); } ERROR IS ..\..\src\work.c(195) : error C2109: subscript requires array or pointer type |
|
Tags |
dynamic mesh zones, grid motion, udf crash |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issues with UDF for ball movement inside a valve | A.Jalal | Fluent UDF and Scheme Programming | 1 | July 30, 2015 01:11 |
UDF for 2D translational movement | ThinkQuick | Fluent UDF and Scheme Programming | 0 | July 24, 2012 01:59 |
Simple UDF array question [Fluent] | Patrick1 | Fluent UDF and Scheme Programming | 0 | April 19, 2012 01:41 |
Pb with simple Fluent UDF example for inlet | francois | FLUENT | 0 | October 9, 2008 10:34 |
simple rigid body motion UDF | Dan | FLUENT | 0 | January 11, 2006 05:48 |