Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented dimension checking of full models in PyGTK GUI #1

Merged
merged 9 commits into from
Dec 17, 2023
Merged
5 changes: 2 additions & 3 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -2243,13 +2243,12 @@ if conf.env['WITH_TCLTK']:
if conf.CheckTcl():
if conf.CheckTclVersion():
if conf.CheckTk():
if with_tcltk and conf.CheckTkVersion():
if conf.CheckTkVersion():
if conf.env['STATIC_TCLTK']:
if conf.CheckTkTable():
pass
else:
without_tcltk_reason = "TkTable not found"
with_tcltk = False
conf.env.set_optional('tcltk',active=False,reason="TkTable not found")
else:
conf.env.set_optional('tcltk',active=False,reason="Require Tk version <= 8.4. See 'scons -h'")
else:
Expand Down
11 changes: 10 additions & 1 deletion a4.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def shebang(script,args):
return [script]+args

if __name__ == "__main__":

p = argparse.ArgumentParser(description='ASCEND launcher')

p.add_argument('--debug',action='store_true',help='Show launcher debug output')
g = p.add_mutually_exclusive_group()
g.add_argument('--gdb',action='store_true',help='Run action via GDB')
Expand All @@ -109,6 +109,15 @@ if __name__ == "__main__":
p_gui.set_defaults(action='gui')

p_run = s.add_parser('run',help = 'Run model and solve (without GUI, via QRSlv solver)')

if sys.version_info.major == 3 and sys.version_info.minor < 8:
class ExtendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest) or []
items.extend(values)
setattr(namespace, self.dest, items)
p_run.register('action', 'extend', ExtendAction)

p_run.set_defaults(action='run')
p_run.add_argument('file',type=pathlib.Path,help='ASCEND model file to be opened')
p_run.add_argument('--model','-m', help="Name of MODEL to instantiate (defaults to filename without extension)");
Expand Down
125 changes: 100 additions & 25 deletions ascend/compiler/dimen_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,154 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dimen_io.h"

#include <ascend/general/platform.h>
#include <ascend/general/ascMalloc.h>
#include <ascend/general/dstring.h>
#include <ascend/utilities/error.h>

#include "dimen_io.h"

static
void WriteFrac(FILE *f, struct fraction frac, CONST char *str, int *CONST p)
{
if (Numerator(frac)){
if (*p) PUTC('*',f);
static void WriteFrac(FILE *f, struct fraction frac, CONST char *str, int *CONST p){
if(Numerator(frac)){

Check warning on line 36 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L36

Added line #L36 was not covered by tests
if(*p) PUTC('*',f);
(*p) = 1;
if (Denominator(frac)==1) {
if(Denominator(frac)==1) {
FPRINTF(f,"%s^%d",str,Numerator(frac));
} else {
}else{
FPRINTF(f,"%s^(%d/%d)",str,Numerator(frac),Denominator(frac));
}
}
}

void WriteDimensions(FILE *f, CONST dim_type *dimp)
{
void WriteDimensions(FILE *f, CONST dim_type *dimp){

Check warning on line 47 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L47

Added line #L47 was not covered by tests
struct fraction frac;
int printed=0;
if (IsWild(dimp)) {
if(IsWild(dimp)) {
FPRINTF(f,"*");
} else {
}else{
int i;
for( i=0; i<NUM_DIMENS; i++ ) {
frac = GetDimFraction(*dimp,i);
WriteFrac(f,frac,DimName(i),&printed);
}
if (!printed) FPRINTF(f,"dimensionless");
if(!printed) FPRINTF(f,"dimensionless");
}
}

/*
Append one dimensional power to the DString output,
dim
*dim
*dim^2
/dim^3
*dim^(1/2)
/dim^(3/2)
If '*printed' is nonzero, we need to write the * or / before the dim name.
If fraction is negative, we need to write /, else *; we don't write '-' exponent here.
If denominator is not one, we need to write parentheses around the fraction.
Fraction is assumed to already be simplified (denom is positive)
*/
static void WriteDimPowerString(Asc_DString *dp, const char *dim, const struct fraction frac, int *printed){
char expo[40];

if(*printed){
if(Numerator(frac) > 0){
Asc_DStringAppend(dp,"*",-1);

Check warning on line 80 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L80

Added line #L80 was not covered by tests
}else{
Asc_DStringAppend(dp,"/",-1);
}
}else{
if(Numerator(frac) < 0){
Asc_DStringAppend(dp,"1/",-1);

Check warning on line 86 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L86

Added line #L86 was not covered by tests
}
}
if(Denominator(frac)==1) {
if(Numerator(frac)==1){
sprintf(expo,"%s",dim);
}else{
sprintf(expo,"%s^%d",dim,Numerator(frac)>0 ? Numerator(frac) : -Numerator(frac));
}
}else{
sprintf(expo,"%s^(%d/%d)",dim,Numerator(frac)>0 ? Numerator(frac) : -Numerator(frac), Denominator(frac));
}
Asc_DStringAppend(dp,expo,-1);
*printed = 1;
}

ASC_DLLSPEC char *WriteDimensionStringFull(CONST dim_type *p){
Asc_DString ds;
Asc_DString *dp = &ds;
Asc_DStringInit(dp);

struct fraction frac;
int printed=0;

if(IsWild(p)) {
Asc_DStringAppend(dp,"*",-1);

Check warning on line 111 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L111

Added line #L111 was not covered by tests
}else{
int i;
// positive powers first
for( i=0; i<NUM_DIMENS; i++ ) {
frac = Simplify(GetDimFraction(*p,i));
if(Numerator(frac)>0){
WriteDimPowerString(dp,DimName(i),frac,&printed);
}
}
// negative powers next
for( i=0; i<NUM_DIMENS; i++ ) {
frac = Simplify(GetDimFraction(*p,i));
if(Numerator(frac)<0){
WriteDimPowerString(dp,DimName(i),frac,&printed);
}
}
if(!printed) Asc_DStringAppend(dp,"1",-1);
}

return Asc_DStringResult(dp);
}

char *WriteDimensionString(CONST dim_type *p)
{



char *WriteDimensionString(CONST dim_type *p){

Check warning on line 137 in ascend/compiler/dimen_io.c

View check run for this annotation

Codecov / codecov/patch

ascend/compiler/dimen_io.c#L137

Added line #L137 was not covered by tests
Asc_DString ds, *dsPtr;
char *result;
int numseen = 0, i, k;
char expo[40];
if (p==NULL) {
if(p==NULL){
return NULL;
}
if (IsWild(p)) {
if(IsWild(p)){
result = ASC_NEW_ARRAY(char,2);
sprintf(result,"*");
return result;
}
dsPtr = &ds;
Asc_DStringInit(dsPtr);
for (i=0; i < NUM_DIMENS; i++) {
for(i=0; i < NUM_DIMENS; i++){
k = GetDimPower(*(p),i);
if (k > 0) {
if (numseen) {
if(k > 0) {
if(numseen) {
Asc_DStringAppend(dsPtr,"*",1);
}
Asc_DStringAppend(dsPtr,DimName(i),-1);
if (k > 1) {
if(k > 1) {
sprintf(expo,"^%d",k);
Asc_DStringAppend(dsPtr,expo,-1);
}
numseen = 1;
}
}
if (!numseen) {
if(!numseen){
Asc_DStringAppend(dsPtr,"1",1);
}
for (i=0; i < NUM_DIMENS; i++) {
for(i=0; i < NUM_DIMENS; i++){
k = GetDimPower(*(p),i);
if (k < 0) {
if(k < 0) {
Asc_DStringAppend(dsPtr,"/",1);
Asc_DStringAppend(dsPtr,DimName(i),-1);
if (k < -1) {
if(k < -1) {
sprintf(expo,"^%d",-k);
Asc_DStringAppend(dsPtr,expo,-1);
}
Expand All @@ -107,3 +180,5 @@
result = Asc_DStringResult(dsPtr);
return result;
}

/* vim: set sw=2 ts=8 et: */
16 changes: 14 additions & 2 deletions ascend/compiler/dimen_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@
*/

/**
* Write the human readable and parser edible string form of the
* dimen given.
Write the human readable and parser edible string form of the
dimen given.

FIXME this function does not correctly output things like fractional powers,
which affects reporting of dimensionality errors (as of Dec 2023). Integer
powers are fine, and integer powers are somehow all that ASCEND aims to
support (although TODO document where that design decision is enforced)
*/
ASC_DLLSPEC char *WriteDimensionString(CONST dim_type *p);

/**
Write the 'canonical, indigestible' form as a string. The caller owns
the returned string and must free it. This function is useful for
clearer error messages from chkdim_check_system.
*/
ASC_DLLSPEC char *WriteDimensionStringFull(CONST dim_type *p);

/**
* Write the canonical, indigestible form of the dimen with
* full numeric details. ugh.
Expand Down
2 changes: 2 additions & 0 deletions ascend/compiler/relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#ifndef ASC_RELATION_H
#define ASC_RELATION_H

#include "instance_enum.h"
#include "rel_blackbox.h"
#include "relerr.h"

/** @addtogroup compiler_rel Compiler Relations
Expand Down
Loading
Loading