5. Using Python classes for verification#

5.1. Naming#

First of all, it is appropriate to name the object JEVEUXqui that serves as the basis for the SD. Remember that each JEVEUXest object is assigned a name, which is a string of characters of length 24 (CHARACTER 24). It should also be noted that the user names his concepts in the command file via a character string of length 8 (CHARACTER*8). One of the basic principles for building global SDs in Code_Aster is to **always* prefix the data name JEVEUXrelatifs to the concept (produced by the command) with the name of the latter. For example, the mesh contains a real vector with the coordinates of the nodes, it is created like this:

COOVAL = MAIL (1:8) //'. COORDO. VALE '

CALL WKVECT (COOVAL, 'G V R',3* NBNO, JCOOR)

Here MAIL is the name of the concept given by the user (MAIL = LIRE_MAILLAGE (…)). When we are in the Python class that will be instantiated for the concept that we need to check, the first thing to do is to determine the name of all the objects that will belong to the SD:

class sd_mesh (sd_title):

name = SDNom (end=8)

This will name the prefix of all JEVEUXcontenus objects in the SD. It is constructed by taking the first 8 characters of the SD (end=8). Next, it is a question of checking the presence of objects in the SD. For example, the sd_mesh must contain an object DIME, which is a vector of integers of length 6:

class sd_mesh (sd_title):

name = SDNom (end=8)

toto = ASVi (SDNom (name='). DIME '), lonmax=6,)

The toto object is an integer vector (ASVi) with an attribute LONMAXvaut 6. We could also have written in a more compact way:

class sd_mesh (sd_title):

name = SDNom (end=8)

DIME = ASVi (lonmax=6),

This last construction (DIME = ASVi (lonmax=6,)) is a facility offered to the developer, based on the fact that the name of a JEVEUXest object always builds the same way. Implicitly, when you write DIME = asVi (), you build an instance with name DIMEde the class As VIdont the object JEVEUXa for name namj (1:8) /”. DIME “. This instantiation should be preferred, in order to facilitate the reading of the catalog. Sometimes the attributes of an object (such as its type) can be variable depending on the operator creating this SD. In this case, you can use the member function Among (). For example:

VALE = asVect (ltyp=Among (4,8,16,24), type=Among ('C', 'I', 'K', 'R'),

docu=Among ( », “2”, “3”),)

The. VALEd “A field at the nodes can contain complex values, integers, real values, or even strings of characters of length 8, 16, or 24.

5.2. Optional objects#

We can declare that an object is optional in the SD, for example, there may not be a GROUP_NOdans the SD mesh:

class sd_mesh (sd_title):

name = SDNom (end=8)

GROUPENO = Optional (asColl (access='NO', storage=' DISPERSE ',

modelong=” VARIABLE “, type=”I”,))

The Optional function sets the optional attribute to True. By default, all objects are mandatory. As the SD verification mechanism (SDVERI) covers all objects JEVEUXattachés in concept, it is imperative that all objects (mandatory or optional) have been declared in the SD class!

5.3. Existence method « exists »#

Sometimes it’s useful to know if an SD exists. To do this, you can overload the exists method which returns a boolean:

def exists (self):

# returns « true » if the SD seems to exist (and therefore can be # verified) Return self. REFE .exists

To verify the existence, the easiest way is to check the presence of a mandatory object (here REFE).

It’s important to note that in the AsBase class (from which all the others derive), exist is an attribute (taking a logical True or False value). It is built using the Fortran JEEXIN routine (OBJET, IRET) at the lowest level.

When exist is overloaded as above, it becomes a method. Therefore it is imperative to call it as such, that is to say without forgetting the opening and closing brackets « () ».

For example, the sd_ligrel redefines the exists method, so we should call it this way:

if self.contact_resolved ():

# don’t forget the () because sd_ligrel.exists is a method

Self assert. LIGRE .exists ()

5.4. « check_ » verification methods#

All classes derived from ASBase contain the check method. By default, this function only checks the compliance of the SD with the attributes of the object JEVEUX. For example:

class sd_mesh (sd_title):

name = SDNom (end=8)

DIME = ASVi (lonmax=6),

We just check that the object JEVEUXnomj (1:8) //”. DIME It is indeed a vector of integers of length 8. However, it is possible (and desirable!) to override a check method to do more advanced checks. For example, still in the sd_mesh, we would like to check that the pointer with the name MAIL (1:8) //”. NOMNOE “, which contains the names of the nodes, has a length equal to the number of nodes:

class sd_mesh (sd_title):

name = SDNom (end=8)

DIME = ASVi (lonmax=6),

NOMNOE = ASPn (ltyp=8)
def check_ NOEUDS (self, checker):

Dime = self. DIME .get ()

nb_no = dime [0]

Self assert. NOMNOE .nomuti = nb_no

It has been declared that the object NOMNOEétait has a name pointer (contained in strings of length ltyp=8). Then we declare a new method check_ NOEUDS, one of whose arguments must be checker (this base class for checks contains in particular a mechanism to control the depth of the checks and avoid checking the same objects several times). All member functions that start with check_ will be executed when instantiating the SD class that is being checked. It should be noted that both obligations:

  • the method must start with check_

  • the method must have an object checkeren argument

The checker class contains a dictionary of all verified JEVEUXdéjà objects, to do so, you just need to use the names member data:

if checker.names.has_key (name): return

This results in: if the object JEVEUXde name names has already been checked, then return.

5.5. Access to object content JEVEUX « get () »#

With the previous example, we introduced another control mechanism, it is the line dime = self. DIME .get () .It is in fact possible to access the content of Fortran objects in order to retrieve information. To do this, the supervisor uses the two methods of the aster module: getvectjev and getcolljev.

It goes without saying that it is entirely possible to define attributes and methods specific to the SD that are described. For example, in the sd_mesh, there is a member function u_size giving generic information:

class sd_mesh (sd_title):

name = SDNom (end=8)

DIME = ASVi (lonmax=6),

def u_dime (self):

dime=self. DIME .get ()

nb_no =Dime [0]

nb_nl =Dime [1]

nb_ma =Dime [2]

nb_sm =Dime [3]

nb_sm-mx=Dime [4]

dim_coor =dime [5]

return nb_no, nb_nl, nb_ma, nb_ma, nb_sm, nb_sm-mx, dim_coor

Note: if the object is a simple object, get () returns a Python list, if the object is a collection, get () returns a Python dictionary.

5.6. Inheritance and typing#

All of the classes that describe SDs can be used in other classes. For example:

class sd_mesh (sd_title):

name = SDNom (end=8)

COORDO = sd_cham_no ()

In this example, we see a double mechanism. The first is classical inheritance: the sd_mesh derives from the sd_title whose description is:

class sd_title (AsBase):

TITR = As VK80 (SDNom (start=19), optional=True)

The sd_title only contains a K80 vector stored in the object JEVEUXdont the name starts at the 19th character. This object is optional.

The second mechanism uses the concept of data typing specific to an object language like Python. Indeed, the object nomj (1:8) //”. COORDO It’s an SD of the type fiel_no:

class sd_cham_no (sd_title):

name = SDNom (end=19)

VALE = asVect (ltyp=Among (4,8,16,24), type=Among ('C', 'I', 'K', 'R'), docu=Among (", '2', '3'),)

REFE = Ace VK24 ()

DESC = Savi (docu=' CHNO '),

Be careful with circular references (the mesh SD contains a cham_no object that contains a mesh object). It is up to the developer to take care of this (see for example sd_cham_no).

5.7. Utilities#

A number of verification operations are available in the sd_util module:

  • sdu_assert (ojb, checker, bool, comment=): check that the boolean (bool) is true;

  • sdu_compare (ojb, checker, val1, val1, comp, val2,): check that the comparison relationship between val1 and val2 is respected with comp= “==”/”! =”/”>=”/”>” > “/”/”<=”/”<”;

  • sdu_tous_different (ojb, checker, sequence=None,): checks that the elements of the sequence are all different;

  • sdu_all_non_blanks (ojb, checker, sequence=None,): checks that the elements (strings) of the sequence are all « non-white »;

  • sdu_all_included (ojb, checker, sequence=None, Vmin=none, Vmax=none,): checks that all values in the sequence are between vmin and vmax;

  • sdu_monotone (seqini): checks that a sequence is sorted in ascending (or descending) order;

  • sdu_nom_gd (numgd): returns the name of the number quantity (numgd);

  • sdu_licmp_gd (numgd): returns the list of cmps for the number quantity (numgd);

  • sdu_nb_ec (numgd): returns the number of integers coded to describe the components of the quantity (numgd);

  • sdu_ensemble (lojb): check that JEVEUXde lojb objects exist simultaneously.