Fluids

Fluids

Topics related to Fluent, CFX, Turbogrid and more.

coupled simulation of vehicle and fluid tank

    • vikram chahal
      Subscriber
      i am doing coupled simulation of vehicle and fluid tank. In which i am trying to interact the vehicle bouncing and pitching motion to fluid tank and vice versa in fluent using udf. but during the interpretion of UDF i am getting parse error. can you suggest where changes are required. below is the udf:
       
       
      #include "udf.h"
      #include "thread.h" // Include the thread header file
      DEFINE_EXECUTE_AT_END(coupled_simulation)
      {
          real simulation_time = 0.0;
          real dt = 0.5; // Time step size
          real end_time = 4.0; // End time of simulation

           Define vehicle parameters
          real m_truck = 2000.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 = -10000.0; // Braking force (N)

          // Define fluid parameters
          real fluid_density = 1000.0; // Density of the fluid (kg/m^3)
          real fluid_velocity_x = 0.0; // Initial fluid velocity in x-direction (m/s)
          real fluid_velocity_y = 0.0; // Initial fluid velocity in y-direction (m/s)
          real fluid_velocity_z = 0.0; // Initial fluid velocity in z-direction (m/s)

          // Initial conditions for vehicle motion
          real x = 0.0; // Initial vertical displacement
          real theta = 0.0; // Initial angular displacement
          real x_dot = 0.0; // Initial vertical velocity
          real theta_dot = 0.0; // Initial angular velocity

          // Main simulation loop
          while (simulation_time < end_time)
          {
              // Step 1: Calculate vehicle response to braking force
              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;

              x_dot += x_ddot * dt;
              theta_dot += theta_ddot * dt;

              x += x_dot * dt;
              theta += theta_dot * dt;
              real simulation_time = CURRENT_TIME + dt;
              // Step 2: Apply vehicle response to fluid tank and compute fluid forces and moments
              real fluid_force_x = 10.0;
              real fluid_force_y = 0.0;
              real fluid_force_z = 0.0;
              real fluid_moment_x = 0.0;
              real fluid_moment_y = 5.0;
              real fluid_moment_z = 0.0;

              Thread *t;
              face_t f;
              cell_t c;

              /* Loop over all cells */
              begin_c_loop(c, t)
              {
                  /* Loop over all faces of the cell */
                  real A[ND_ND], F[ND_ND];
                  real p = C_P(c, t); // Pressure of the cell
                  C_CENTROID(A, c, t); // Centroid of the cell
                  int i;
                  for (i = 0; i < ND_ND; i++)
                  {
                      F[i] = 0.0; // Initialize force vector
                  }

                  /* Loop over all faces of the cell */
                  for (i = 0; i < THREAD_NFACES(t); i++)
                  {
                      f = C_FACE(c, t, i);
                      F_AREA(A, f, t); // Face area vector

                      /* Calculate face force */
                      int j;
                      for (j = 0; j < ND_ND; j++)
                      {
                          F[j] += p * F_AREA(A, f, t)[j]; // Pressure force on face
                      }
                  }

                  /* 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 3: Update vehicle motion based on fluid forces and moments
              Excitation force = fluid force
              x_dot += fluid_force_x / m_truck * dt;
              theta_dot += fluid_moment_y / I_truck * dt;

              x += x_dot * dt;
              theta += theta_dot * dt;
              simulation_time +=dt;
              // Step 5: Output results if needed
              // printf("Time: %.2f, x: %.4f, theta: %.4f\n", simulation_time, x, theta);

              // Step 6: Check termination condition
              if (simulation_time >= end_time)
                  break;
          }
      }
    • Rob
      Forum Moderator

      Compile the code, not all functions are available in the Interpreted mode. I won't be debugging code, but if you give some clues where the error is I can have a brief look. 

      • vikram chahal
        Subscriber
        eal fluid_force_x = 10.0;
                real fluid_force_y = 0.0;
                real fluid_force_z = 0.0;
                real fluid_moment_x = 0.0;
                real fluid_moment_y = 5.0;
                real fluid_moment_z = 0.0;

         

                Thread *t;
                face_t f;
                cell_t c;

         

                /* Loop over all cells */
                begin_c_loop(c, t)
                {
        showing error in step 2 of this code line 44,45,46,47,48,49, 51,52,53 for real fluid force and moments in x,y,z direction showing parse error and line 56 showinfg t as undeclared variable.
    • vikram chahal
      Subscriber
      real fluid_force_x = 10.0;
              real fluid_force_y = 0.0;
              real fluid_force_z = 0.0;
              real fluid_moment_x = 0.0;
              real fluid_moment_y = 5.0;
              real fluid_moment_z = 0.0;

       

              Thread *t;
              face_t f;
              cell_t c;

       

              /* Loop over all cells */
              begin_c_loop(c, t)
              {
      showing error in step 2 for real fluid force and moments in x,y,z direction showing parse error and showinfg t as undeclared variable.
    • Rob
      Forum Moderator

      OK, a parse error may be syntax related, so look in the few lines above the one reported in the error. But, if t (thread in your case?) is showing an error, where is that defined? DEFINE_EXECUTE_AT_END doesn't pass the thread so you need to tell the solver what it is. 

    • 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??
    • Rob
      Forum Moderator

      How are you adding the force to the fluid cells? 

    • vikram chahal
      Subscriber

      IN THIS WAY , PLEASE SUGGEST IF ANY CHANGES ARE REQUIRED

        // 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
              }
    • Rob
      Forum Moderator

      That adds the values to a UDM, it doesn't do anything to the solution. 

      • vikram chahal
        Subscriber

        Thanks a lot Rob...

        could you please tell how can i apply moment to a tank about z-axis that pass throug CG in ansys fluent.

    • Rob
      Forum Moderator

      That'll be DEFINE_SOURCE assuming you don't want to actually move the body around. Note, if the whole domain moves you don't need to remesh. 

      • vikram chahal
        Subscriber

        could you please provide the UDF code. i have write this one 

        #include "udf.h"

        DEFINE_SOURCE(moment_source, c, t, dS, eqn)
        {
            real time = CURRENT_TIME; // Get the current simulation time
            real moment = 0.0; // Initialize moment to zero
            real CG[ND_ND]; // Center of gravity coordinates
            real cellCG[ND_ND]; // Cell center coordinates

            C_CENTROID(CG, c, t); // Get CG of the domain
            C_CENTROID(cellCG, c, t); // Get cell center coordinates

            real distance_from_CG = cellCG[2] - CG[2]; // Distance along z-axis

            /* Check if time is within the specified duration */
            if (time <= 2.0)
            {
                /* Apply moment only to cells at CG */
                if (distance_from_CG = 0.0)
                {
                    moment = 5000.0; // Apply 5000 Nm moment
            }

            dS[eqn] = 0.0; // Reset the source term
            return moment; // Return the moment as the source term
        }

        and hooked as the souce term of fluid domain under cell conditions.

    • vikram chahal
      Subscriber

       

       

       

    • vikram chahal
      Subscriber

      where should i hook this UDF?

       

    • Rob
      Forum Moderator

      Source terms are hooked into the cell zones. 

      • vikram chahal
        Subscriber
        #include "udf.h"

        DEFINE_SOURCE(moment_source, c, t, dS, eqn)
        {
            real time = CURRENT_TIME; // Get the current simulation time
            real moment = 0.0; // Initialize moment to zero
            real CG[ND_ND]; // Center of gravity coordinates
            real cellCG[ND_ND]; // Cell center coordinates

            C_CENTROID(CG, c, t); // Get CG of the domain
            C_CENTROID(cellCG, c, t); // Get cell center coordinates

            real distance_from_CG = cellCG[2] - CG[2]; // Distance along z-axis

            /* Check if time is within the specified duration */
            if (time <= 2.0)
            {
                /* Apply moment only to cells at CG */
                if (distance_from_CG = 0.0)
                {
                    moment = 5000.0; // Apply 5000 Nm moment
            }

            dS[eqn] = 0.0; // Reset the source term
            return moment; // Return the moment as the source term
        }

        and hooked as the souce term of fluid domain under cell conditions.

        this UDF is giving very less value of force... could  you please check it whether syntex and logic is correct or not.

    • Rob
      Forum Moderator

      You're returning a source at the centre position by the looks of it? I'm not able to check code, only point you at the manual. 

Viewing 12 reply threads
  • The topic ‘coupled simulation of vehicle and fluid tank’ is closed to new replies.