Use of ChatGPT 4 for coding new YADE DEM constitutive laws

Asked by Idan

Hello to everyone,

I thought about trying ChatGPT 4 for writing new material laws and physics into YADE DEM,

as I have little experience with the YADE code, it would be helpful if someone can give me a feedback.

Here is a code for temperature dependent cohesive burgers model written by ChatGPT 4:

HEADER:

#pragma once
#include<yade/pkg/dem/CohesiveFrictionalContactLaw.hpp>
#include<yade/pkg/dem/ScGeom.hpp>
#include<yade/pkg/dem/FrictPhys.hpp>

class TempDependentCohesiveBurgersViscoElasticModel: public CohFrictMat {
public:
    TempDependentCohesiveBurgersViscoElasticModel(Real young, Real poisson, Real frictionAngle, Real density, Real cohesion, Real viscousDamping, Real viscosity, Real tempRef);
    virtual ~TempDependentCohesiveBurgersViscoElasticModel();

    // Temperature reference for the model
    Real tempRef;

    // Viscosity for the viscoelastic component of the model
    Real viscosity;

    YADE_CLASS_BASE_DOC_ATTRS_CTOR(TempDependentCohesiveBurgersViscoElasticModel, CohFrictMat, "Temperature-dependent cohesive Burger's viscoelastic model inheriting from CohFrictMat",
        ((Real, viscousDamping, 0, ,"Viscous damping for the viscoelastic component of the model"))
        ((Real, tempRef, 298.15, ,"Reference temperature for the model"))
        ((Real, viscosity, 0, ,"Viscosity for the viscoelastic component of the model")),
        /* ctor */ TempDependentCohesiveBurgersViscoElasticModel(young, poisson, frictionAngle, density, cohesion, viscousDamping, viscosity, tempRef); );
};
REGISTER_SERIALIZABLE(TempDependentCohesiveBurgersViscoElasticModel);

class TempDependentCohesiveBurgersViscoElasticContactLaw: public Law2_ScGeom_FrictPhys_CundallStrack {
public:
    virtual void go(shared_ptr<IGeom>& ig, shared_ptr<IPhys>& ip, Interaction* contact);
    YADE_CLASS_BASE_DOC(TempDependentCohesiveBurgersViscoElasticContactLaw, Law2_ScGeom_FrictPhys_CundallStrack, "Contact law for the temperature-dependent cohesive Burger's viscoelastic model");
};
REGISTER_SERIALIZABLE(TempDependentCohesiveBurgersViscoElasticContactLaw);

CCP File:

#include "TempDependentCohesiveBurgersViscoElastic.hpp"

TempDependentCohesiveBurgersViscoElasticModel::TempDependentCohesiveBurgersViscoElasticModel(Real young, Real poisson, Real frictionAngle, Real density, Real cohesion, Real viscousDamping, Real viscosity, Real tempRef)
    : CohFrictMat(young, poisson, frictionAngle, density, cohesion)
    , viscousDamping(viscousDamping)
    , viscosity(viscosity)
    , tempRef(tempRef) {
}

TempDependentCohesiveBurgersViscoElasticModel::~TempDependentCohesiveBurgersViscoElasticModel() {
}

YADE_PLUGIN((TempDependentCohesiveBurgersViscoElasticModel));

void TempDependentCohesiveBurgersViscoElasticContactLaw::go(shared_ptr<IGeom>& ig, shared_ptr<IPhys>& ip, Interaction* contact) {
    // Cast the geometry and physical parameters to the correct types
    ScGeom* geom = static_cast<ScGeom*>(ig.get());
    FrictPhys* phys = static_cast<FrictPhys*>(ip.get());

    // Get the associated bodies and materials
    Body* b1 = Body::byId(contact->getId1(), scene);
    Body* b2 = Body::byId(contact->getId2(), scene);
    TempDependentCohesiveBurgersViscoElasticModel* mat1 = static_cast<TempDependentCohesiveBurgersViscoElasticModel*>(b1->material.get());
    TempDependentCohesiveBurgersViscoElasticModel* mat2 = static_cast<TempDependentCohesiveBurgersViscoElasticModel*>(b2->material.get());

    // Compute the normal and tangential forces based on the temperature-dependent cohesive Burger's viscoelastic model
    // Note: You will need to implement the specific calculations for the model
    Vector3r normalForce, tangentialForce;
    // ...

    // Apply the forces to the bodies
    scene->forces.addForce(contact->getId1(), normalForce + tangentialForce);
    scene->forces.addForce(contact->getId2(), -(normalForce + tangentialForce));

    // Update the interaction geometry
    // ...
}

YADE_PLUGIN((TempDependentCohesiveBurgersViscoElasticContactLaw));

It is not fully completed but does this look fine? what is the best IDE to be used debugging such model? thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Idan
Solved:
Last query:
Last reply:
Revision history for this message
Jan Stránský (honzik) said :
#1

Hello,

for the first sight, it looks good :-)
I will have closer look later, as I am interested in GPT capabilities.

> // Note: You will need to implement the specific calculations for the model
> // ...

this is actual GPT output? In fact, it is the most important and challenging part of the model implementation..

Cheers
Jan

Revision history for this message
Idan (idanko3089) said :
#2

Hi,

I asked it to continue implementing the model with exponential temperature dependence (it was lacking the nature of temperature dependence as it does not exist in the original burgers formulation).

Here is the result:

Header:

#pragma once
#include<yade/pkg/dem/CohesiveFrictionalContactLaw.hpp>
#include<yade/pkg/dem/ScGeom.hpp>
#include<yade/pkg/dem/FrictPhys.hpp>
#include<yade/pkg/dem/ThermalState.hpp>

class TempDependentBurgersViscoElasticModel: public CohFrictMat {
public:
    TempDependentBurgersViscoElasticModel(Real young, Real poisson, Real frictionAngle, Real density, Real cohesion, Real viscousDamping, Real viscosity, Real tempRef, Real tempExpFactor);
    virtual ~TempDependentBurgersViscoElasticModel();

    // Temperature reference for the model
    Real tempRef;

    // Temperature exponential factor
    Real tempExpFactor;

    // Viscosity for the viscoelastic component of the model
    Real viscosity;

    YADE_CLASS_BASE_DOC_ATTRS_CTOR(TempDependentBurgersViscoElasticModel, CohFrictMat, "Temperature-dependent Burger's viscoelastic model inheriting from CohFrictMat",
        ((Real, viscousDamping, 0, ,"Viscous damping for the viscoelastic component of the model"))
        ((Real, tempRef, 298.15, ,"Reference temperature for the model"))
        ((Real, viscosity, 0, ,"Viscosity for the viscoelastic component of the model"))
        ((Real, tempExpFactor, 0, ,"Temperature exponential factor for the temperature-dependent tangential force")),
        /* ctor */ TempDependentBurgersViscoElasticModel(young, poisson, frictionAngle, density, cohesion, viscousDamping, viscosity, tempRef, tempExpFactor); );
};
REGISTER_SERIALIZABLE(TempDependentBurgersViscoElasticModel);

class TempDependentBurgersViscoElasticContactLaw: public Law2_ScGeom_FrictPhys_CundallStrack {
public:
    virtual void go(shared_ptr<IGeom>& ig, shared_ptr<IPhys>& ip, Interaction* contact);
    YADE_CLASS_BASE_DOC(TempDependentBurgersViscoElasticContactLaw, Law2_ScGeom_FrictPhys_CundallStrack, "Contact law for the temperature-dependent Burger's viscoelastic model");
};
REGISTER_SERIALIZABLE(TempDependentBurgersViscoElasticContactLaw);

CCP file:

#include "TempDependentBurgersViscoElastic.hpp"

TempDependentBurgersViscoElasticModel::TempDependentBurgersViscoElasticModel(Real young, Real poisson, Real frictionAngle, Real density, Real cohesion, Real viscousDamping, Real viscosity, Real tempRef, Real tempExpFactor)
    : CohFrictMat(young, poisson, frictionAngle, density, cohesion)
    , viscousDamping(viscousDamping)
    , viscosity(viscosity)
    , tempRef(tempRef)
    , tempExpFactor(tempExpFactor) {
}

TempDependentBurgersViscoElasticModel::~TempDependentBurgersViscoElasticModel() {
}

YADE_PLUGIN((TempDependentBurgersViscoElasticModel));

void TempDependentBurgersViscoElasticContactLaw::go(shared_ptr<IGeom>& ig, shared_ptr<IPhys>& ip, Interaction* contact) {
// Cast the geometry and physical parameters to the correct types
ScGeom* geom = static_cast<ScGeom*>(ig.get());
FrictPhys* phys = static_cast<FrictPhys*>(ip.get());

// Get the associated bodies and materials
Body* b1 = Body::byId(contact->getId1(), scene);
Body* b2 = Body::byId(contact->getId2(), scene);
TempDependentBurgersViscoElasticModel* mat1 = static_cast<TempDependentBurgersViscoElasticModel*>(b1->material.get());
TempDependentBurgersViscoElasticModel* mat2 = static_cast<TempDependentBurgersViscoElasticModel*>(b2->material.get());

// Compute the normal force based on Hooke's law
Real kn = 2 * phys->kn * geom->radius / (geom->radius + geom->radius);
Real Fn = kn * geom->penetrationDepth;

// Compute the tangential force based on a simplified version of the Burger's model
Real kt = 2 * phys->kt * geom->radius / (geom->radius + geom->radius);
Real viscosity = 0.5 * (mat1->viscosity + mat2->viscosity);
Real viscousDamping = 0.5 * (mat1->viscousDamping + mat2->viscousDamping);

Vector3r relVel = b1->state->vel - b2->state->vel;
Vector3r relAngVel = b1->state->angVel - b2->state->angVel;

Vector3r relTangentialVel = relVel - geom->normal.dot(relVel) * geom->normal;
Vector3r relTangentialAngVel = geom->radius * 0.5 * (relAngVel.cross(geom->normal));

Vector3r relTangentialTotalVel = relTangentialVel + relTangentialAngVel;
Vector3r tangentialForce = -(kt * geom->uT + viscosity * relTangentialTotalVel);

// Get the temperatures from the thermal state
shared_ptr<ThermalState> ts1 = YADE_PTR_CAST<ThermalState>(b1->state);
shared_ptr<ThermalState> ts2 = YADE_PTR_CAST<ThermalState>(b2->state);
Real avgTemp = 0.5 * (ts1->temp + ts2->temp);

// Apply temperature-dependent exponential factor
Real tempExpFactor = 0.5 * (mat1->tempExpFactor + mat2->tempExpFactor);
Real tempRef = 0.5 * (mat1->tempRef + mat2->tempRef);
Real tempFactor = std::exp(tempExpFactor * (avgTemp - tempRef));
tangentialForce *= tempFactor;

// Apply Coulomb friction law
Real maxTangentialForce = phys->tangensOfFrictionAngle * Fn;
if (tangentialForce.norm() > maxTangentialForce) {
    tangentialForce = tangentialForce.normalized() * maxTangentialForce;
}

Vector3r normalForce = geom->normal * Fn;

// Apply the forces to the bodies
scene->forces.addForce(contact->getId1(), normalForce + tangentialForce);
scene->forces.addForce(contact->getId2(), -(normalForce + tangentialForce));
scene->forces.addTorque(contact->getId1(), geom->radius * 0.5 * tangentialForce.cross(geom->normal));
scene->forces.addTorque(contact->getId2(), -geom->radius * 0.5 * tangentialForce.cross(geom->normal));

geom->uT = geom->uT - geom->rotate(tangentialForce / kt);

}

YADE_PLUGIN((TempDependentBurgersViscoElasticContactLaw));

Hope it is more helpful now.

Revision history for this message
Xue (1q12) said :
#3

Hello,

If you don't mind, may I ask for your guidance on how you would instruct GPT-4 to write the script mentioned above? Specifically, I am interested in how you would enable GPT-4 to access the variable names and locate the required Hpp files in the given code. I would greatly appreciate your response.
Thank you very much!

xue

Revision history for this message
Idan (idanko3089) said :
#5

Sure,

here is in general the prompt i used:

"can you write me c++ code (header and ccp files) for new temperature dependent cohesive burgers visco elastic model for Yade Dem ?, inherit from cohfrictmat and ThermalState. implement exponential temperature dependence"

Revision history for this message
Xue (1q12) said :
#6

Thank you very much for your response!
xue

Revision history for this message
Jan Stránský (honzik) said :
#7

Hello,

I have tried the result.

TLTR: not so good actually..

As is my so fart experience with ChatGPT, all that glitters is not gold.
For the first sight, it looks relatively OK.
For more closer look, there are maaany problems to make it even compile (I gave up after not so short time)
- wrong includes,
- non-existent includes
- using non-existent variables
- redeclaration of variables and functions
- ...

Concerning actual implementation (TempDependentBurgersViscoElasticContactLaw::go), you have to check yourself if it computes it correctly.
Specifically the tangentialForce value.

Maybe just asking it for the implementation of Law2::go functions would have better results, but currently it produces non-compilable code (therefore the actual value is questionable).

Cheers
Jan

Revision history for this message
Idan (idanko3089) said :
#8

thanks! i will try to prompt it with the source code and see if there are better results.