


{"id":358956,"date":"2024-03-26T08:15:23","date_gmt":"2024-03-26T08:15:23","guid":{"rendered":"\/forum\/forums\/reply\/358956\/"},"modified":"2024-03-26T08:15:23","modified_gmt":"2024-03-26T08:15:23","slug":"358956","status":"publish","type":"reply","link":"https:\/\/innovationspace.ansys.com\/forum\/forums\/reply\/358956\/","title":{"rendered":"Reply To: coupled simulation of vehicle and fluid tank"},"content":{"rendered":"<p>&lt;div&gt;&lt;div&gt;#include &#8220;udf.h&#8221;&lt;\/div&gt;&lt;div&gt;#include &lt;stdio.h&gt; \/\/ Include the standard I\/O library for file operations&lt;\/div&gt;&lt;div&gt;DEFINE_ADJUST(coupled_simulation1, d)&lt;\/div&gt;&lt;div&gt;{&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real simulation_time = CURRENT_TIME; \/\/ Get the current simulation time&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real dt = RP_Get_Real(&#8220;flow-time-step&#8221;); \/\/ Get the flow time step&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real end_time = RP_Get_Real(&#8220;flow-time-end&#8221;); \/\/ End time of simulation&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real braking_duration = 2.0; \/\/ Duration of braking force (seconds)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Open file for writing&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; FILE *fp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; fp = fopen(&#8220;E:\\coupled trial 3\\50 prcnt_files\\dp0\\FFF\\Fluent\\libudf6\\result\\simulation_results.txt&#8221;, &#8220;w&#8221;);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; if (fp == NULL) \/\/ Check if file was opened successfully&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; Message(&#8220;Error opening file.\\n&#8221;);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; return -1; \/\/ Exit initialization function&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; Domain *domain = Get_Domain(3); \/\/ Replace 1 with the appropriate domain ID&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; if (!domain)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; Message(&#8220;Error: Unable to obtain domain.\\n&#8221;);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fclose(fp); \/\/ Close the file before returning&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; return -1; \/\/ Exit initialization function&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Define vehicle parameters&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real m_truck = 5450.0; \/\/ Mass of the truck (kg)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real I_truck = 5000.0; \/\/ Moment of inertia of the truck (kg*m^2)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real k1 = 2.86e10; \/\/ Front spring stiffness (N\/m)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real k2 = 3.870e5; \/\/ Rear spring stiffness (N\/m)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real l1 = 0.2; \/\/ Distance of front axle from CG (m)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real l2 = 0.8; \/\/ Distance of rear axle from CG (m)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real F_braking = 27000.0; \/\/ Braking force (N)&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Define fluid parameters&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real fluid_density = 1000.0; \/\/ Density of the fluid (kg\/m^3)&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Initial conditions for vehicle motion&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real x = 2.0; \/\/ Initial vertical displacement&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real theta = 2.0; \/\/ Initial angular displacement&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real x_dot = 0.0; \/\/ Initial vertical velocity&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; real theta_dot = 0.0; \/\/ Initial angular velocity&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; real fluid_force_x, fluid_force_y, fluid_force_z, fluid_moment_x, fluid_moment_y, fluid_moment_z;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; Thread *t;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; cell_t c;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; face_t f;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Main simulation loop&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; while (simulation_time &lt; end_time)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Step 1: Calculate vehicle response to braking force&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real F_brake = (simulation_time &lt; braking_duration) ? F_braking : 0.0; \/\/ Apply braking force only within the braking duration&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real x_ddot = (-k1 * (x &#8211; l1 * theta) &#8211; k2 * (x + l2 * theta) + F_braking) \/ m_truck;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real theta_ddot = (-k1 * l1 * (x &#8211; l1 * theta) + k2 * l2 * (x + l2 * theta)) \/ I_truck;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Step 2: Apply vehicle response in terms of x(t) and theta(t) to fluid domain as a body force in the source term&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_force_x = 0.0; \/\/ Body force in x-direction &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_force_y = 0.0; \/\/ Body force in y-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_force_z = 0.0; &nbsp;\/\/ Body force in z-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_moment_x = 0.0; \/\/ vehicle moment in x-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_moment_y = 0.0; \/\/ vehicle moment in y-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; real vehicle_moment_z = 0.0; \/\/ vehicle moment in z-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Update the body force components based on vehicle response&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ For simplicity, assuming all the force acts in the z-direction&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; vehicle_force_y = -m_truck * x_ddot;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; vehicle_moment_z = -k1 * l1 * (x &#8211; l1 * theta) + k2 * l2 * (x + l2 * theta);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Apply the body force to the fluid domain&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; t = Lookup_Thread(domain, 3); \/\/ Assuming FLUID_THREAD_ID is the ID of the fluid thread&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; thread_loop_c(t, d)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 0) = vehicle_force_x;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 1) = vehicle_force_y;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 2) = vehicle_force_z;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Apply the moment components based on vehicle response&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Here, we&#8217;ll assume all the moments act around the z-axis&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 3) = 0.0; \/\/ Moment about x-axis&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 4) = 0.0; \/\/ Moment about y-axis&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_UDMI(c, t, 5) = vehicle_moment_z; \/\/ Moment about z-axis&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Step 3: Calculate fluid forces and moments&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_force_x = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_force_y = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_force_z = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_x = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_y = 0.0;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_z = 0.0;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Loop over fluid cells&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; thread_loop_c(t, d)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin_c_loop(c, t)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; real A[ND_ND], F[ND_ND], F_area[ND_ND]; &nbsp;\/\/ Add F_area vector to store face area&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; real p = C_P(c, t); \/\/ Pressure of the cell&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C_CENTROID(A, c, t); \/\/ Centroid of the cell&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; ND_ND; i++)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; F[i] = 0.0; \/\/ Initialize force vector&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Loop over faces of the cell using C_FACE macro&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int f = 0; f &lt; C_NFACES(c, t); f++)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; face_t face = C_FACE(c, t, f);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; F_AREA(F_area, face, t); \/\/ Face area vector&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Calculate face force&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; ND_ND; j++)&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; F[j] += p * F_area[j]; \/\/ Use F_area instead of calling F_AREA macro multiple times&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Accumulate forces and moments&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_force_x += F[0];&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_force_y += F[1];&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_force_z += F[2];&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_x += (A[1] * F[2] &#8211; A[2] * F[1]);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_y += (A[2] * F[0] &#8211; A[0] * F[2]);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fluid_moment_z += (A[0] * F[1] &#8211; A[1] * F[0]);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end_c_loop(c, t);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; }&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Step 4: Update vehicle response based on fluid forces and moments as excitation force on vehicle equations&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; x_dot += fluid_force_y \/ m_truck * dt;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; theta_dot += fluid_moment_z \/ I_truck * dt;&lt;\/div&gt;&lt;br&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; x += x_dot * dt;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; theta += theta_dot * dt;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Update simulation results in the file&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; fprintf(fp, &#8220;%.4f, %.4f\\n&#8221;, x, theta);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; printf(&#8220;Time: %.2f, x: %.4f, theta: %.4f\\n&#8221;, simulation_time, x, theta);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Increment simulation time&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; &nbsp; &nbsp; simulation_time += dt;&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; }&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; \/\/ Close the file after simulation&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; fclose(fp);&lt;\/div&gt;&lt;div&gt;&nbsp; &nbsp; return 0; \/\/ Successful initialization&lt;\/div&gt;&lt;div&gt;}&lt;\/div&gt;now this code is getting compiled and hooking, but during simulation i am getting very low value of force in order of 0.4 N. i have doubt regarding macro i have used. could you please suggest me use of define_adjust macro is suitable for this coupled simulation or not??&lt;\/div&gt;<\/p>\n","protected":false},"template":"","class_list":["post-358956","reply","type-reply","status-publish","hentry"],"aioseo_notices":[],"acf":[],"_links":{"self":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/replies\/358956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/replies"}],"about":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/types\/reply"}],"version-history":[{"count":0,"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/replies\/358956\/revisions"}],"wp:attachment":[{"href":"https:\/\/innovationspace.ansys.com\/forum\/wp-json\/wp\/v2\/media?parent=358956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}