Date: 2026-07-30T21:14:23+00:00

Actual source code: axis.c

      1: #include <petsc/private/drawimpl.h>

      3: /*
      4:    val is the label value.  sep is the separation to the next (or previous)
      5:    label; this is useful in determining how many significant figures to
      6:    keep.
      7:  */
      8: PetscErrorCode PetscADefLabel(PetscReal val, PetscReal sep, char **p)
      9: {
     10:   static char buf[40];

     12:   PetscFunctionBegin;
     13:   /* Find the string */
     14:   if (PetscAbsReal(val) / sep < 1.e-4) {
     15:     buf[0] = '0';
     16:     buf[1] = 0;
     17:   } else {
     18:     PetscCall(PetscSNPrintf(buf, PETSC_STATIC_ARRAY_LENGTH(buf), "%0.1e", (double)val));
     19:     PetscCall(PetscStripZerosPlus(buf));
     20:     PetscCall(PetscStripe0(buf));
     21:     PetscCall(PetscStripInitialZero(buf));
     22:     PetscCall(PetscStripAllZeros(buf));
     23:     PetscCall(PetscStripTrailingZeros(buf));
     24:   }
     25:   *p = buf;
     26:   PetscFunctionReturn(PETSC_SUCCESS);
     27: }

     29: /* Finds "nice" locations for the ticks */
     30: PetscErrorCode PetscADefTicks(PetscReal low, PetscReal high, int num, int *ntick, PetscReal *tickloc, int maxtick)
     31: {
     32:   int       i, power;
     33:   PetscReal x = 0.0, base = 0.0, eps;

     35:   PetscFunctionBegin;
     36:   PetscCall(PetscAGetBase(low, high, num, &base, &power));
     37:   PetscCall(PetscAGetNice(low, base, -1, &x));

     39:   /* Values are of the form j * base */
     40:   /* Find the starting value */
     41:   if (x < low) x += base;

     43:   i   = 0;
     44:   eps = base / 10;
     45:   while (i < maxtick && x <= high + eps) {
     46:     tickloc[i++] = x;
     47:     x += base;
     48:   }
     49:   *ntick = i;
     50:   if (i) tickloc[i - 1] = PetscMin(tickloc[i - 1], high);
     51:   if (i < 2 && num < 10) PetscCall(PetscADefTicks(low, high, num + 1, ntick, tickloc, maxtick));
     52:   PetscFunctionReturn(PETSC_SUCCESS);
     53: }

     55: #define EPS 1.e-6

     57: static PetscErrorCode PetscExp10(PetscReal d, PetscReal *result)
     58: {
     59:   PetscFunctionBegin;
     60:   *result = PetscPowReal((PetscReal)10.0, d);
     61:   PetscFunctionReturn(PETSC_SUCCESS);
     62: }

     64: static PetscErrorCode PetscMod(PetscReal x, PetscReal y, PetscReal *result)
     65: {
     66:   int i;

     68:   PetscFunctionBegin;
     69:   if (y == 1) {
     70:     *result = 0.0;
     71:     PetscFunctionReturn(PETSC_SUCCESS);
     72:   }
     73:   i = ((int)x) / ((int)y);
     74:   x = x - ((PetscReal)i) * y;
     75:   while (x > y) x -= y;
     76:   *result = x;
     77:   PetscFunctionReturn(PETSC_SUCCESS);
     78: }

     80: static PetscErrorCode PetscCopysign(PetscReal a, PetscReal b, PetscReal *result)
     81: {
     82:   PetscFunctionBegin;
     83:   if (b >= 0) *result = a;
     84:   else *result = -a;
     85:   PetscFunctionReturn(PETSC_SUCCESS);
     86: }

     88: /*
     89:     Given a value "in" and a "base", return a nice value.
     90:     based on "sign", extend up (+1) or down (-1)
     91:  */
     92: PetscErrorCode PetscAGetNice(PetscReal in, PetscReal base, int sign, PetscReal *result)
     93: {
     94:   PetscReal etmp, s, s2, m;

     96:   PetscFunctionBegin;
     97:   PetscCall(PetscCopysign(0.5, (double)sign, &s));
     98:   etmp = in / base + 0.5 + s;
     99:   PetscCall(PetscCopysign(0.5, etmp, &s));
    100:   PetscCall(PetscCopysign(EPS * etmp, (double)sign, &s2));
    101:   etmp = etmp - 0.5 + s - s2;
    102:   PetscCall(PetscMod(etmp, 1.0, &m));
    103:   etmp    = base * (etmp - m);
    104:   *result = etmp;
    105:   PetscFunctionReturn(PETSC_SUCCESS);
    106: }

    108: PetscErrorCode PetscAGetBase(PetscReal vmin, PetscReal vmax, int num, PetscReal *Base, int *power)
    109: {
    110:   PetscReal        base, ftemp, e10;
    111:   static PetscReal base_try[5] = {10.0, 5.0, 2.0, 1.0, 0.5};
    112:   int              i;

    114:   PetscFunctionBegin;
    115:   /* labels of the form n * BASE */
    116:   /* get an approximate value for BASE */
    117:   base = (vmax - vmin) / (double)(num + 1);

    119:   /* make it of form   m x 10^power,  m in [1.0, 10) */
    120:   if (base <= 0.0) {
    121:     base = PetscAbsReal(vmin);
    122:     if (base < 1.0) base = 1.0;
    123:   }
    124:   ftemp = PetscLog10Real((1.0 + EPS) * base);
    125:   if (ftemp < 0.0) ftemp -= 1.0;
    126:   *power = (int)ftemp;
    127:   PetscCall(PetscExp10((double)-*power, &e10));
    128:   base = base * e10;
    129:   if (base < 1.0) base = 1.0;
    130:   /* now reduce it to one of 1, 2, or 5 */
    131:   for (i = 1; i < 5; i++) {
    132:     if (base >= base_try[i]) {
    133:       PetscCall(PetscExp10((double)*power, &e10));
    134:       base = base_try[i - 1] * e10;
    135:       if (i == 1) *power = *power + 1;
    136:       break;
    137:     }
    138:   }
    139:   *Base = base;
    140:   PetscFunctionReturn(PETSC_SUCCESS);
    141: }