Ansys Learning Forum › Forums › Discuss Simulation › Fluids › coupled simulation of vehicle and fluid tank › Reply To: coupled simulation of vehicle and fluid tank
March 26, 2024 at 8:15 am
vikram chahal
Subscriber
#include "udf.h"
#include // Include the standard I/O library for file operations
DEFINE_ADJUST(coupled_simulation1, d)
{
  real simulation_time = CURRENT_TIME; // Get the current simulation time
  real dt = RP_Get_Real("flow-time-step"); // Get the flow time step
  real end_time = RP_Get_Real("flow-time-end"); // End time of simulation
  real braking_duration = 2.0; // Duration of braking force (seconds)
  // Open file for writing
  FILE *fp;
  fp = fopen("E:\coupled trial 3\50 prcnt_files\dp0\FFF\Fluent\libudf6\result\simulation_results.txt", "w");
  if (fp == NULL) // Check if file was opened successfully
  {
    Message("Error opening file.\n");
    return -1; // Exit initialization function
  }
  Domain *domain = Get_Domain(3); // Replace 1 with the appropriate domain ID
  if (!domain)
  {
    Message("Error: Unable to obtain domain.\n");
    fclose(fp); // Close the file before returning
    return -1; // Exit initialization function
  }
  // Define vehicle parameters
  real m_truck = 5450.0; // Mass of the truck (kg)
  real I_truck = 5000.0; // Moment of inertia of the truck (kg*m^2)
  real k1 = 2.86e10; // Front spring stiffness (N/m)
  real k2 = 3.870e5; // Rear spring stiffness (N/m)
  real l1 = 0.2; // Distance of front axle from CG (m)
  real l2 = 0.8; // Distance of rear axle from CG (m)
  real F_braking = 27000.0; // Braking force (N)
  // Define fluid parameters
  real fluid_density = 1000.0; // Density of the fluid (kg/m^3)
  // Initial conditions for vehicle motion
  real x = 2.0; // Initial vertical displacement
  real theta = 2.0; // Initial angular displacement
  real x_dot = 0.0; // Initial vertical velocity
  real theta_dot = 0.0; // Initial angular velocity
  real fluid_force_x, fluid_force_y, fluid_force_z, fluid_moment_x, fluid_moment_y, fluid_moment_z;
  Thread *t;
  cell_t c;
  face_t f;
  // Main simulation loop
  while (simulation_time < end_time)
  {
    // Step 1: Calculate vehicle response to braking force
    real F_brake = (simulation_time < braking_duration) ? F_braking : 0.0; // Apply braking force only within the braking duration
    real x_ddot = (-k1 * (x - l1 * theta) - k2 * (x + l2 * theta) + F_braking) / m_truck;
    real theta_ddot = (-k1 * l1 * (x - l1 * theta) + k2 * l2 * (x + l2 * theta)) / I_truck;
    // Step 2: Apply vehicle response in terms of x(t) and theta(t) to fluid domain as a body force in the source term
    real vehicle_force_x = 0.0; // Body force in x-direction  Â
    real vehicle_force_y = 0.0; // Body force in y-direction
    real vehicle_force_z = 0.0;  // Body force in z-direction
    real vehicle_moment_x = 0.0; // vehicle moment in x-direction
    real vehicle_moment_y = 0.0; // vehicle moment in y-direction
    real vehicle_moment_z = 0.0; // vehicle moment in z-direction
    // Update the body force components based on vehicle response
    // For simplicity, assuming all the force acts in the z-direction
    vehicle_force_y = -m_truck * x_ddot;
    vehicle_moment_z = -k1 * l1 * (x - l1 * theta) + k2 * l2 * (x + l2 * theta);
    // Apply the body force to the fluid domain
    t = Lookup_Thread(domain, 3); // Assuming FLUID_THREAD_ID is the ID of the fluid thread
    thread_loop_c(t, d)
    {
      C_UDMI(c, t, 0) = vehicle_force_x;
      C_UDMI(c, t, 1) = vehicle_force_y;
      C_UDMI(c, t, 2) = vehicle_force_z;
     Â
       // Apply the moment components based on vehicle response
      // Here, we'll assume all the moments act around the z-axis
      C_UDMI(c, t, 3) = 0.0; // Moment about x-axis
      C_UDMI(c, t, 4) = 0.0; // Moment about y-axis
      C_UDMI(c, t, 5) = vehicle_moment_z; // Moment about z-axis
    }
    // Step 3: Calculate fluid forces and moments
    fluid_force_x = 0.0;
    fluid_force_y = 0.0;
    fluid_force_z = 0.0;
    fluid_moment_x = 0.0;
    fluid_moment_y = 0.0;
    fluid_moment_z = 0.0;
    // Loop over fluid cells
    thread_loop_c(t, d)
    {
      begin_c_loop(c, t)
      {
        real A[ND_ND], F[ND_ND], F_area[ND_ND];  // Add F_area vector to store face area
        real p = C_P(c, t); // Pressure of the cell
        C_CENTROID(A, c, t); // Centroid of the cell
        for (int i = 0; i < ND_ND; i++)
        {
          F[i] = 0.0; // Initialize force vector
        }
        // Loop over faces of the cell using C_FACE macro
        for (int f = 0; f < C_NFACES(c, t); f++)
        {
          face_t face = C_FACE(c, t, f);
          F_AREA(F_area, face, t); // Face area vector
          // Calculate face force
          for (int j = 0; j < ND_ND; j++)
          {
            F[j] += p * F_area[j]; // Use F_area instead of calling F_AREA macro multiple times
          }
        }
        // Accumulate forces and moments
        fluid_force_x += F[0];
        fluid_force_y += F[1];
        fluid_force_z += F[2];
        fluid_moment_x += (A[1] * F[2] - A[2] * F[1]);
        fluid_moment_y += (A[2] * F[0] - A[0] * F[2]);
        fluid_moment_z += (A[0] * F[1] - A[1] * F[0]);
      }
      end_c_loop(c, t);
    }
    // Step 4: Update vehicle response based on fluid forces and moments as excitation force on vehicle equations
    x_dot += fluid_force_y / m_truck * dt;
    theta_dot += fluid_moment_z / I_truck * dt;
    x += x_dot * dt;
    theta += theta_dot * dt;
    // Update simulation results in the file
    fprintf(fp, "%.4f, %.4f\n", x, theta);
   Â
    printf("Time: %.2f, x: %.4f, theta: %.4f\n", simulation_time, x, theta);
    // Increment simulation time
    simulation_time += dt;
  }
  // Close the file after simulation
  fclose(fp);
  return 0; // Successful initialization
}
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??