CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   STAR-CCM+ (http://www.cfd-online.com/Forums/star-ccm/)
-   -   Java with CCM+ (http://www.cfd-online.com/Forums/star-ccm/124289-java-ccm.html)

crevoise October 1, 2013 10:12

Java with CCM+
 
Hello,

I am trying to write some Java script for some automation of tasks.
I am totally new in this, so I guess the question will sound trivial to some.

I want ,for example, to erase all the chemical species which are loaded in my continuum.

To erase a particular one, I have the following script:
Simulation simulation_0 = getActiveSimulation();
PhysicsContinuum physicsContinuum_0 = ((PhysicsContinuum)simulation_0.getContinuumManage r().getContinuum("continuum"));

MultiComponentGasModel multiComponentGasModel_0 =
physicsContinuum_0.getModelManager().getModel(Mult iComponentGasModel.class);

GasMixture gasMixture_0 =
((GasMixture) multiComponentGasModel_0.getMixture());

GasComponent gasComponent_0 =
((GasComponent) gasMixture_0.getComponents().getComponent("Chemica l_1"));

gasMixture_0.getComponents().deleteMixtureComponen ts(new NeoObjectVector(new Object[] {gasComponent_0}));

So that it will erase the gasComponent_0 defined by chemical_1


How can I simply select all the gasComponent and put them in the new object to then erase them?

Moreover, without knowing the name of the species (chemical_1), how can I selection the gasComponent on a specific position on the array?
I tried with something like
((GasComponent) gasMixture_0.getComponents().getComponent([1]));
but without success.
If I can do that, I then can loop and erase one by on all the species.


Thank you for your help

rwryne October 1, 2013 12:56

edit: see comment below, much better solution

olesen October 2, 2013 02:32

Quote:

Originally Posted by crevoise (Post 454479)
Hello,

I am trying to write some Java script for some automation of tasks.
I am totally new in this, so I guess the question will sound trivial to some.

I want ,for example, to erase all the chemical species which are loaded in my continuum.

To erase a particular one, I have the following script:
Simulation simulation_0 = getActiveSimulation();
PhysicsContinuum physicsContinuum_0 = ((PhysicsContinuum)simulation_0.getContinuumManage r().getContinuum("continuum"));

MultiComponentGasModel multiComponentGasModel_0 =
physicsContinuum_0.getModelManager().getModel(Mult iComponentGasModel.class);

GasMixture gasMixture_0 =
((GasMixture) multiComponentGasModel_0.getMixture());

GasComponent gasComponent_0 =
((GasComponent) gasMixture_0.getComponents().getComponent("Chemica l_1"));

gasMixture_0.getComponents().deleteMixtureComponen ts(new NeoObjectVector(new Object[] {gasComponent_0}));

So that it will erase the gasComponent_0 defined by chemical_1


How can I simply select all the gasComponent and put them in the new object to then erase them?

Moreover, without knowing the name of the species (chemical_1), how can I selection the gasComponent on a specific position on the array?
I tried with something like
((GasComponent) gasMixture_0.getComponents().getComponent([1]));
but without success.
If I can do that, I then can loop and erase one by on all the species.


Thank you for your help


This is untested, but should give you ideas about another approach. You may wish to add try/catch in various places to improve the robustness.
Code:


import star.common.PhysicsContinuum;
import star.common.Simulation;
import star.common.StarMacro;
import star.material.MixtureComponentManager;
import star.material.MultiComponentGasModel;
 
/**
 * Erase all species attached to given physics continuum
 */
public class EraseSpecies extends StarMacro
{
    private Simulation sim_ = null;
    private static final String PHYSICS_NAME = "continuum";
 
    /** The entry point for a StarMacro. */
    @Override
    public void execute()
    {
        sim_ = this.getActiveSimulation();
        PhysicsContinuum physics =
            (PhysicsContinuum) sim_.getContinuumManager().getContinuum(PHYSICS_NAME);
        MixtureComponentManager gasMixMgr = physics.getModelManager()
            .getModel(MultiComponentGasModel.class).getMixture().getComponents();
        gasMixMgr.deleteChildren(gasMixMgr.getComponents());
    }
}


crevoise October 2, 2013 06:19

Hello Rwryne and Olessen
Thanks a lot for your answers

I have tested the Olessen proposal, and it works perfectly.

I have some questions about it, in order to fully understand it myself:
1/ You are calling the class 'MixtureComponentManager'
How do you know the existence of this class? Is there somewhere a list of all the available classes available in CCM+?

2/ Before starting the StarMacro, you are doing this initialization:
private Simulation sim_ = null;
private static final String PHYSICS_NAME = "continuum";

****************************
If I understand well, you are
a/ clearing sim_ : Is it important to all time do so before starting a java script?
b/ declaring the string varaible Physic_name defined by the continuum.
If there are several continuum defined, how does this will work?
****************************

/** The entry point for a StarMacro. */
@Override
public void execute() etc....

*******************
c/ What is the use of @Override ?


Thanks a lot for your explanations and your help

olesen October 4, 2013 02:27

Quote:

Originally Posted by crevoise (Post 454633)
You are calling the class 'MixtureComponentManager'
How do you know the existence of this class? Is there somewhere a list of all the available classes available in CCM+?

The STARCCM installation contains the javadocs for its API. The descriptions are not exactly complete, but should provide you with enough information. The subdirectory: star/lib/doc/client/html/

Quote:

a/ clearing sim_ : Is it important to all time do so before starting a java script?
This is not needed, but I think it is good style.

Quote:

b/ declaring the string varaible Physic_name defined by the continuum.
If there are several continuum defined, how does this will work?
This only works for the case that you posed. If the continuum is named differently, or if there are more of them that you want to clear, you'll have a problem. If you want to loop over all possible physics continua, you'd need something like this:

Code:

ContinuumManager contMgr = sim_.getContinuumManager();
 
for (PhysicsContinuum cont: contMgr.getObjectsOf(PhysicsContinuum.class))
{
    ModelManager mdlMgr = cont.getModelManager()
 
    try
    {
        MixtureComponentManager gasMixMgr = mdlMgr
            .getModel(MultiComponentGasModel.class).getMixture().getComponents();
        gasMixMgr.deleteChildren(gasMixMgr.getComponents());
    }
    catch (Exception ex)
    {}
}

Note that we stuff the interesting bits inside of a try/catch grouping. This makes it failsafe for continua that are not actually gas mixtures.


Quote:

@Override
public void execute() etc....

*******************
c/ What is the use of @Override ?
This somewhat pedantic. It works equally well without this annotation. However, the StarMacro already provides an empty 'execute' method that we are overriding and the @Override annotation documuments this.

crevoise October 4, 2013 10:44

Thank you Olesen for your valuable answers and info about javadocs, this is really helpful.

It allowed me to go on with some script, but am facing one more problem:
I am now working on passive scalar, where I wish to collect all of them.
From what I've read from the javadocs (part PassiveScalarManager), and from the previous experience from the species collection, I wrote the following:

Code:

import passivescalar.*;
..

PassiveScalarMaterial passiv=
physics.getModelManager().getModel(PassiveScalarModel.class)
.getPassiveScalarManager().getPassiveScalarMaterials();

Which returns:
incompatible type:
found: java.util.Collection<star.passivescalar.PassiveSca larMaterial>
required: star.passivesscalar.PassiveScalarMaterial

Yet, when I am applying the above script in order to collect a specific passive scalar, this will work:

Code:

..

PassiveScalarMaterial passiv=
physics.getModelManager().getModel(PassiveScalarModel.class)
.getPassiveScalarManager().getPassiveScalarMaterial("PassSc1");

In the javadoc, it is yet specified that the collection .getPassiveScalarMaterials is available.

What am I doing wrong?
Thanks again for your help

olesen October 4, 2013 10:57

Quote:

Originally Posted by crevoise (Post 455060)
I wrote the following:
Code:

import passivescalar.*;
..
PassiveScalarMaterial passiv=
physics.getModelManager().getModel(PassiveScalarModel.class)
.getPassiveScalarManager().getPassiveScalarMaterials();

Which returns:
incompatible type:
found: java.util.Collection<star.passivescalar.PassiveSca larMaterial>
required: star.passivesscalar.PassiveScalarMaterial

Like the error message states, you are trying to assign things incorrectly. It's not much different than trying to assign a String to a double, for example.

If you try with the proper assignment, I'm convinced you can resolve the error:
Code:


Collection<PassiveScalarMaterial> matlLst = physics.getModelManager().getModel(PassiveScalarModel.class)
.getPassiveScalarManager().getPassiveScalarMaterials();

You can then go on and do other useful things with this collection of materials. You can, of course, just get a single material by name too. That is what the "getPassiveScalarMaterial(String name)" method seems to provide.

crevoise October 10, 2013 08:42

Hello Olesen,

A small reply to thanks you for all the hints and help your provided me, it was really useful to start scripting. It's going quite well so far.
Thanks again

cfdguy October 12, 2013 11:24

Dear all,

Have you visited MacroHut already?


All times are GMT -4. The time now is 07:03.