Arrays and sets

From ASCEND
Jump to: navigation, search
This article is incomplete or needs expanding. Please help out by adding your comments.

This page will be expanded to include use of sets and arrays in ASCEND. For the moment here are some quick examples:

REQUIRE "atoms.a4l";

MODEL mymodel;
   (* declare a new set -- but note that its members could be declared in a sub-model if desired *)
   myset IS_A set OF symbol_constant;

   (* declare the members of the set -- note, this is done in the declarative section because the set is constant-valued *)
   myset :== ['A','B','D'];

   (* declare an array that contains elements indexed by the members of the myset *)

   myarray[myset] IS_A distance;

   (* write a relationship between the values of this array *)
   myarray['A'] = myarray['B'] + 1;

METHODS
METHOD on_load;
   (* fix all members in myarray *)
   FIX myarray[myset];

   (* free just one member *)
   FREE myarray['A'];
END on_load;
END mymodel;

See also Thin-walled tank/Arrays.

FOR loops

ASCEND provides looping syntax. The syntax used depends on whether you are in the declarative (main) part of your model (FOR..CREATE), or in the procedural part of the model (FOR..DO).

See the FOR for more information.

Counting elements in sets using CARD

In ASCEND, the function CARD[set] can be used to count the number of elements in a set. A typical use is as follows:

A IS_A set OF symbol_constant;

A :== ['A','B','C','D'];
n_A IS_A integer_constant;

n_A :== CARD[A];

Now the value of n_A will be set to 4.

Note: you can't use the CARD function in equations; it is only able to be used when assigning values to constants.

Adding and subtracting sets

Sets can be useful for slicing up arrays in different ways, for example:

REQUIRE "atoms.a4l";
MODEL mymodel;
  A, B, C IS_A set OF integer_constant;

  A :== [1..10];
  B :== [1,2,9,10];

  C :== A - B; (* set difference, will contain 3,4,..8. *)

  a[A] IS_A temperature;

  n_C IS_A integer_constant;
  n_C :== CARD[C];

  T_avg_C IS_A temperature;
  T_avg_C * n_C = SUM[a[i] | i IN C];

METHODS
METHOD on_load;
  (* fix elements 1, 2, 9 and 10 *)
  FIX a[B];

  (* initialise those same elements to 290 K *)
  FOR i IN B DO
    a[i] := 290 {K};

  END FOR;
  (* initialise the remaining elements to 320 K *)
  a[C] := 320 {K};

END on_load;
END mymodel;
Note the use of the SUM statement for looping through elements of a set and adding up the resulting expressions. Inside the sum is the '|' or SUCH_THAT operator ('|' is simply shorthand for the longhand version, 'SUCH_THAT'.).