Actual source code: ex36A.c
2: static char help[] = "Transistor amplifier (autonomous).\n";
4: /*F
5: M y'=f(y)
7: Useful options: -ts_monitor_lg_solution -ts_monitor_lg_timestep -lg_indicate_data_points 0
8: F*/
10: /*
11: Include "petscts.h" so that we can use TS solvers. Note that this
12: file automatically includes:
13: petscsys.h - base PETSc routines petscvec.h - vectors
14: petscmat.h - matrices
15: petscis.h - index sets petscksp.h - Krylov subspace methods
16: petscviewer.h - viewers petscpc.h - preconditioners
17: petscksp.h - linear solvers
18: */
19: #include <petscts.h>
21: FILE *gfilepointer_data, *gfilepointer_info;
23: /* Defines the source */
24: PetscErrorCode Ue(PetscScalar t, PetscScalar *U)
25: {
26: PetscFunctionBeginUser;
27: U = 0.4 * sin(200 * pi * t);
28: PetscFunctionReturn(PETSC_SUCCESS);
29: }
30: * /
32: /*
33: Defines the DAE passed to the time solver
34: */
35: static PetscErrorCode IFunctionImplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, Vec F, void *ctx)
36: {
37: const PetscScalar *y, *ydot;
38: PetscScalar *f;
40: PetscFunctionBeginUser;
41: /* The next three lines allow us to access the entries of the vectors directly */
42: PetscCall(VecGetArrayRead(Y, &y));
43: PetscCall(VecGetArrayRead(Ydot, &ydot));
44: PetscCall(VecGetArray(F, &f));
46: f[0] = PetscSinReal(200 * PETSC_PI * y[5]) / 2500. - y[0] / 1000. - ydot[0] / 1.e6 + ydot[1] / 1.e6;
47: f[1] = 0.0006666766666666667 - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 1.e8 - y[1] / 4500. + ydot[0] / 1.e6 - ydot[1] / 1.e6;
48: f[2] = -1.e-6 + PetscExpReal((500 * (y[1] - y[2])) / 13.) / 1.e6 - y[2] / 9000. - ydot[2] / 500000.;
49: f[3] = 0.0006676566666666666 - (99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 1.e8 - y[3] / 9000. - (3 * ydot[3]) / 1.e6 + (3 * ydot[4]) / 1.e6;
50: f[4] = -y[4] / 9000. + (3 * ydot[3]) / 1.e6 - (3 * ydot[4]) / 1.e6;
51: f[5] = -1 + ydot[5];
53: PetscCall(VecRestoreArrayRead(Y, &y));
54: PetscCall(VecRestoreArrayRead(Ydot, &ydot));
55: PetscCall(VecRestoreArray(F, &f));
56: PetscFunctionReturn(PETSC_SUCCESS);
57: }
59: /*
60: Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian.
61: */
62: static PetscErrorCode IJacobianImplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, PetscReal a, Mat A, Mat B, void *ctx)
63: {
64: PetscInt rowcol[] = {0, 1, 2, 3, 4, 5};
65: const PetscScalar *y, *ydot;
66: PetscScalar J[6][6];
68: PetscFunctionBeginUser;
69: PetscCall(VecGetArrayRead(Y, &y));
70: PetscCall(VecGetArrayRead(Ydot, &ydot));
72: PetscCall(PetscMemzero(J, sizeof(J)));
74: J[0][0] = -0.001 - a / 1.e6;
75: J[0][1] = a / 1.e6;
76: J[0][5] = (2 * PETSC_PI * PetscCosReal(200 * PETSC_PI * y[5])) / 25.;
77: J[1][0] = a / 1.e6;
78: J[1][1] = -0.00022222222222222223 - a / 1.e6 - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 2.6e6;
79: J[1][2] = PetscExpReal((500 * (y[1] - y[2])) / 13.) / 2.6e6;
80: J[2][1] = PetscExpReal((500 * (y[1] - y[2])) / 13.) / 26000.;
81: J[2][2] = -0.00011111111111111112 - a / 500000. - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 26000.;
82: J[3][1] = (-99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 2.6e6;
83: J[3][2] = (99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 2.6e6;
84: J[3][3] = -0.00011111111111111112 - (3 * a) / 1.e6;
85: J[3][4] = (3 * a) / 1.e6;
86: J[4][3] = (3 * a) / 1.e6;
87: J[4][4] = -0.00011111111111111112 - (3 * a) / 1.e6;
88: J[5][5] = a;
90: PetscCall(MatSetValues(B, 6, rowcol, 6, rowcol, &J[0][0], INSERT_VALUES));
92: PetscCall(VecRestoreArrayRead(Y, &y));
93: PetscCall(VecRestoreArrayRead(Ydot, &ydot));
95: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
96: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
97: if (A != B) {
98: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
99: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
100: }
101: PetscFunctionReturn(PETSC_SUCCESS);
102: }
104: int main(int argc, char **argv)
105: {
106: TS ts; /* ODE integrator */
107: Vec Y; /* solution will be stored here */
108: Mat A; /* Jacobian matrix */
109: PetscMPIInt size;
110: PetscInt n = 6;
111: PetscScalar *y;
113: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114: Initialize program
115: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
116: PetscFunctionBeginUser;
117: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
118: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
119: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Only for sequential runs");
121: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122: Create necessary matrix and vectors
123: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
124: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
125: PetscCall(MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE));
126: PetscCall(MatSetFromOptions(A));
127: PetscCall(MatSetUp(A));
129: PetscCall(MatCreateVecs(A, &Y, NULL));
131: PetscCall(VecGetArray(Y, &y));
132: y[0] = 0.0;
133: y[1] = 3.0;
134: y[2] = y[1];
135: y[3] = 6.0;
136: y[4] = 0.0;
137: y[5] = 0.0;
138: PetscCall(VecRestoreArray(Y, &y));
140: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141: Create timestepping solver context
142: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143: PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
144: PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
145: PetscCall(TSSetType(ts, TSARKIMEX));
146: PetscCall(TSSetEquationType(ts, TS_EQ_DAE_IMPLICIT_INDEX1));
147: PetscCall(TSARKIMEXSetFullyImplicit(ts, PETSC_TRUE));
148: /*PetscCall(TSSetType(ts,TSROSW));*/
149: PetscCall(TSSetIFunction(ts, NULL, IFunctionImplicit, NULL));
150: PetscCall(TSSetIJacobian(ts, A, A, IJacobianImplicit, NULL));
152: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153: Set initial conditions
154: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155: PetscCall(TSSetSolution(ts, Y));
157: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158: Set solver options
159: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
160: PetscCall(TSSetMaxTime(ts, 0.15));
161: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));
162: PetscCall(TSSetTimeStep(ts, .001));
163: PetscCall(TSSetFromOptions(ts));
165: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166: Do Time stepping
167: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
168: PetscCall(TSSolve(ts, Y));
170: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171: Free work space. All PETSc objects should be destroyed when they are no longer needed.
172: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
173: PetscCall(MatDestroy(&A));
174: PetscCall(VecDestroy(&Y));
175: PetscCall(TSDestroy(&ts));
176: PetscCall(PetscFinalize());
177: return 0;
178: }